Files
compras/database/migrations/2026_10_30_000026_create_supplier_responses.php
T
nala 48347e2d3a
tests / ci (push) Canceled after 0s
commit5
2026-10-30 10:55:18 +02:00

47 lines
2.3 KiB
PHP

<?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('supplier_responses', function (Blueprint $table) {
$table->id();
$table->foreignId('tenant_id')->constrained()->restrictOnDelete();
$table->foreignId('supplier_evaluation_id')->constrained()->restrictOnDelete();
$table->foreignId('created_by')->constrained('users')->restrictOnDelete();
$table->uuid('operation_key');
$table->string('creation_hash', 64);
$table->unsignedInteger('version')->default(1);
$table->string('status')->default('draft');
$table->json('payload');
$table->json('contributors');
$table->json('decision')->nullable();
$table->foreignId('reviewed_by')->nullable()->constrained('users')->restrictOnDelete();
$table->text('review_reason')->nullable();
$table->timestamp('reviewed_at')->nullable();
$table->timestamps();
$table->unique(['tenant_id', 'operation_key']);
$table->index(['supplier_evaluation_id', 'status']);
});
if (DB::getDriverName() === 'pgsql') {
DB::unprepared("CREATE FUNCTION sgc_preserve_supplier_response() RETURNS trigger LANGUAGE plpgsql AS $$ BEGIN
IF TG_OP = 'DELETE' OR OLD.status = 'decided' THEN RAISE EXCEPTION 'Preservar contraditorio e conclusao'; END IF;
RETURN NEW; END $$;
CREATE TRIGGER preserve_supplier_response BEFORE UPDATE OR DELETE ON supplier_responses FOR EACH ROW EXECUTE FUNCTION sgc_preserve_supplier_response();");
} elseif (DB::getDriverName() === 'sqlite') {
DB::unprepared("CREATE TRIGGER preserve_supplier_response_delete BEFORE DELETE ON supplier_responses BEGIN SELECT RAISE(ABORT, 'Preservar contraditorio'); END;");
DB::unprepared("CREATE TRIGGER preserve_supplier_response_decided BEFORE UPDATE ON supplier_responses WHEN OLD.status = 'decided' BEGIN SELECT RAISE(ABORT, 'Preservar conclusao'); END;");
}
}
public function down(): void
{
throw new LogicException('Preservar contraditórios e conclusões.');
}
};