11SET search_path = public, auth;
22
3- -- Add PRIMARY KEY constraint to the foods table
3+ -- Add PRIMARY KEY constraint to the foods table (from original 20250711173824_meal_features.sql)
44ALTER TABLE public .foods
55ADD CONSTRAINT foods_pkey PRIMARY KEY (id);
66
7-
8-
9-
10-
11- -- Create meals table
7+ -- Create meals table (from original 20250711173824_meal_features.sql)
128CREATE TABLE public .meals (
139 id uuid NOT NULL DEFAULT gen_random_uuid(),
1410 user_id uuid NOT NULL ,
@@ -20,6 +16,7 @@ CREATE TABLE public.meals (
2016 CONSTRAINT meals_pkey PRIMARY KEY (id)
2117);
2218
19+ -- Create meal_foods table (from original 20250711173824_meal_features.sql)
2320CREATE TABLE public .meal_foods (
2421 id uuid NOT NULL DEFAULT gen_random_uuid(),
2522 meal_id uuid NOT NULL ,
@@ -32,6 +29,7 @@ CREATE TABLE public.meal_foods (
3229 CONSTRAINT meal_foods_pkey PRIMARY KEY (id)
3330);
3431
32+ -- Create meal_plans table (from original 20250711173824_meal_features.sql)
3533CREATE TABLE public .meal_plans (
3634 id uuid NOT NULL DEFAULT gen_random_uuid(),
3735 user_id uuid NOT NULL ,
@@ -54,11 +52,81 @@ CREATE TABLE public.meal_plans (
5452 )
5553);
5654
57- -- Add meal_plan_id to food_entries table
55+ -- Refactor meal_plan_templates (from 20250711215400_refactor_meal_plan_templates.sql)
56+ -- Drop old tables if they exist from previous attempts (important for clean re-runs)
57+ DROP TABLE IF EXISTS public .meal_plan_template_assignments CASCADE;
58+ DROP TABLE IF EXISTS public .meal_day_presets CASCADE;
59+ DROP TABLE IF EXISTS public .meal_plan_templates CASCADE;
60+
61+ -- Create the new, cleaner meal_plan_templates table
62+ CREATE TABLE public .meal_plan_templates (
63+ id UUID NOT NULL DEFAULT gen_random_uuid(),
64+ user_id UUID NOT NULL ,
65+ plan_name VARCHAR (255 ) NOT NULL ,
66+ description TEXT ,
67+ start_date DATE NOT NULL ,
68+ end_date DATE NULL ,
69+ is_active BOOLEAN NOT NULL DEFAULT TRUE,
70+ created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(),
71+ updated_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(),
72+ CONSTRAINT meal_plan_templates_pkey PRIMARY KEY (id),
73+ CONSTRAINT meal_plan_templates_user_id_fkey FOREIGN KEY (user_id) REFERENCES auth .users (id) ON DELETE CASCADE
74+ );
75+
76+ -- Create the meal_plan_template_assignments table (from 20250711215400_refactor_meal_plan_templates.sql)
77+ CREATE TABLE public .meal_plan_template_assignments (
78+ id UUID NOT NULL DEFAULT gen_random_uuid(),
79+ template_id UUID NOT NULL ,
80+ day_of_week INTEGER NOT NULL , -- 0 for Sunday, 1 for Monday, etc.
81+ meal_type VARCHAR (50 ) NOT NULL , -- 'breakfast', 'lunch', 'dinner', 'snacks'
82+ meal_id UUID, -- Made nullable here due to later migration
83+ CONSTRAINT meal_plan_template_assignments_pkey PRIMARY KEY (id),
84+ CONSTRAINT meal_plan_template_assignments_template_id_fkey FOREIGN KEY (template_id) REFERENCES public .meal_plan_templates (id) ON DELETE CASCADE ,
85+ CONSTRAINT meal_plan_template_assignments_meal_id_fkey FOREIGN KEY (meal_id) REFERENCES public .meals (id) ON DELETE CASCADE
86+ );
87+
88+ -- Add columns to meal_plan_template_assignments (from 20250712154550_add_food_to_meal_plan_assignments.sql)
89+ ALTER TABLE meal_plan_template_assignments
90+ ADD COLUMN item_type VARCHAR (50 ) NOT NULL DEFAULT ' meal' ,
91+ ADD COLUMN food_id UUID,
92+ ADD COLUMN variant_id UUID,
93+ ADD COLUMN quantity NUMERIC (10 , 2 ),
94+ ADD COLUMN unit VARCHAR (50 );
95+
96+ -- Add foreign key constraints for food and food_variant to meal_plan_template_assignments
97+ ALTER TABLE meal_plan_template_assignments
98+ ADD CONSTRAINT fk_food
99+ FOREIGN KEY (food_id) REFERENCES foods(id) ON DELETE CASCADE ;
100+
101+ ALTER TABLE meal_plan_template_assignments
102+ ADD CONSTRAINT fk_food_variant
103+ FOREIGN KEY (variant_id) REFERENCES food_variants(id) ON DELETE CASCADE ;
104+
105+ -- Add meal_plan_template_id to food_entries table and fix foreign key (consolidated)
58106ALTER TABLE public .food_entries
59- ADD COLUMN IF NOT EXISTS meal_plan_id uuid;
107+ ADD COLUMN meal_plan_template_id uuid;
108+
109+ ALTER TABLE public .food_entries
110+ ADD CONSTRAINT food_entries_meal_plan_template_id_fkey
111+ FOREIGN KEY (meal_plan_template_id) REFERENCES public .meal_plan_templates (id) ON DELETE SET NULL ;
112+
113+ -- Add a unique constraint to ensure only one plan is active for a user at any given time. (from 20250711215400_refactor_meal_plan_templates.sql)
114+ CREATE UNIQUE INDEX one_active_meal_plan_per_user
115+ ON public .meal_plan_templates (user_id)
116+ WHERE is_active = TRUE;
60117
61- -- Add foreign key constraints after all tables are created
118+ -- Add/Update check constraint for meal_plan_template_assignments (from 20250712155253_alter_meal_id_nullable_in_meal_plan_assignments.sql)
119+ ALTER TABLE meal_plan_template_assignments
120+ DROP CONSTRAINT IF EXISTS chk_item_type_and_id;
121+
122+ ALTER TABLE meal_plan_template_assignments
123+ ADD CONSTRAINT chk_item_type_and_id
124+ CHECK (
125+ (item_type = ' meal' AND meal_id IS NOT NULL AND food_id IS NULL ) OR
126+ (item_type = ' food' AND food_id IS NOT NULL AND meal_id IS NULL )
127+ );
128+
129+ -- Add foreign key constraints after all tables are created (from original 20250711173824_meal_features.sql)
62130ALTER TABLE public .meals
63131ADD CONSTRAINT meals_user_id_fkey FOREIGN KEY (user_id) REFERENCES auth .users (id) ON DELETE CASCADE NOT VALID;
64132ALTER TABLE public .meals VALIDATE CONSTRAINT meals_user_id_fkey;
@@ -80,7 +148,3 @@ ALTER TABLE public.meal_plans VALIDATE CONSTRAINT meal_plans_user_id_fkey;
80148ALTER TABLE public .meal_plans VALIDATE CONSTRAINT meal_plans_meal_id_fkey;
81149ALTER TABLE public .meal_plans VALIDATE CONSTRAINT meal_plans_food_id_fkey;
82150ALTER TABLE public .meal_plans VALIDATE CONSTRAINT meal_plans_variant_id_fkey;
83-
84- ALTER TABLE public .food_entries
85- ADD CONSTRAINT food_entries_meal_plan_id_fkey FOREIGN KEY (meal_plan_id) REFERENCES public .meal_plans (id) ON DELETE SET NULL NOT VALID;
86- ALTER TABLE public .food_entries VALIDATE CONSTRAINT food_entries_meal_plan_id_fkey;
0 commit comments