@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
public function up(): void
|
||||
{
|
||||
Schema::table('tenants', fn (Blueprint $table) => $table->json('requisition_approval_policy')->nullable());
|
||||
Schema::table('approval_steps', fn (Blueprint $table) => $table->json('routing')->nullable());
|
||||
}
|
||||
|
||||
public function down(): void
|
||||
{
|
||||
if (DB::table('tenants')->whereNotNull('requisition_approval_policy')->exists() || DB::table('approval_steps')->whereNotNull('routing')->exists()) {
|
||||
throw new LogicException('Preserve os planos e níveis configurados antes de reverter a estrutura.');
|
||||
}
|
||||
Schema::table('approval_steps', fn (Blueprint $table) => $table->dropColumn('routing'));
|
||||
Schema::table('tenants', fn (Blueprint $table) => $table->dropColumn('requisition_approval_policy'));
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,85 @@
|
||||
<?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.');
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user