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.'); } };