fix: make evaluation form category migration fully idempotent
All checks were successful
Build and Push Docker Image / build (push) Successful in 9m59s

The migration partially applied (column added, then failed on DROP
CONSTRAINT). Make every statement idempotent with IF EXISTS / IF NOT
EXISTS so it can safely re-run after resolve --rolled-back.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Matt
2026-04-07 13:34:47 -04:00
parent ef2186e89a
commit d9f3b3d2a4

View File

@@ -1,18 +1,21 @@
-- AlterTable: add nullable category column to EvaluationForm -- AlterTable: add nullable category column to EvaluationForm
ALTER TABLE "EvaluationForm" ADD COLUMN "category" "CompetitionCategory"; ALTER TABLE "EvaluationForm" ADD COLUMN IF NOT EXISTS "category" "CompetitionCategory";
-- Drop old unique constraint (IF EXISTS — may not exist on fresh databases where -- Drop old unique constraint (IF EXISTS — may not exist on fresh databases where
-- the constraint was never created or was already removed by an earlier migration) -- the constraint was never created or was already removed by an earlier migration)
ALTER TABLE "EvaluationForm" DROP CONSTRAINT IF EXISTS "EvaluationForm_roundId_version_key"; ALTER TABLE "EvaluationForm" DROP CONSTRAINT IF EXISTS "EvaluationForm_roundId_version_key";
-- Add new unique constraint including category -- Add new unique constraint including category
ALTER TABLE "EvaluationForm" DROP CONSTRAINT IF EXISTS "EvaluationForm_roundId_version_category_key";
ALTER TABLE "EvaluationForm" ADD CONSTRAINT "EvaluationForm_roundId_version_category_key" UNIQUE ("roundId", "version", "category"); ALTER TABLE "EvaluationForm" ADD CONSTRAINT "EvaluationForm_roundId_version_category_key" UNIQUE ("roundId", "version", "category");
-- Partial unique index: prevent duplicate shared forms at the same version -- Partial unique index: prevent duplicate shared forms at the same version
-- (PostgreSQL treats NULLs as distinct in unique constraints, so we need this) -- (PostgreSQL treats NULLs as distinct in unique constraints, so we need this)
DROP INDEX IF EXISTS "EvaluationForm_roundId_version_null_category";
CREATE UNIQUE INDEX "EvaluationForm_roundId_version_null_category" CREATE UNIQUE INDEX "EvaluationForm_roundId_version_null_category"
ON "EvaluationForm" ("roundId", "version") WHERE "category" IS NULL; ON "EvaluationForm" ("roundId", "version") WHERE "category" IS NULL;
-- Compound index for category-aware active form lookups -- Compound index for category-aware active form lookups
DROP INDEX IF EXISTS "EvaluationForm_roundId_isActive_category_idx";
CREATE INDEX "EvaluationForm_roundId_isActive_category_idx" CREATE INDEX "EvaluationForm_roundId_isActive_category_idx"
ON "EvaluationForm" ("roundId", "isActive", "category"); ON "EvaluationForm" ("roundId", "isActive", "category");