Fix idempotency on 0069

This commit is contained in:
Chris Parsons 2026-04-06 15:32:39 +00:00
parent 90127fe5bd
commit b19445f2fe
2 changed files with 22 additions and 10 deletions

View file

@ -1,19 +1,27 @@
-- Step 1: Add new columns as nullable -- Step 1: Add new columns as nullable
ALTER TABLE "sports_seasons" ADD COLUMN "draft_on" date;--> statement-breakpoint ALTER TABLE "sports_seasons" ADD COLUMN IF NOT EXISTS "draft_on" date;--> statement-breakpoint
ALTER TABLE "sports_seasons" ADD COLUMN "draft_off" date;--> statement-breakpoint ALTER TABLE "sports_seasons" ADD COLUMN IF NOT EXISTS "draft_off" date;--> statement-breakpoint
-- Step 2: Backfill data based on existing is_draftable flag -- Step 2: Backfill data based on existing is_draftable flag (only if column still exists)
UPDATE "sports_seasons" DO $$
SET "draft_on" = '2026-04-06', "draft_off" = '2099-12-31' BEGIN
WHERE "is_draftable" = true;--> statement-breakpoint IF EXISTS (
SELECT 1 FROM information_schema.columns
WHERE table_name = 'sports_seasons' AND column_name = 'is_draftable'
) THEN
UPDATE "sports_seasons"
SET "draft_on" = '2026-04-06', "draft_off" = '2099-12-31'
WHERE "is_draftable" = true;
UPDATE "sports_seasons" UPDATE "sports_seasons"
SET "draft_on" = '2020-01-01', "draft_off" = '2020-12-31' SET "draft_on" = '2020-01-01', "draft_off" = '2020-12-31'
WHERE "is_draftable" = false;--> statement-breakpoint WHERE "is_draftable" = false;
END IF;
END $$;--> statement-breakpoint
-- Step 3: Apply NOT NULL constraints -- Step 3: Apply NOT NULL constraints
ALTER TABLE "sports_seasons" ALTER COLUMN "draft_on" SET NOT NULL;--> statement-breakpoint ALTER TABLE "sports_seasons" ALTER COLUMN "draft_on" SET NOT NULL;--> statement-breakpoint
ALTER TABLE "sports_seasons" ALTER COLUMN "draft_off" SET NOT NULL;--> statement-breakpoint ALTER TABLE "sports_seasons" ALTER COLUMN "draft_off" SET NOT NULL;--> statement-breakpoint
-- Step 4: Drop old column -- Step 4: Drop old column
ALTER TABLE "sports_seasons" DROP COLUMN "is_draftable"; ALTER TABLE "sports_seasons" DROP COLUMN IF EXISTS "is_draftable";

View file

@ -0,0 +1,4 @@
ALTER TABLE "playoff_match_games" ADD COLUMN "scheduled_date" date;--> statement-breakpoint
UPDATE "playoff_match_games"
SET "scheduled_date" = DATE("scheduled_at" AT TIME ZONE 'UTC')
WHERE "scheduled_at" IS NOT NULL;