commit3
This commit is contained in:
@@ -1,17 +1,27 @@
|
||||
<?php
|
||||
|
||||
// Explicitly called by disposable/new QA setup only, never registered in host migrations.
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
$db=DB::connection();
|
||||
if($db->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('Payables schema refused outside isolated QA.');
|
||||
$scope=function(Blueprint $t){foreach(['tenant_id','company_id','unit_id'] as $k)$t->unsignedBigInteger($k);};
|
||||
Schema::create('pa_access',function(Blueprint $t)use($scope){$t->id();$scope($t);$t->unsignedBigInteger('user_id');$t->string('person');$t->string('action');$t->unique(['tenant_id','company_id','unit_id','user_id','action']);});
|
||||
Schema::create('pa_sources',function(Blueprint $t)use($scope){$t->id();$scope($t);$t->string('source_key',64)->unique();$t->unsignedBigInteger('local_source_id');$t->timestamp('created_at');});
|
||||
Schema::create('pa_proposals',function(Blueprint $t)use($scope){$t->id();$scope($t);$t->foreignId('source_id')->constrained('pa_sources');$t->string('source_revision',64);$t->integer('capture_version');$t->jsonb('snapshot');$t->jsonb('missing');$t->unsignedBigInteger('prepared_by');$t->string('prepared_person');$t->text('reason');$t->timestamp('created_at');$t->unique(['source_id','capture_version']);});
|
||||
Schema::create('pa_obligations',function(Blueprint $t)use($scope){$t->id();$scope($t);$t->foreignId('source_id')->unique()->constrained('pa_sources');$t->foreignId('proposal_id')->constrained('pa_proposals');$t->string('source_revision',64);$t->bigInteger('principal_minor');$t->string('currency',3);$t->date('due_on')->nullable();$t->unsignedBigInteger('recognised_by');$t->string('recognised_person');$t->text('reason');$t->timestamp('created_at');});
|
||||
Schema::create('pa_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('pa_events',function(Blueprint $t)use($scope){$t->id();$scope($t);$t->foreignId('source_id')->constrained('pa_sources');$t->unsignedBigInteger('actor_id');$t->string('kind');$t->jsonb('payload');$t->timestamp('created_at');});
|
||||
Schema::create('pa_outbox',function(Blueprint $t){$t->foreignId('event_id')->primary()->constrained('pa_events');$t->string('state');});
|
||||
DB::unprepared("CREATE FUNCTION pa_append_only() RETURNS trigger LANGUAGE plpgsql AS $$ BEGIN RAISE EXCEPTION 'PA_APPEND_ONLY'; END $$");
|
||||
foreach(['pa_sources','pa_proposals','pa_obligations','pa_events','pa_outbox','pa_commands'] as $t)DB::unprepared("CREATE TRIGGER pa_history_immutable BEFORE UPDATE OR DELETE ON {$t} FOR EACH ROW EXECUTE FUNCTION pa_append_only()");
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
use Tempo\Finance\Payables\PayablesSchema;
|
||||
|
||||
$db = DB::connection();
|
||||
if ($db->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('Payables schema refused outside isolated QA.');
|
||||
}
|
||||
// Compras migrations now install the same shared schema before QA fixtures.
|
||||
if (Schema::hasTable('pa_sources')) {
|
||||
foreach (['pa_access', 'pa_sources', 'pa_proposals', 'pa_obligations', 'pa_commands', 'pa_events', 'pa_outbox'] as $table) {
|
||||
if (! Schema::hasTable($table)) {
|
||||
throw new LogicException('Incomplete payables schema; no automatic repair.');
|
||||
}
|
||||
}
|
||||
$triggers = DB::selectOne("select count(*) as n from pg_trigger where tgname='pa_history_immutable' and tgrelid in ('pa_sources'::regclass,'pa_proposals'::regclass,'pa_obligations'::regclass,'pa_commands'::regclass,'pa_events'::regclass,'pa_outbox'::regclass)");
|
||||
if ((int) $triggers->n !== 6) {
|
||||
throw new LogicException('Payables append-only protection missing.');
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
require_once __DIR__.'/src/PayablesSchema.php';
|
||||
PayablesSchema::install();
|
||||
|
||||
Reference in New Issue
Block a user