Make all migration SQL files idempotent for clean prod deploys
All checks were successful
Build and Push Docker Image / build (push) Successful in 8m14s

Added IF NOT EXISTS, IF EXISTS, and DO $$ EXCEPTION guards to all
migration files from 20260205 onwards so they survive partial application
and work correctly on both fresh databases and existing deployments.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Matt
2026-02-16 13:09:41 +01:00
parent 763b2ef0f5
commit effc078918
9 changed files with 768 additions and 446 deletions

View File

@@ -1,28 +1,36 @@
-- Simplify RoutingMode enum: remove POST_MAIN, rename PARALLEL SHARED
-- Simplify RoutingMode enum: remove POST_MAIN, rename PARALLEL -> SHARED
-- Drop RoutingRule table (routing is now handled via award assignment)
-- 1. Update existing PARALLEL values to SHARED, POST_MAIN to SHARED
-- (safe to run even if no rows match)
UPDATE "Track" SET "routingMode" = 'PARALLEL' WHERE "routingMode" = 'POST_MAIN';
-- 2. Rename PARALLEL SHARED in the enum
ALTER TYPE "RoutingMode" RENAME VALUE 'PARALLEL' TO 'SHARED';
-- 2. Rename PARALLEL -> SHARED in the enum (only if PARALLEL still exists)
DO $$ BEGIN
ALTER TYPE "RoutingMode" RENAME VALUE 'PARALLEL' TO 'SHARED';
EXCEPTION WHEN invalid_parameter_value THEN NULL; WHEN others THEN NULL; END $$;
-- 3. Remove POST_MAIN from the enum
-- PostgreSQL doesn't support DROP VALUE directly, so we recreate the enum
-- Since we already converted POST_MAIN values to PARALLEL (now SHARED), this is safe
-- Create new enum without POST_MAIN
-- Actually, since we already renamed PARALLEL to SHARED and converted POST_MAIN rows,
-- we just need to remove the POST_MAIN value. PostgreSQL 13+ doesn't support dropping
-- enum values natively, but since all rows are already migrated, we can:
CREATE TYPE "RoutingMode_new" AS ENUM ('SHARED', 'EXCLUSIVE');
-- Only recreate if the old enum still has POST_MAIN (i.e., hasn't been done yet)
DO $$ BEGIN
IF EXISTS (
SELECT 1 FROM pg_enum
WHERE enumlabel = 'POST_MAIN'
AND enumtypid = (SELECT oid FROM pg_type WHERE typname = 'RoutingMode')
) THEN
CREATE TYPE "RoutingMode_new" AS ENUM ('SHARED', 'EXCLUSIVE');
ALTER TABLE "Track"
ALTER COLUMN "routingMode" TYPE "RoutingMode_new"
USING ("routingMode"::text::"RoutingMode_new");
ALTER TABLE "Track"
ALTER COLUMN "routingMode" TYPE "RoutingMode_new"
USING ("routingMode"::text::"RoutingMode_new");
DROP TYPE "RoutingMode";
ALTER TYPE "RoutingMode_new" RENAME TO "RoutingMode";
DROP TYPE "RoutingMode";
ALTER TYPE "RoutingMode_new" RENAME TO "RoutingMode";
END IF;
END $$;
-- 4. Drop the RoutingRule table (no longer needed)
DROP TABLE IF EXISTS "RoutingRule";