-- Migration: Add mode progression columns to brain_question_progress -- This migration alters the brain_question_progress table to support per-card mode progression -- Step 1: Add new columns with defaults ALTER TABLE "brain_question_progress" ADD COLUMN IF NOT EXISTS "current_mode" text DEFAULT 'photo-see-bisaya', ADD COLUMN IF NOT EXISTS "mode_index" integer DEFAULT 0, ADD COLUMN IF NOT EXISTS "consecutive_first_try" integer DEFAULT 0, ADD COLUMN IF NOT EXISTS "is_review_only" boolean DEFAULT false; --> statement-breakpoint -- Step 2: Migrate existing data - copy 'mode' to 'current_mode' if it exists UPDATE "brain_question_progress" SET "current_mode" = COALESCE("mode", 'photo-see-bisaya') WHERE "current_mode" IS NULL OR "current_mode" = 'photo-see-bisaya'; --> statement-breakpoint -- Step 3: Set mode_index based on current_mode value UPDATE "brain_question_progress" SET "mode_index" = CASE "current_mode" WHEN 'photo-see-bisaya' THEN 0 WHEN 'photo-see-english' THEN 1 WHEN 'photo-hide-bisaya' THEN 2 WHEN 'photo-hide-english' THEN 3 WHEN 'blank-see-bisaya' THEN 4 WHEN 'blank-see-english' THEN 5 ELSE 0 END WHERE "mode_index" = 0 OR "mode_index" IS NULL; --> statement-breakpoint -- Step 4: Make current_mode NOT NULL now that all rows have values ALTER TABLE "brain_question_progress" ALTER COLUMN "current_mode" SET NOT NULL; --> statement-breakpoint -- Step 5: Drop the old 'mode' column (optional - keeping for backwards compatibility during transition) -- Note: We keep 'mode' column temporarily in case of rollback needs -- ALTER TABLE "brain_question_progress" DROP COLUMN IF EXISTS "mode";