getDriverName() === 'sqlite' && $db->getDatabaseName() === ':memory:') || ($db->getDriverName() === 'pgsql' && $db->getConfig('host') === '127.0.0.1' && preg_match('/^compras_receipts_test_[a-f0-9]{12}$/D', $db->getDatabaseName())); if (! app()->environment('testing') || ! $isolated) { throw new LogicException('Approval and budget cycle schema requires isolated QA.'); } $schema = $db->getSchemaBuilder(); if ($schema->hasTable('budget_requisition_cycles') || $schema->hasColumn('requisitions', 'approval_cycle') || $schema->hasColumn('budget_entries', 'requisition_cycle') || $schema->hasColumn('workflow_notices', 'event_kind')) { throw new LogicException('Do not reinstall or repair partial cycle structures automatically.'); } $schema->table('requisitions', fn (Blueprint $table) => $table->unsignedInteger('approval_cycle')->default(1)); $schema->table('approval_steps', function (Blueprint $table) { $table->unsignedInteger('approval_cycle')->default(1); $table->dropUnique(['requisition_id', 'position']); $table->unique(['requisition_id', 'approval_cycle', 'position'], 'approval_step_cycle_position'); }); $schema->table('budget_entries', function (Blueprint $table) { $table->unsignedInteger('requisition_cycle')->default(1); $table->dropUnique(['requisition_id', 'kind']); $table->unique(['requisition_id', 'requisition_cycle', 'kind'], 'budget_entry_cycle_kind'); }); $schema->table('workflow_notices', function (Blueprint $table) { $table->string('event_kind', 20)->default('task'); $table->dropUnique(['approval_step_id', 'recipient_id']); $table->unique(['approval_step_id', 'recipient_id', 'event_kind'], 'workflow_notice_event_recipient'); }); if ($schema->hasTable('budget_distributions')) { $schema->table('budget_distributions', function (Blueprint $table) { $table->unsignedInteger('requisition_cycle')->default(1); $table->dropUnique(['requisition_id']); $table->unique(['requisition_id', 'requisition_cycle'], 'budget_distribution_cycle'); }); } $schema->create('budget_requisition_cycles', function (Blueprint $table) { $table->id(); $table->foreignId('tenant_id')->constrained()->restrictOnDelete(); $table->foreignId('requisition_id')->constrained()->restrictOnDelete(); $table->unsignedInteger('cycle'); $table->string('state', 20); $table->uuid('open_operation'); $table->foreignId('opened_by')->constrained('users')->restrictOnDelete(); $table->timestamp('opened_at'); $table->uuid('close_operation')->nullable(); $table->foreignId('closed_by')->nullable()->constrained('users')->restrictOnDelete(); $table->timestamp('closed_at')->nullable(); $table->unique(['tenant_id', 'requisition_id', 'cycle'], 'budget_requisition_cycle'); $table->unique(['tenant_id', 'open_operation']); $table->unique(['tenant_id', 'close_operation']); }); } public function down(): void { throw new LogicException('Restore the verified pre-cycle backup and compatible artifact; never delete previous approval or financial cycles.'); } };