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

51 lines
2.8 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_evaluations', function (Blueprint $table) {
$table->id();
$table->foreignId('tenant_id')->constrained()->restrictOnDelete();
$table->foreignId('supplier_id')->constrained()->restrictOnDelete();
$table->foreignId('department_id')->nullable()->constrained()->restrictOnDelete();
$table->foreignId('created_by')->constrained('users')->restrictOnDelete();
$table->foreignId('submitted_by')->nullable()->constrained('users')->restrictOnDelete();
$table->foreignId('reviewed_by')->nullable()->constrained('users')->restrictOnDelete();
$table->foreignId('previous_id')->nullable()->unique()->constrained('supplier_evaluations')->restrictOnDelete();
$table->uuid('operation_key');
$table->string('creation_hash', 64);
$table->unsignedInteger('revision')->default(1);
$table->unsignedInteger('version')->default(1);
$table->string('status')->default('draft');
$table->json('contributors');
$table->json('supplier_snapshot');
$table->json('payload');
$table->text('review_reason')->nullable();
$table->timestamp('reviewed_at')->nullable();
$table->timestamps();
$table->unique(['tenant_id', 'operation_key']);
$table->index(['tenant_id', 'supplier_id', 'status']);
});
if (DB::getDriverName() === 'pgsql') {
DB::unprepared("CREATE OR REPLACE FUNCTION sgc_preserve_supplier_evaluation() RETURNS trigger LANGUAGE plpgsql AS $$ BEGIN
IF TG_OP = 'DELETE' OR OLD.status = 'approved' THEN RAISE EXCEPTION 'Preservar avaliação aprovada e histórico'; END IF;
RETURN NEW; END $$;
CREATE TRIGGER preserve_supplier_evaluation BEFORE UPDATE OR DELETE ON supplier_evaluations FOR EACH ROW EXECUTE FUNCTION sgc_preserve_supplier_evaluation();");
} elseif (DB::getDriverName() === 'sqlite') {
DB::unprepared("CREATE TRIGGER preserve_supplier_evaluation_delete BEFORE DELETE ON supplier_evaluations BEGIN SELECT RAISE(ABORT, 'Preservar histórico de avaliação'); END;");
DB::unprepared("CREATE TRIGGER preserve_supplier_evaluation_approved BEFORE UPDATE ON supplier_evaluations WHEN OLD.status = 'approved' BEGIN SELECT RAISE(ABORT, 'Preservar avaliação aprovada'); END;");
}
}
public function down(): void
{
throw new LogicException('Preservar avaliações, decisões e histórico.');
}
};