Skip to content

Commit 05f7bf3

Browse files
github-actions[bot]Bill Chirico
andcommitted
fix(db): handle all migration edge cases for show_program_content column
Improve migration idempotency by handling four scenarios: 1. Both columns exist: backfill and drop old column 2. Only old column exists: rename to new column 3. Neither column exists: add new column 4. Only new column exists: no action needed Co-authored-by: Bill Chirico <undefined@users.noreply.github.com>
1 parent cca0001 commit 05f7bf3

1 file changed

Lines changed: 30 additions & 7 deletions

File tree

supabase/migrations/20260124000006_rename_show_program_content.sql

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,57 @@
33
44
Adds the show_program_content column to control Program tab visibility.
55
6-
This migration handles two scenarios:
7-
1. Fresh database: adds show_program_content column directly
8-
2. Existing database with show_twelve_step_content: renames the column
6+
This migration handles four scenarios:
7+
1. Both columns exist: backfill new from old, then drop old column
8+
2. Only old column exists: rename old to new column
9+
3. Neither column exists: add new column with default
10+
4. Only new column exists: no action needed (already migrated)
911
*/
1012

1113
DO $$
1214
BEGIN
13-
-- Check if the old column exists (existing database)
15+
-- Case 1: Both columns exist (partial migration or manual addition)
16+
-- Backfill new column from old and drop old column
1417
IF EXISTS (
1518
SELECT 1 FROM information_schema.columns
1619
WHERE table_schema = 'public'
1720
AND table_name = 'profiles'
1821
AND column_name = 'show_twelve_step_content'
22+
) AND EXISTS (
23+
SELECT 1 FROM information_schema.columns
24+
WHERE table_schema = 'public'
25+
AND table_name = 'profiles'
26+
AND column_name = 'show_program_content'
27+
) THEN
28+
-- Backfill any NULL values in new column from old column
29+
UPDATE public.profiles
30+
SET show_program_content = show_twelve_step_content
31+
WHERE show_program_content IS NULL AND show_twelve_step_content IS NOT NULL;
32+
-- Drop the old column
33+
ALTER TABLE public.profiles
34+
DROP COLUMN show_twelve_step_content;
35+
-- Case 2: Only old column exists (existing database)
36+
-- Rename old column to new
37+
ELSIF EXISTS (
38+
SELECT 1 FROM information_schema.columns
39+
WHERE table_schema = 'public'
40+
AND table_name = 'profiles'
41+
AND column_name = 'show_twelve_step_content'
1942
) THEN
20-
-- Rename existing column
2143
ALTER TABLE public.profiles
2244
RENAME COLUMN show_twelve_step_content TO show_program_content;
23-
-- Check if neither column exists (fresh database)
45+
-- Case 3: Neither column exists (fresh database)
46+
-- Add new column
2447
ELSIF NOT EXISTS (
2548
SELECT 1 FROM information_schema.columns
2649
WHERE table_schema = 'public'
2750
AND table_name = 'profiles'
2851
AND column_name = 'show_program_content'
2952
) THEN
30-
-- Add new column
3153
ALTER TABLE public.profiles
3254
ADD COLUMN show_program_content BOOLEAN DEFAULT true;
3355
END IF;
56+
-- Case 4: Only new column exists - no action needed (migration already complete)
3457
END $$;
3558

3659
COMMENT ON COLUMN public.profiles.show_program_content IS

0 commit comments

Comments
 (0)