commit melhorias compras5
tests / ci (push) Waiting to run

This commit is contained in:
2026-11-22 19:39:09 +02:00
parent 36dcfdf00c
commit db1615fac1
79 changed files with 4082 additions and 120 deletions
@@ -0,0 +1,32 @@
<?php
use App\Services\BudgetDistributionSchema;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Facades\DB;
// Deliberately outside automatic migrations. Only the reviewed installer supplies this scope.
return new class extends Migration
{
public function up(): void
{
$db = DB::connection();
$scope = config('budget-distributions.schema_installation');
if (! app()->runningInConsole() || ! app()->environment(['local', 'testing', 'payable-testing'])
|| $db->getDriverName() !== 'pgsql' || $db->getConfig('host') !== '127.0.0.1'
|| ! is_array($scope) || ($scope['database'] ?? null) !== $db->getDatabaseName()
|| ($scope['username'] ?? null) !== $db->getConfig('username')
|| ! preg_match('/^compras_(receipts_test|distribution_restore)_[a-f0-9]{12}$/D', $db->getDatabaseName())) {
throw new LogicException('Distribution pilot schema requires the exact authorised installer scope.');
}
$identity = $db->selectOne('select current_database() as db, current_user as role');
if ($identity === null || $identity->db !== $scope['database'] || $identity->role !== $scope['username']) {
throw new LogicException('Distribution pilot schema identity mismatch.');
}
BudgetDistributionSchema::install();
}
public function down(): void
{
throw new LogicException('Preserve distribution history; use reviewed recovery or a compatible forward correction.');
}
};
@@ -0,0 +1,18 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
public function up(): void
{
Schema::table('requisition_items', fn (Blueprint $table) => $table->json('category_context')->nullable());
}
public function down(): void
{
throw new LogicException('Preserve recorded category context; use reviewed recovery instead of deleting history.');
}
};
@@ -0,0 +1,30 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
public function up(): void
{
Schema::create('procurement_saved_searches', function (Blueprint $table) {
$table->id();
$table->foreignId('tenant_id')->constrained()->restrictOnDelete();
$table->foreignId('user_id')->constrained()->restrictOnDelete();
$table->string('name', 100);
$table->string('term', 100);
$table->string('active_name_key', 64)->nullable();
$table->unsignedInteger('version')->default(1);
$table->timestamp('archived_at')->nullable();
$table->timestamps();
$table->unique(['tenant_id', 'user_id', 'active_name_key'], 'saved_search_active_name_unique');
$table->index(['tenant_id', 'user_id', 'archived_at'], 'saved_search_owner_index');
});
}
public function down(): void
{
throw new LogicException('Preserve saved search criteria; no automatic history deletion.');
}
};
@@ -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.');
}
};