diff --git a/prisma/migrations/20260215200000_phase7_fk_renames/migration.sql b/prisma/migrations/20260215200000_phase7_fk_renames/migration.sql index be6eb12..fddf65b 100644 --- a/prisma/migrations/20260215200000_phase7_fk_renames/migration.sql +++ b/prisma/migrations/20260215200000_phase7_fk_renames/migration.sql @@ -3,10 +3,17 @@ -- ============================================================================= -- This migration renames stageId columns to roundId and updates FK constraints -- to point to the Round table instead of Stage table. +-- +-- NOTE: After the pipeline migration (20260213), most tables have BOTH a +-- nullable roundId column (legacy, no FK) AND a stageId column. We must +-- drop the old roundId column before renaming stageId → roundId. -- ─── 1. EvaluationForm ─────────────────────────────────────────────────────── --- Drop FK constraint +-- Drop old roundId column (nullable, no FK since 20260213 migration) +ALTER TABLE "EvaluationForm" DROP COLUMN IF EXISTS "roundId"; + +-- Drop FK constraint on stageId ALTER TABLE "EvaluationForm" DROP CONSTRAINT IF EXISTS "EvaluationForm_stageId_fkey"; -- Drop indexes @@ -26,275 +33,239 @@ ALTER TABLE "EvaluationForm" ADD CONSTRAINT "EvaluationForm_roundId_fkey" -- ─── 2. FileRequirement ────────────────────────────────────────────────────── --- Drop FK constraint +ALTER TABLE "FileRequirement" DROP COLUMN IF EXISTS "roundId"; + ALTER TABLE "FileRequirement" DROP CONSTRAINT IF EXISTS "FileRequirement_stageId_fkey"; --- Drop index DROP INDEX IF EXISTS "FileRequirement_stageId_idx"; --- Rename column ALTER TABLE "FileRequirement" RENAME COLUMN "stageId" TO "roundId"; --- Recreate index CREATE INDEX "FileRequirement_roundId_idx" ON "FileRequirement"("roundId"); --- Recreate FK pointing to Round ALTER TABLE "FileRequirement" ADD CONSTRAINT "FileRequirement_roundId_fkey" FOREIGN KEY ("roundId") REFERENCES "Round"("id") ON DELETE CASCADE ON UPDATE CASCADE; -- ─── 3. Assignment ─────────────────────────────────────────────────────────── --- Drop FK constraint +ALTER TABLE "Assignment" DROP COLUMN IF EXISTS "roundId"; + ALTER TABLE "Assignment" DROP CONSTRAINT IF EXISTS "Assignment_stageId_fkey"; --- Drop indexes and unique constraint DROP INDEX IF EXISTS "Assignment_userId_projectId_stageId_key"; DROP INDEX IF EXISTS "Assignment_stageId_idx"; --- Rename column ALTER TABLE "Assignment" RENAME COLUMN "stageId" TO "roundId"; --- Recreate unique constraint and index with new name CREATE UNIQUE INDEX "Assignment_userId_projectId_roundId_key" ON "Assignment"("userId", "projectId", "roundId"); CREATE INDEX "Assignment_roundId_idx" ON "Assignment"("roundId"); --- Recreate FK pointing to Round ALTER TABLE "Assignment" ADD CONSTRAINT "Assignment_roundId_fkey" FOREIGN KEY ("roundId") REFERENCES "Round"("id") ON DELETE CASCADE ON UPDATE CASCADE; -- ─── 4. GracePeriod ────────────────────────────────────────────────────────── --- Drop FK constraint +ALTER TABLE "GracePeriod" DROP COLUMN IF EXISTS "roundId"; + ALTER TABLE "GracePeriod" DROP CONSTRAINT IF EXISTS "GracePeriod_stageId_fkey"; --- Drop indexes DROP INDEX IF EXISTS "GracePeriod_stageId_idx"; DROP INDEX IF EXISTS "GracePeriod_stageId_userId_extendedUntil_idx"; --- Rename column ALTER TABLE "GracePeriod" RENAME COLUMN "stageId" TO "roundId"; --- Recreate indexes CREATE INDEX "GracePeriod_roundId_idx" ON "GracePeriod"("roundId"); CREATE INDEX "GracePeriod_roundId_userId_extendedUntil_idx" ON "GracePeriod"("roundId", "userId", "extendedUntil"); --- Recreate FK pointing to Round ALTER TABLE "GracePeriod" ADD CONSTRAINT "GracePeriod_roundId_fkey" FOREIGN KEY ("roundId") REFERENCES "Round"("id") ON DELETE CASCADE ON UPDATE CASCADE; -- ─── 5. LiveVotingSession ──────────────────────────────────────────────────── --- Drop FK constraint +ALTER TABLE "LiveVotingSession" DROP COLUMN IF EXISTS "roundId"; + ALTER TABLE "LiveVotingSession" DROP CONSTRAINT IF EXISTS "LiveVotingSession_stageId_fkey"; --- Drop unique index DROP INDEX IF EXISTS "LiveVotingSession_stageId_key"; --- Rename column ALTER TABLE "LiveVotingSession" RENAME COLUMN "stageId" TO "roundId"; --- Recreate unique index CREATE UNIQUE INDEX "LiveVotingSession_roundId_key" ON "LiveVotingSession"("roundId"); --- Recreate FK pointing to Round (nullable) ALTER TABLE "LiveVotingSession" ADD CONSTRAINT "LiveVotingSession_roundId_fkey" FOREIGN KEY ("roundId") REFERENCES "Round"("id") ON DELETE CASCADE ON UPDATE CASCADE; -- ─── 6. FilteringRule ──────────────────────────────────────────────────────── --- Drop FK constraint +ALTER TABLE "FilteringRule" DROP COLUMN IF EXISTS "roundId"; + ALTER TABLE "FilteringRule" DROP CONSTRAINT IF EXISTS "FilteringRule_stageId_fkey"; --- Drop index DROP INDEX IF EXISTS "FilteringRule_stageId_idx"; --- Rename column ALTER TABLE "FilteringRule" RENAME COLUMN "stageId" TO "roundId"; --- Recreate index CREATE INDEX "FilteringRule_roundId_idx" ON "FilteringRule"("roundId"); --- Recreate FK pointing to Round ALTER TABLE "FilteringRule" ADD CONSTRAINT "FilteringRule_roundId_fkey" FOREIGN KEY ("roundId") REFERENCES "Round"("id") ON DELETE CASCADE ON UPDATE CASCADE; -- ─── 7. FilteringResult ────────────────────────────────────────────────────── --- Drop FK constraint +ALTER TABLE "FilteringResult" DROP COLUMN IF EXISTS "roundId"; + ALTER TABLE "FilteringResult" DROP CONSTRAINT IF EXISTS "FilteringResult_stageId_fkey"; --- Drop indexes and unique constraint DROP INDEX IF EXISTS "FilteringResult_stageId_projectId_key"; DROP INDEX IF EXISTS "FilteringResult_stageId_idx"; --- Rename column ALTER TABLE "FilteringResult" RENAME COLUMN "stageId" TO "roundId"; --- Recreate unique constraint and index CREATE UNIQUE INDEX "FilteringResult_roundId_projectId_key" ON "FilteringResult"("roundId", "projectId"); CREATE INDEX "FilteringResult_roundId_idx" ON "FilteringResult"("roundId"); --- Recreate FK pointing to Round ALTER TABLE "FilteringResult" ADD CONSTRAINT "FilteringResult_roundId_fkey" FOREIGN KEY ("roundId") REFERENCES "Round"("id") ON DELETE CASCADE ON UPDATE CASCADE; -- ─── 8. FilteringJob ───────────────────────────────────────────────────────── --- Drop FK constraint +ALTER TABLE "FilteringJob" DROP COLUMN IF EXISTS "roundId"; + ALTER TABLE "FilteringJob" DROP CONSTRAINT IF EXISTS "FilteringJob_stageId_fkey"; --- Drop index DROP INDEX IF EXISTS "FilteringJob_stageId_idx"; --- Rename column ALTER TABLE "FilteringJob" RENAME COLUMN "stageId" TO "roundId"; --- Recreate index CREATE INDEX "FilteringJob_roundId_idx" ON "FilteringJob"("roundId"); --- Recreate FK pointing to Round ALTER TABLE "FilteringJob" ADD CONSTRAINT "FilteringJob_roundId_fkey" FOREIGN KEY ("roundId") REFERENCES "Round"("id") ON DELETE CASCADE ON UPDATE CASCADE; -- ─── 9. AssignmentJob ──────────────────────────────────────────────────────── --- Drop FK constraint +ALTER TABLE "AssignmentJob" DROP COLUMN IF EXISTS "roundId"; + ALTER TABLE "AssignmentJob" DROP CONSTRAINT IF EXISTS "AssignmentJob_stageId_fkey"; --- Drop index DROP INDEX IF EXISTS "AssignmentJob_stageId_idx"; --- Rename column ALTER TABLE "AssignmentJob" RENAME COLUMN "stageId" TO "roundId"; --- Recreate index CREATE INDEX "AssignmentJob_roundId_idx" ON "AssignmentJob"("roundId"); --- Recreate FK pointing to Round ALTER TABLE "AssignmentJob" ADD CONSTRAINT "AssignmentJob_roundId_fkey" FOREIGN KEY ("roundId") REFERENCES "Round"("id") ON DELETE CASCADE ON UPDATE CASCADE; -- ─── 10. ReminderLog ───────────────────────────────────────────────────────── --- Drop FK constraint +ALTER TABLE "ReminderLog" DROP COLUMN IF EXISTS "roundId"; + ALTER TABLE "ReminderLog" DROP CONSTRAINT IF EXISTS "ReminderLog_stageId_fkey"; --- Drop indexes and unique constraint DROP INDEX IF EXISTS "ReminderLog_stageId_userId_type_key"; DROP INDEX IF EXISTS "ReminderLog_stageId_idx"; --- Rename column ALTER TABLE "ReminderLog" RENAME COLUMN "stageId" TO "roundId"; --- Recreate unique constraint and index CREATE UNIQUE INDEX "ReminderLog_roundId_userId_type_key" ON "ReminderLog"("roundId", "userId", "type"); CREATE INDEX "ReminderLog_roundId_idx" ON "ReminderLog"("roundId"); --- Recreate FK pointing to Round ALTER TABLE "ReminderLog" ADD CONSTRAINT "ReminderLog_roundId_fkey" FOREIGN KEY ("roundId") REFERENCES "Round"("id") ON DELETE CASCADE ON UPDATE CASCADE; -- ─── 11. EvaluationSummary ─────────────────────────────────────────────────── --- Drop FK constraint +ALTER TABLE "EvaluationSummary" DROP COLUMN IF EXISTS "roundId"; + ALTER TABLE "EvaluationSummary" DROP CONSTRAINT IF EXISTS "EvaluationSummary_stageId_fkey"; --- Drop indexes and unique constraint DROP INDEX IF EXISTS "EvaluationSummary_projectId_stageId_key"; DROP INDEX IF EXISTS "EvaluationSummary_stageId_idx"; --- Rename column ALTER TABLE "EvaluationSummary" RENAME COLUMN "stageId" TO "roundId"; --- Recreate unique constraint and index CREATE UNIQUE INDEX "EvaluationSummary_projectId_roundId_key" ON "EvaluationSummary"("projectId", "roundId"); CREATE INDEX "EvaluationSummary_roundId_idx" ON "EvaluationSummary"("roundId"); --- Recreate FK pointing to Round ALTER TABLE "EvaluationSummary" ADD CONSTRAINT "EvaluationSummary_roundId_fkey" FOREIGN KEY ("roundId") REFERENCES "Round"("id") ON DELETE CASCADE ON UPDATE CASCADE; -- ─── 12. EvaluationDiscussion ──────────────────────────────────────────────── --- Drop FK constraint +ALTER TABLE "EvaluationDiscussion" DROP COLUMN IF EXISTS "roundId"; + ALTER TABLE "EvaluationDiscussion" DROP CONSTRAINT IF EXISTS "EvaluationDiscussion_stageId_fkey"; --- Drop indexes and unique constraint DROP INDEX IF EXISTS "EvaluationDiscussion_projectId_stageId_key"; DROP INDEX IF EXISTS "EvaluationDiscussion_stageId_idx"; --- Rename column ALTER TABLE "EvaluationDiscussion" RENAME COLUMN "stageId" TO "roundId"; --- Recreate unique constraint and index CREATE UNIQUE INDEX "EvaluationDiscussion_projectId_roundId_key" ON "EvaluationDiscussion"("projectId", "roundId"); CREATE INDEX "EvaluationDiscussion_roundId_idx" ON "EvaluationDiscussion"("roundId"); --- Recreate FK pointing to Round ALTER TABLE "EvaluationDiscussion" ADD CONSTRAINT "EvaluationDiscussion_roundId_fkey" FOREIGN KEY ("roundId") REFERENCES "Round"("id") ON DELETE CASCADE ON UPDATE CASCADE; -- ─── 13. Message ───────────────────────────────────────────────────────────── --- Drop FK constraint +-- Message has roundId (from init, nullable) and stageId (from pipeline, nullable) +ALTER TABLE "Message" DROP COLUMN IF EXISTS "roundId"; + ALTER TABLE "Message" DROP CONSTRAINT IF EXISTS "Message_stageId_fkey"; --- Drop index DROP INDEX IF EXISTS "Message_stageId_idx"; --- Rename column (nullable, so SET NULL on delete) ALTER TABLE "Message" RENAME COLUMN "stageId" TO "roundId"; --- Recreate index CREATE INDEX "Message_roundId_idx" ON "Message"("roundId"); --- Recreate FK pointing to Round with SET NULL ALTER TABLE "Message" ADD CONSTRAINT "Message_roundId_fkey" FOREIGN KEY ("roundId") REFERENCES "Round"("id") ON DELETE SET NULL ON UPDATE CASCADE; -- ─── 14. Cohort ────────────────────────────────────────────────────────────── +-- Cohort was created in pipeline migration with stageId only (no roundId) --- Drop FK constraint ALTER TABLE "Cohort" DROP CONSTRAINT IF EXISTS "Cohort_stageId_fkey"; --- Drop indexes DROP INDEX IF EXISTS "Cohort_stageId_idx"; --- Rename column ALTER TABLE "Cohort" RENAME COLUMN "stageId" TO "roundId"; --- Recreate index CREATE INDEX "Cohort_roundId_idx" ON "Cohort"("roundId"); --- Recreate FK pointing to Round ALTER TABLE "Cohort" ADD CONSTRAINT "Cohort_roundId_fkey" FOREIGN KEY ("roundId") REFERENCES "Round"("id") ON DELETE CASCADE ON UPDATE CASCADE; -- ─── 15. LiveProgressCursor ────────────────────────────────────────────────── +-- LiveProgressCursor was created in pipeline migration with stageId only (no roundId) --- Drop FK constraint ALTER TABLE "LiveProgressCursor" DROP CONSTRAINT IF EXISTS "LiveProgressCursor_stageId_fkey"; --- Drop unique index DROP INDEX IF EXISTS "LiveProgressCursor_stageId_key"; --- Rename column ALTER TABLE "LiveProgressCursor" RENAME COLUMN "stageId" TO "roundId"; --- Recreate unique index CREATE UNIQUE INDEX "LiveProgressCursor_roundId_key" ON "LiveProgressCursor"("roundId"); --- Recreate FK pointing to Round ALTER TABLE "LiveProgressCursor" ADD CONSTRAINT "LiveProgressCursor_roundId_fkey" FOREIGN KEY ("roundId") REFERENCES "Round"("id") ON DELETE CASCADE ON UPDATE CASCADE; -- ─── 16. SpecialAward: Drop trackId column ─────────────────────────────────── --- Drop FK constraint ALTER TABLE "SpecialAward" DROP CONSTRAINT IF EXISTS "SpecialAward_trackId_fkey"; --- Drop unique index DROP INDEX IF EXISTS "SpecialAward_trackId_key"; --- Drop column ALTER TABLE "SpecialAward" DROP COLUMN IF EXISTS "trackId"; + +-- ─── 17. ConflictOfInterest: roundId was made nullable in pipeline migration ─ +-- It still exists, just restore FK to new Round table +ALTER TABLE "ConflictOfInterest" ADD CONSTRAINT "ConflictOfInterest_roundId_fkey" + FOREIGN KEY ("roundId") REFERENCES "Round"("id") ON DELETE CASCADE ON UPDATE CASCADE; + +-- ─── 18. TaggingJob: roundId was made nullable in pipeline migration ───────── +-- Restore FK to new Round table +ALTER TABLE "TaggingJob" ADD CONSTRAINT "TaggingJob_roundId_fkey" + FOREIGN KEY ("roundId") REFERENCES "Round"("id") ON DELETE SET NULL ON UPDATE CASCADE;