51 lines
2.6 KiB
PHP
51 lines
2.6 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_improvements', 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->foreignId('responsible_id')->constrained('users')->restrictOnDelete();
|
|
$table->uuid('operation_key');
|
|
$table->string('creation_hash', 64);
|
|
$table->string('title', 200);
|
|
$table->text('description');
|
|
$table->date('due_on');
|
|
$table->unsignedInteger('version')->default(1);
|
|
$table->string('status')->default('open');
|
|
$table->json('contributors');
|
|
$table->text('result')->nullable();
|
|
$table->text('evidence')->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_improvement() RETURNS trigger LANGUAGE plpgsql AS $$ BEGIN
|
|
IF TG_OP = 'DELETE' OR OLD.status = 'closed' THEN RAISE EXCEPTION 'Preservar medida encerrada e histórico'; END IF;
|
|
RETURN NEW; END $$;
|
|
CREATE TRIGGER preserve_supplier_improvement BEFORE UPDATE OR DELETE ON supplier_improvements FOR EACH ROW EXECUTE FUNCTION sgc_preserve_supplier_improvement();");
|
|
} elseif (DB::getDriverName() === 'sqlite') {
|
|
DB::unprepared("CREATE TRIGGER preserve_supplier_improvement_delete BEFORE DELETE ON supplier_improvements BEGIN SELECT RAISE(ABORT, 'Preservar histórico da medida'); END;");
|
|
DB::unprepared("CREATE TRIGGER preserve_supplier_improvement_closed BEFORE UPDATE ON supplier_improvements WHEN OLD.status = 'closed' BEGIN SELECT RAISE(ABORT, 'Preservar medida encerrada'); END;");
|
|
}
|
|
}
|
|
|
|
public function down(): void
|
|
{
|
|
throw new LogicException('Preservar medidas e decisões.');
|
|
}
|
|
};
|