-- ============================================================================= -- Phase 7/8 Migration Part 1: Rename stageId → roundId on 15 tables -- ============================================================================= -- This migration renames stageId columns to roundId and updates FK constraints -- to point to the Round table instead of Stage table. -- ─── 1. EvaluationForm ─────────────────────────────────────────────────────── -- Drop FK constraint ALTER TABLE "EvaluationForm" DROP CONSTRAINT IF EXISTS "EvaluationForm_stageId_fkey"; -- Drop indexes DROP INDEX IF EXISTS "EvaluationForm_stageId_version_key"; DROP INDEX IF EXISTS "EvaluationForm_stageId_isActive_idx"; -- Rename column ALTER TABLE "EvaluationForm" RENAME COLUMN "stageId" TO "roundId"; -- Recreate indexes with new name CREATE UNIQUE INDEX "EvaluationForm_roundId_version_key" ON "EvaluationForm"("roundId", "version"); CREATE INDEX "EvaluationForm_roundId_isActive_idx" ON "EvaluationForm"("roundId", "isActive"); -- Recreate FK pointing to Round ALTER TABLE "EvaluationForm" ADD CONSTRAINT "EvaluationForm_roundId_fkey" FOREIGN KEY ("roundId") REFERENCES "Round"("id") ON DELETE CASCADE ON UPDATE CASCADE; -- ─── 2. FileRequirement ────────────────────────────────────────────────────── -- Drop FK constraint 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 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 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 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 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 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 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 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 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 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 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 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 ────────────────────────────────────────────────────────────── -- 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 ────────────────────────────────────────────────── -- 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";