id(); $table->foreignId('tenant_id')->constrained()->restrictOnDelete(); $table->foreignId('people_process_id')->constrained()->restrictOnDelete(); $table->foreignId('people_application_id')->constrained()->restrictOnDelete(); $table->foreignId('people_review_id')->constrained()->restrictOnDelete(); $table->unsignedInteger('criterion_index'); $table->text('question'); $table->foreignId('raised_by')->constrained('users')->restrictOnDelete(); $table->foreignId('responsible_id')->constrained('users')->restrictOnDelete(); $table->date('due_on'); $table->uuid('operation_key'); $table->string('creation_hash', 64); $table->string('status')->default('open'); $table->unsignedInteger('version')->default(1); $table->json('responses'); $table->json('decisions'); $table->timestamps(); $table->unique(['people_review_id', 'criterion_index']); $table->unique(['tenant_id', 'operation_key']); }); if (DB::getDriverName() === 'pgsql') { DB::unprepared("CREATE FUNCTION sgc_preserve_people_clarification() RETURNS trigger LANGUAGE plpgsql AS $$ BEGIN IF TG_OP = 'DELETE' OR OLD.status = 'resolved' THEN RAISE EXCEPTION 'Preservar esclarecimento e conclusao'; END IF; IF NEW.version <> OLD.version + 1 OR ROW(NEW.tenant_id, NEW.people_process_id, NEW.people_application_id, NEW.people_review_id, NEW.criterion_index, NEW.question, NEW.raised_by, NEW.operation_key, NEW.creation_hash) IS DISTINCT FROM ROW(OLD.tenant_id, OLD.people_process_id, OLD.people_application_id, OLD.people_review_id, OLD.criterion_index, OLD.question, OLD.raised_by, OLD.operation_key, OLD.creation_hash) THEN RAISE EXCEPTION 'Preservar origem e versao do esclarecimento'; END IF; RETURN NEW; END $$; CREATE TRIGGER preserve_people_clarification BEFORE UPDATE OR DELETE ON people_clarifications FOR EACH ROW EXECUTE FUNCTION sgc_preserve_people_clarification();"); } elseif (DB::getDriverName() === 'sqlite') { DB::unprepared("CREATE TRIGGER preserve_people_clarification_delete BEFORE DELETE ON people_clarifications BEGIN SELECT RAISE(ABORT, 'Preservar esclarecimento'); END;"); DB::unprepared("CREATE TRIGGER preserve_people_clarification_update BEFORE UPDATE ON people_clarifications WHEN OLD.status = 'resolved' OR NEW.version <> OLD.version + 1 OR NEW.tenant_id IS NOT OLD.tenant_id OR NEW.people_process_id IS NOT OLD.people_process_id OR NEW.people_application_id IS NOT OLD.people_application_id OR NEW.people_review_id IS NOT OLD.people_review_id OR NEW.criterion_index IS NOT OLD.criterion_index OR NEW.question IS NOT OLD.question OR NEW.raised_by IS NOT OLD.raised_by OR NEW.operation_key IS NOT OLD.operation_key OR NEW.creation_hash IS NOT OLD.creation_hash BEGIN SELECT RAISE(ABORT, 'Preservar origem e conclusao'); END;"); } } public function down(): void { throw new LogicException('Preservar esclarecimentos e respostas.'); } };