Files
compras/database/qa-migrations/2026_11_22_000004_create_budget_distributions.php
T
nala 36dcfdf00c
tests / ci (push) Canceled after 0s
commit melhorias compras4
2026-11-22 16:28:03 +02:00

86 lines
4.8 KiB
PHP

<?php
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.
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('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()');
}
}
});
}
public function down(): void
{
throw new LogicException('Use the verified pre-extension backup; distribution history is not deleted by a downgrade.');
}
};