@@ -1,7 +1,7 @@
|
||||
<?php
|
||||
|
||||
use App\Services\BudgetDistributionSchema;
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
// Explicit QA path: not discovered by the normal application migration directory.
|
||||
@@ -16,66 +16,7 @@ return new class extends Migration
|
||||
throw new LogicException('Distribution schema requires the guarded disposable QA database.');
|
||||
}
|
||||
|
||||
$db->transaction(function () use ($db): void {
|
||||
$schema = $db->getSchemaBuilder();
|
||||
$schema->table('requisitions', fn (Blueprint $table) => $table->json('budget_distribution_preparation')->nullable());
|
||||
$schema->create('budget_distributions', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->foreignId('tenant_id')->constrained()->restrictOnDelete();
|
||||
$table->foreignId('requisition_id')->unique()->constrained()->restrictOnDelete();
|
||||
$table->string('phase');
|
||||
$table->json('snapshot');
|
||||
$table->foreignId('created_by')->constrained('users')->restrictOnDelete();
|
||||
$table->timestamps();
|
||||
});
|
||||
$schema->create('budget_distribution_events', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->foreignId('tenant_id')->constrained()->restrictOnDelete();
|
||||
$table->foreignId('distribution_id')->constrained('budget_distributions')->restrictOnDelete();
|
||||
$table->string('kind', 50);
|
||||
$table->uuid('operation_key');
|
||||
$table->string('payload_hash', 64);
|
||||
$table->json('result');
|
||||
$table->json('snapshot');
|
||||
$table->foreignId('created_by')->constrained('users')->restrictOnDelete();
|
||||
$table->timestamp('created_at');
|
||||
$table->unique(['tenant_id', 'operation_key']);
|
||||
$table->unique(['distribution_id', 'kind']);
|
||||
});
|
||||
$schema->create('budget_distribution_entries', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->foreignId('tenant_id')->constrained()->restrictOnDelete();
|
||||
$table->foreignId('distribution_id')->constrained('budget_distributions')->restrictOnDelete();
|
||||
$table->string('allocation_key', 120);
|
||||
$table->foreignId('budget_id')->constrained('budgets')->restrictOnDelete();
|
||||
$table->string('kind', 50);
|
||||
$table->bigInteger('reserved_minor');
|
||||
$table->bigInteger('committed_minor');
|
||||
$table->foreignId('created_by')->constrained('users')->restrictOnDelete();
|
||||
$table->timestamp('created_at');
|
||||
$table->unique(['distribution_id', 'allocation_key', 'kind']);
|
||||
});
|
||||
$schema->create('budget_distribution_consumptions', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->foreignId('tenant_id')->constrained()->restrictOnDelete();
|
||||
$table->foreignId('distribution_id')->constrained('budget_distributions')->restrictOnDelete();
|
||||
$table->string('allocation_key', 120);
|
||||
$table->foreignId('budget_id')->constrained('budgets')->restrictOnDelete();
|
||||
$table->foreignId('invoice_id')->constrained('invoices')->restrictOnDelete();
|
||||
$table->bigInteger('amount_minor');
|
||||
$table->bigInteger('subtotal_minor');
|
||||
$table->bigInteger('total_minor');
|
||||
$table->foreignId('created_by')->constrained('users')->restrictOnDelete();
|
||||
$table->timestamp('created_at');
|
||||
$table->unique(['invoice_id', 'allocation_key']);
|
||||
});
|
||||
if ($db->getDriverName() === 'pgsql') {
|
||||
$db->unprepared("CREATE OR REPLACE FUNCTION sgc_distribution_history_guard() RETURNS trigger LANGUAGE plpgsql AS 'BEGIN RAISE EXCEPTION ''Distribution history is append only''; END'");
|
||||
foreach (['budget_distribution_events', 'budget_distribution_entries', 'budget_distribution_consumptions'] as $table) {
|
||||
$db->statement('CREATE TRIGGER distribution_history_guard BEFORE UPDATE OR DELETE ON '.$table.' FOR EACH ROW EXECUTE FUNCTION sgc_distribution_history_guard()');
|
||||
}
|
||||
}
|
||||
});
|
||||
BudgetDistributionSchema::install();
|
||||
}
|
||||
|
||||
public function down(): void
|
||||
|
||||
@@ -0,0 +1,84 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
// Optional contract structure. Deliberately outside normal and deployment migrations.
|
||||
return new class extends Migration
|
||||
{
|
||||
public function up(): void
|
||||
{
|
||||
$db = DB::connection();
|
||||
$isolated = ($db->getDriverName() === 'sqlite' && $db->getDatabaseName() === ':memory:')
|
||||
|| ($db->getDriverName() === 'pgsql' && preg_match('/^compras_receipts_test_[a-f0-9]{12}$/D', $db->getDatabaseName()));
|
||||
if (! app()->environment('testing') || ! $isolated) {
|
||||
throw new LogicException('Order parts require the guarded disposable QA database.');
|
||||
}
|
||||
$schema = $db->getSchemaBuilder();
|
||||
$tables = ['budget_order_commands', 'budget_order_bindings', 'budget_order_allocations', 'budget_order_consumptions'];
|
||||
$found = count(array_filter($tables, fn ($table) => $schema->hasTable($table)));
|
||||
if ($found !== 0) {
|
||||
throw new LogicException('Order-parts schema must be installed once into an empty isolated composition.');
|
||||
}
|
||||
if (! $schema->hasTable('budget_distribution_entries')) {
|
||||
throw new LogicException('Install the explicit V1 distribution schema first.');
|
||||
}
|
||||
$db->transaction(function () use ($schema, $db, $tables): void {
|
||||
$schema->create('budget_order_commands', function (Blueprint $table): void {
|
||||
$table->id();
|
||||
$table->foreignId('tenant_id')->constrained()->restrictOnDelete();
|
||||
$table->string('kind', 20);
|
||||
$table->string('document_key', 60);
|
||||
$table->uuid('operation_key');
|
||||
$table->string('payload_hash', 64);
|
||||
$table->json('result');
|
||||
$table->json('snapshot');
|
||||
$table->foreignId('created_by')->constrained('users')->restrictOnDelete();
|
||||
$table->timestamp('created_at');
|
||||
$table->unique(['tenant_id', 'operation_key']);
|
||||
$table->unique(['tenant_id', 'kind', 'document_key']);
|
||||
});
|
||||
$schema->create('budget_order_bindings', function (Blueprint $table): void {
|
||||
$table->id();
|
||||
$table->foreignId('tenant_id')->constrained()->restrictOnDelete();
|
||||
$table->foreignId('command_id')->constrained('budget_order_commands')->restrictOnDelete();
|
||||
$table->foreignId('distribution_id')->constrained('budget_distributions')->restrictOnDelete();
|
||||
$table->unsignedInteger('source_version');
|
||||
$table->unique(['distribution_id', 'source_version']);
|
||||
$table->unique(['command_id', 'distribution_id']);
|
||||
});
|
||||
foreach (['budget_order_allocations', 'budget_order_consumptions'] as $name) {
|
||||
$schema->create($name, function (Blueprint $table) use ($name): void {
|
||||
$table->id();
|
||||
$table->foreignId('tenant_id')->constrained()->restrictOnDelete();
|
||||
$table->foreignId('command_id')->constrained('budget_order_commands')->restrictOnDelete();
|
||||
$table->foreignId('distribution_id')->constrained('budget_distributions')->restrictOnDelete();
|
||||
$table->uuid('order_key');
|
||||
$table->string('allocation_key', 120);
|
||||
$table->foreignId('budget_id')->constrained('budgets')->restrictOnDelete();
|
||||
$table->bigInteger('subtotal_minor');
|
||||
$table->bigInteger('total_minor');
|
||||
$table->bigInteger('amount_minor');
|
||||
if ($name === 'budget_order_consumptions') {
|
||||
$table->foreignId('invoice_id')->constrained()->restrictOnDelete();
|
||||
} else {
|
||||
$table->string('kind', 20);
|
||||
}
|
||||
$table->unique(['command_id', 'distribution_id', 'order_key', 'allocation_key'], $name === 'budget_order_allocations' ? 'order_allocation_binding' : 'order_consumption_binding');
|
||||
});
|
||||
}
|
||||
if ($db->getDriverName() === 'pgsql') {
|
||||
// Existing function always raises; no mutation or deletion of accepted history.
|
||||
foreach ($tables as $table) {
|
||||
$db->statement('CREATE TRIGGER distribution_history_guard BEFORE UPDATE OR DELETE ON '.$table.' FOR EACH ROW EXECUTE FUNCTION sgc_distribution_history_guard()');
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public function down(): void
|
||||
{
|
||||
throw new LogicException('Use a verified pre-extension backup; order-part history is not deleted by downgrade.');
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user