getDriverName() !== 'pgsql' || ! preg_match('/^(compras_receipts_test_|payable_sgf_)[a-f0-9]{12}$/D', $db->getDatabaseName()) || $db->selectOne('select current_database() as d')->d !== $db->getDatabaseName()) { throw new \LogicException('Settlement schema requires isolated QA.'); } if (array_filter(self::TABLES, fn ($t) => Schema::hasTable($t))) { throw new \LogicException('Settlement schema already exists; no automatic repair.'); } $scope = static function (Blueprint $t): void { foreach (['tenant_id', 'company_id', 'unit_id'] as $k) { $t->unsignedBigInteger($k); } }; DB::transaction(function () use ($scope): void { Schema::create('ps_payments', function (Blueprint $t) use ($scope) { $t->id(); $scope($t); $t->foreignId('obligation_id')->constrained('pa_obligations'); $t->string('fact_key', 64)->unique(); $t->string('evidence_key', 64)->unique(); $t->string('supplier_key', 64); $t->string('currency', 3); $t->bigInteger('amount_minor'); $t->date('paid_on'); $t->string('fact_reference'); $t->string('evidence_reference'); $t->string('payload_hash', 64); $t->jsonb('snapshot'); $t->unsignedBigInteger('prepared_by'); $t->string('prepared_person'); $t->text('reason'); $t->timestamp('created_at'); }); Schema::create('ps_payment_decisions', function (Blueprint $t) use ($scope) { $t->id(); $scope($t); $t->foreignId('payment_id')->unique()->constrained('ps_payments'); $t->string('decision'); $t->unsignedBigInteger('actor_id'); $t->string('person'); $t->string('evidence_reference'); $t->text('reason'); $t->timestamp('created_at'); }); Schema::create('ps_proposals', function (Blueprint $t) use ($scope) { $t->id(); $scope($t); $t->foreignId('payment_id')->constrained('ps_payments'); $t->string('reference_key', 64)->unique(); $t->string('reference'); $t->string('plan_hash', 64); $t->jsonb('plan'); $t->unsignedBigInteger('prepared_by'); $t->string('prepared_person'); $t->text('reason'); $t->timestamp('created_at'); $t->unique(['payment_id', 'plan_hash']); }); Schema::create('ps_allocation_decisions', function (Blueprint $t) use ($scope) { $t->id(); $scope($t); $t->foreignId('proposal_id')->unique()->constrained('ps_proposals'); $t->string('decision'); $t->unsignedBigInteger('actor_id'); $t->string('person'); $t->text('reason'); $t->timestamp('created_at'); }); Schema::create('ps_allocation_aliases', function (Blueprint $t) use ($scope) { $t->id(); $scope($t); $t->string('reference_key', 64)->unique(); $t->string('reference'); $t->foreignId('proposal_id')->constrained('ps_proposals'); $t->unsignedBigInteger('actor_id'); $t->timestamp('created_at'); }); Schema::create('ps_allocations', function (Blueprint $t) use ($scope) { $t->id(); $scope($t); $t->foreignId('payment_id')->constrained('ps_payments'); $t->foreignId('obligation_id')->constrained('pa_obligations'); $t->foreignId('decision_id')->constrained('ps_allocation_decisions'); $t->bigInteger('amount_minor'); $t->string('source_revision', 64); $t->timestamp('created_at'); $t->unique(['payment_id', 'obligation_id']); }); Schema::create('ps_commands', function (Blueprint $t) use ($scope) { $t->id(); $scope($t); $t->unsignedBigInteger('actor_id'); $t->uuid('command_key'); $t->string('request_hash', 64); $t->jsonb('result'); $t->timestamp('created_at'); $t->unique(['tenant_id', 'company_id', 'unit_id', 'actor_id', 'command_key']); }); Schema::create('ps_events', function (Blueprint $t) use ($scope) { $t->id(); $scope($t); $t->foreignId('payment_id')->constrained('ps_payments'); $t->unsignedBigInteger('actor_id'); $t->string('person'); $t->string('kind'); $t->jsonb('payload'); $t->timestamp('created_at'); }); Schema::create('ps_outbox', function (Blueprint $t) { $t->foreignId('event_id')->primary()->constrained('ps_events'); $t->string('state'); }); foreach (self::TABLES as $table) { DB::unprepared("CREATE TRIGGER ps_history_immutable BEFORE UPDATE OR DELETE ON {$table} FOR EACH ROW EXECUTE FUNCTION pa_append_only()"); } foreach (['ps_payments', 'ps_allocations'] as $table) { DB::unprepared("ALTER TABLE {$table} ADD CONSTRAINT {$table}_positive CHECK (amount_minor > 0)"); } }); } }