commit compras1 3011
tests / ci (push) Canceled after 0s

This commit is contained in:
2026-11-30 09:33:36 +02:00
parent 96b30368bc
commit 47649bd46c
163 changed files with 9419 additions and 145 deletions
@@ -10,6 +10,7 @@ return new class extends Migration
public function up(): void
{
$this->guardSqlite();
$triggers = $this->sqliteTriggers();
Schema::table('requisitions', function (Blueprint $table) {
$table->foreignId('cost_center_id')->nullable()->change();
$table->date('needed_by')->nullable()->change();
@@ -19,6 +20,7 @@ return new class extends Migration
$table->unsignedBigInteger('total_minor')->nullable()->change();
$table->json('draft_input')->nullable();
});
$this->restoreSqliteTriggers($triggers);
if (DB::getDriverName() === 'pgsql') {
DB::statement("ALTER TABLE requisitions ADD CONSTRAINT requisitions_complete_before_submission CHECK (status IN ('draft', 'cancelled') OR (cost_center_id IS NOT NULL AND needed_by IS NOT NULL AND justification IS NOT NULL AND subtotal_minor IS NOT NULL AND tax_minor IS NOT NULL AND total_minor IS NOT NULL AND draft_input IS NULL))");
}
@@ -30,6 +32,7 @@ return new class extends Migration
throw new LogicException('Complete ou preserve os rascunhos antes de reverter a estrutura; não substituir dados em falta por valores fictícios.');
}
$this->guardSqlite();
$triggers = $this->sqliteTriggers();
if (DB::getDriverName() === 'pgsql') {
DB::statement('ALTER TABLE requisitions DROP CONSTRAINT requisitions_complete_before_submission');
}
@@ -42,6 +45,25 @@ return new class extends Migration
$table->unsignedBigInteger('total_minor')->nullable(false)->change();
$table->dropColumn('draft_input');
});
$this->restoreSqliteTriggers($triggers);
}
/** @return array<string, string> */
private function sqliteTriggers(): array
{
return DB::getDriverName() === 'sqlite'
? DB::table('sqlite_master')->where('type', 'trigger')->where('tbl_name', 'requisitions')->pluck('sql', 'name')->all()
: [];
}
/** @param array<string, string> $triggers */
private function restoreSqliteTriggers(array $triggers): void
{
foreach ($triggers as $name => $sql) {
if (! DB::table('sqlite_master')->where('type', 'trigger')->where('name', $name)->exists()) {
DB::unprepared($sql);
}
}
}
private function guardSqlite(): void
@@ -0,0 +1,52 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Facades\DB;
return new class extends Migration
{
public function up(): void
{
$added = ['status', 'signed_on', 'signed_reference', 'formalized_at', 'formalized_by', 'formalization_reason',
'formalization_operation_key', 'formalization_hash', 'signed_document'];
if (DB::getDriverName() === 'pgsql') {
$keys = "ARRAY['".implode("','", $added)."']";
DB::unprepared("CREATE OR REPLACE FUNCTION sgc_preserve_people_process() RETURNS trigger LANGUAGE plpgsql AS \$\$ BEGIN
IF TG_OP = 'DELETE' OR OLD.status = 'contract_formalized' THEN RAISE EXCEPTION 'Preservar processo e contrato'; END IF;
IF OLD.status = 'contract_draft' AND (
NEW.status <> 'contract_formalized' OR NEW.version <> OLD.version + 1
OR (to_jsonb(NEW) - ARRAY['status','version','contract','updated_at']) IS DISTINCT FROM (to_jsonb(OLD) - ARRAY['status','version','contract','updated_at'])
OR (NEW.contract::jsonb - {$keys}) IS DISTINCT FROM (OLD.contract::jsonb - 'status')
OR NEW.contract::jsonb->>'status' IS DISTINCT FROM 'formalized'
OR NEW.contract::jsonb->>'formalized_by' IS NULL
OR NEW.contract::jsonb->'signed_document'->>'sha256' IS NULL
) THEN RAISE EXCEPTION 'Preservar minuta e seleccao'; END IF;
IF ROW(NEW.tenant_id,NEW.created_by,NEW.kind,NEW.title,NEW.purpose,NEW.closes_on,NEW.rules::text,NEW.operation_key,NEW.creation_hash)
IS DISTINCT FROM ROW(OLD.tenant_id,OLD.created_by,OLD.kind,OLD.title,OLD.purpose,OLD.closes_on,OLD.rules::text,OLD.operation_key,OLD.creation_hash)
THEN RAISE EXCEPTION 'Preservar regras e ambito do processo'; END IF;
RETURN NEW; END \$\$;");
} elseif (DB::getDriverName() === 'sqlite') {
$paths = "'$.".implode("','$.", $added)."'";
DB::unprepared('DROP TRIGGER preserve_people_process_update');
DB::unprepared("CREATE TRIGGER preserve_people_process_update BEFORE UPDATE ON people_processes WHEN
OLD.status = 'contract_formalized'
OR (OLD.status = 'contract_draft' AND (
NEW.status <> 'contract_formalized' OR NEW.version <> OLD.version + 1
OR NEW.opened_by IS NOT OLD.opened_by OR NEW.confirmed_by IS NOT OLD.confirmed_by
OR NEW.selected_application_id IS NOT OLD.selected_application_id OR NEW.selection IS NOT OLD.selection OR NEW.created_at IS NOT OLD.created_at
OR json_remove(NEW.contract,{$paths}) IS NOT json_remove(OLD.contract,'$.status')
OR json_extract(NEW.contract,'$.status') IS NOT 'formalized'
OR json_extract(NEW.contract,'$.formalized_by') IS NULL
OR json_extract(NEW.contract,'$.signed_document.sha256') IS NULL))
OR NEW.tenant_id IS NOT OLD.tenant_id OR NEW.created_by IS NOT OLD.created_by OR NEW.kind IS NOT OLD.kind OR NEW.title IS NOT OLD.title
OR NEW.purpose IS NOT OLD.purpose OR NEW.closes_on IS NOT OLD.closes_on OR NEW.rules IS NOT OLD.rules
OR NEW.operation_key IS NOT OLD.operation_key OR NEW.creation_hash IS NOT OLD.creation_hash
BEGIN SELECT RAISE(ABORT, 'Preservar regras e processo'); END;");
}
}
public function down(): void
{
throw new LogicException('Preservar contratos formalizados e evidências.');
}
};
@@ -0,0 +1,52 @@
<?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::create('business_clients', function (Blueprint $table) {
$table->id();
$table->foreignId('tenant_id')->constrained('tenants');
$table->uuid('client_key');
$table->string('code', 40);
$table->string('name', 180);
$table->string('tax_number', 40)->nullable();
$table->text('address')->nullable();
$table->string('email', 254)->nullable();
$table->boolean('active')->default(true);
$table->unsignedInteger('version')->default(1);
$table->foreignId('created_by')->constrained('users');
$table->string('creation_hash', 64);
$table->timestamps();
$table->unique(['tenant_id', 'client_key']);
$table->unique(['tenant_id', 'code']);
$table->unique(['id', 'tenant_id']);
});
if (DB::getDriverName() === 'pgsql') {
DB::unprepared("CREATE FUNCTION sgc_preserve_business_client() RETURNS trigger LANGUAGE plpgsql AS \$\$ BEGIN
IF TG_OP = 'DELETE' THEN RAISE EXCEPTION 'Preservar identidade do cliente'; END IF;
IF ROW(NEW.tenant_id,NEW.client_key,NEW.created_by,NEW.creation_hash,NEW.created_at)
IS DISTINCT FROM ROW(OLD.tenant_id,OLD.client_key,OLD.created_by,OLD.creation_hash,OLD.created_at)
OR NEW.version <> OLD.version + 1 THEN RAISE EXCEPTION 'Preservar identidade e versao do cliente'; END IF;
RETURN NEW; END \$\$;
CREATE TRIGGER sgc_preserve_business_client BEFORE UPDATE OR DELETE ON business_clients FOR EACH ROW EXECUTE FUNCTION sgc_preserve_business_client();");
} elseif (DB::getDriverName() === 'sqlite') {
DB::unprepared("CREATE TRIGGER sgc_preserve_business_client_update BEFORE UPDATE ON business_clients WHEN
NEW.tenant_id IS NOT OLD.tenant_id OR NEW.client_key IS NOT OLD.client_key OR NEW.created_by IS NOT OLD.created_by
OR NEW.creation_hash IS NOT OLD.creation_hash OR NEW.created_at IS NOT OLD.created_at OR NEW.version <> OLD.version + 1
BEGIN SELECT RAISE(ABORT, 'Preservar identidade e versao do cliente'); END;
CREATE TRIGGER sgc_preserve_business_client_delete BEFORE DELETE ON business_clients
BEGIN SELECT RAISE(ABORT, 'Preservar identidade do cliente'); END;");
}
}
public function down(): void
{
throw new LogicException('Preservar clientes e identidades congeladas nos processos.');
}
};