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

This commit is contained in:
2026-11-23 06:37:16 +02:00
parent db1615fac1
commit dc73540558
73 changed files with 7539 additions and 175 deletions
@@ -0,0 +1,72 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
// Approved multi-order host adaptation, exclusively on disposable QA.
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-source schema requires disposable QA.');
}
$schema = $db->getSchemaBuilder();
if ($schema->hasTable('purchase_order_sources') || $schema->hasColumn('purchase_orders', 'order_key') || ! $schema->hasTable('budget_order_commands')) {
throw new LogicException('Install the optional financial schema first; do not reinstall or repair a partial host schema automatically.');
}
$db->transaction(function () use ($db, $schema): void {
$schema->table('purchase_orders', function (Blueprint $table): void {
$table->dropUnique(['requisition_id']);
$table->dropUnique(['purchase_award_id']);
$table->uuid('order_key')->nullable();
$table->string('order_payload_hash', 64)->nullable();
$table->unsignedInteger('order_source_count')->nullable();
$table->unique(['tenant_id', 'order_key']);
});
$schema->table('purchase_order_items', function (Blueprint $table): void {
$table->foreignId('quotation_item_id')->nullable()->constrained()->restrictOnDelete();
$table->unsignedBigInteger('quantity_offset_milli')->nullable();
});
$schema->create('purchase_order_sources', function (Blueprint $table): void {
$table->id();
$table->foreignId('tenant_id')->constrained()->restrictOnDelete();
$table->foreignId('purchase_order_id')->constrained()->restrictOnDelete();
$table->foreignId('requisition_id')->constrained()->restrictOnDelete();
$table->foreignId('purchase_award_id')->constrained()->restrictOnDelete();
$table->foreignId('quotation_id')->constrained()->restrictOnDelete();
$table->json('snapshot');
$table->unique(['purchase_order_id', 'requisition_id']);
});
if ($db->getDriverName() === 'pgsql') {
$db->statement('CREATE TRIGGER distribution_history_guard BEFORE UPDATE OR DELETE ON purchase_order_sources FOR EACH ROW EXECUTE FUNCTION sgc_distribution_history_guard()');
$db->unprepared(<<<'SQL'
CREATE FUNCTION sgc_order_parts_header_guard() RETURNS trigger LANGUAGE plpgsql AS $$
BEGIN
IF OLD.order_key IS NOT NULL THEN
IF TG_OP = 'DELETE' THEN RAISE EXCEPTION 'Preserve partial order history'; END IF;
IF (to_jsonb(NEW) - 'number' - 'status') IS DISTINCT FROM (to_jsonb(OLD) - 'number' - 'status') THEN
RAISE EXCEPTION 'Preserve the frozen partial order';
END IF;
ELSIF TG_OP = 'UPDATE' AND NEW.order_key IS NOT NULL THEN
RAISE EXCEPTION 'Do not relabel an issued legacy order';
END IF;
IF TG_OP = 'DELETE' THEN RETURN OLD; END IF;
RETURN NEW;
END $$;
CREATE TRIGGER order_parts_header_guard BEFORE UPDATE OR DELETE ON purchase_orders FOR EACH ROW EXECUTE FUNCTION sgc_order_parts_header_guard();
CREATE TRIGGER order_parts_line_guard BEFORE UPDATE OR DELETE ON purchase_order_items FOR EACH ROW WHEN (OLD.quotation_item_id IS NOT NULL) EXECUTE FUNCTION sgc_distribution_history_guard();
SQL);
}
});
}
public function down(): void
{
throw new LogicException('Use verified recovery; never discard order lineage or restore single-order uniqueness over issued documents.');
}
};
@@ -0,0 +1,72 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
// Explicit isolated QA only; not a default or deployment migration.
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('Landed cost effects require a guarded disposable QA database.');
}
$schema = $db->getSchemaBuilder();
$tables = ['budget_landed_commands', 'budget_landed_entries'];
if ($schema->hasTable($tables[0]) || $schema->hasTable($tables[1])) {
throw new LogicException('Never reinstall or repair landed-cost financial history implicitly.');
}
$db->transaction(function () use ($schema, $db, $tables): void {
$schema->create('budget_landed_commands', function (Blueprint $table): void {
$table->id();
$table->foreignId('tenant_id')->constrained()->restrictOnDelete();
$table->foreignId('created_by')->constrained('users')->restrictOnDelete();
$table->uuid('operation_key');
$table->uuid('plan_key');
$table->unsignedInteger('plan_version');
$table->string('plan_hash', 64);
$table->string('kind', 20);
$table->string('payload_hash', 64);
$table->json('snapshot');
$table->timestamp('created_at');
$table->unique(['tenant_id', 'operation_key']);
$table->index(['tenant_id', 'plan_key', 'id']);
});
$schema->create('budget_landed_entries', function (Blueprint $table): void {
$table->id();
$table->foreignId('tenant_id')->constrained()->restrictOnDelete();
$table->foreignId('command_id')->constrained('budget_landed_commands')->restrictOnDelete();
$table->foreignId('budget_id')->constrained('budgets')->restrictOnDelete();
$table->string('source_key', 120);
$table->string('source_kind', 30);
$table->unsignedBigInteger('source_id');
$table->unsignedInteger('source_version');
$table->string('source_revision', 64);
$table->json('source_snapshot');
$table->bigInteger('reserved_minor');
$table->bigInteger('committed_minor');
$table->bigInteger('consumed_minor');
$table->unique(['command_id', 'source_key']);
$table->index(['tenant_id', 'source_key']);
});
foreach ($tables as $table) {
if ($db->getDriverName() === 'pgsql') {
$db->unprepared('CREATE FUNCTION preserve_'.$table.'() RETURNS trigger LANGUAGE plpgsql AS $$ BEGIN RAISE EXCEPTION \'Preserve landed cost history\'; END $$;');
$db->statement('CREATE TRIGGER immutable_landed_history BEFORE UPDATE OR DELETE ON '.$table.' FOR EACH ROW EXECUTE FUNCTION preserve_'.$table.'()');
} else {
$db->unprepared('CREATE TRIGGER '.$table.'_update BEFORE UPDATE ON '.$table." BEGIN SELECT RAISE(ABORT, 'Preserve landed cost history'); END;");
$db->unprepared('CREATE TRIGGER '.$table.'_delete BEFORE DELETE ON '.$table." BEGIN SELECT RAISE(ABORT, 'Preserve landed cost history'); END;");
}
}
});
}
public function down(): void
{
throw new LogicException('Recover from a verified pre-extension backup; never delete financial history.');
}
};