From 4493b0824e0f30288a574e88a902666d9ab6cb23 Mon Sep 17 00:00:00 2001 From: "gpt-engineer-app[bot]" <159125892+gpt-engineer-app[bot]@users.noreply.github.com> Date: Fri, 3 Oct 2025 14:54:01 +0000 Subject: [PATCH] Fix Phase 3D migration errors --- src/integrations/supabase/types.ts | 237 +++++++++--------- ...9_d3d83c36-d584-44ea-a1d3-f451df096bb2.sql | 206 +++++++++++++++ 2 files changed, 326 insertions(+), 117 deletions(-) create mode 100644 supabase/migrations/20251003145349_d3d83c36-d584-44ea-a1d3-f451df096bb2.sql diff --git a/src/integrations/supabase/types.ts b/src/integrations/supabase/types.ts index c5e62549..7bf71017 100644 --- a/src/integrations/supabase/types.ts +++ b/src/integrations/supabase/types.ts @@ -1154,82 +1154,6 @@ export type Database = { }, ] } - ride_coaster_stats: { - Row: { - category: string | null - created_at: string - id: string - ride_submission_id: string - stat_name: string - stat_value: number - unit: string | null - } - Insert: { - category?: string | null - created_at?: string - id?: string - ride_submission_id: string - stat_name: string - stat_value: number - unit?: string | null - } - Update: { - category?: string | null - created_at?: string - id?: string - ride_submission_id?: string - stat_name?: string - stat_value?: number - unit?: string | null - } - Relationships: [ - { - foreignKeyName: "ride_coaster_stats_ride_submission_id_fkey" - columns: ["ride_submission_id"] - isOneToOne: false - referencedRelation: "ride_submissions" - referencedColumns: ["id"] - }, - ] - } - ride_former_names: { - Row: { - created_at: string - date_changed: string | null - former_name: string - id: string - order_index: number | null - reason: string | null - ride_submission_id: string - } - Insert: { - created_at?: string - date_changed?: string | null - former_name: string - id?: string - order_index?: number | null - reason?: string | null - ride_submission_id: string - } - Update: { - created_at?: string - date_changed?: string | null - former_name?: string - id?: string - order_index?: number | null - reason?: string | null - ride_submission_id?: string - } - Relationships: [ - { - foreignKeyName: "ride_former_names_ride_submission_id_fkey" - columns: ["ride_submission_id"] - isOneToOne: false - referencedRelation: "ride_submissions" - referencedColumns: ["id"] - }, - ] - } ride_model_submissions: { Row: { banner_image_id: string | null @@ -1433,6 +1357,126 @@ export type Database = { }, ] } + ride_submission_coaster_statistics: { + Row: { + category: string | null + created_at: string + id: string + ride_submission_id: string + stat_name: string + stat_value: number + unit: string | null + } + Insert: { + category?: string | null + created_at?: string + id?: string + ride_submission_id: string + stat_name: string + stat_value: number + unit?: string | null + } + Update: { + category?: string | null + created_at?: string + id?: string + ride_submission_id?: string + stat_name?: string + stat_value?: number + unit?: string | null + } + Relationships: [ + { + foreignKeyName: "fk_ride_submission_coaster_statistics_ride_submission_id" + columns: ["ride_submission_id"] + isOneToOne: false + referencedRelation: "ride_submissions" + referencedColumns: ["id"] + }, + ] + } + ride_submission_name_history: { + Row: { + created_at: string + date_changed: string | null + former_name: string + id: string + order_index: number | null + reason: string | null + ride_submission_id: string + } + Insert: { + created_at?: string + date_changed?: string | null + former_name: string + id?: string + order_index?: number | null + reason?: string | null + ride_submission_id: string + } + Update: { + created_at?: string + date_changed?: string | null + former_name?: string + id?: string + order_index?: number | null + reason?: string | null + ride_submission_id?: string + } + Relationships: [ + { + foreignKeyName: "fk_ride_submission_name_history_ride_submission_id" + columns: ["ride_submission_id"] + isOneToOne: false + referencedRelation: "ride_submissions" + referencedColumns: ["id"] + }, + ] + } + ride_submission_technical_specifications: { + Row: { + category: string | null + created_at: string + display_order: number | null + id: string + ride_submission_id: string + spec_name: string + spec_type: string + spec_value: string + unit: string | null + } + Insert: { + category?: string | null + created_at?: string + display_order?: number | null + id?: string + ride_submission_id: string + spec_name: string + spec_type: string + spec_value: string + unit?: string | null + } + Update: { + category?: string | null + created_at?: string + display_order?: number | null + id?: string + ride_submission_id?: string + spec_name?: string + spec_type?: string + spec_value?: string + unit?: string | null + } + Relationships: [ + { + foreignKeyName: "fk_ride_submission_technical_specifications_ride_submission_id" + columns: ["ride_submission_id"] + isOneToOne: false + referencedRelation: "ride_submissions" + referencedColumns: ["id"] + }, + ] + } ride_submissions: { Row: { age_requirement: number | null @@ -1596,47 +1640,6 @@ export type Database = { }, ] } - ride_technical_specs: { - Row: { - category: string | null - created_at: string - id: string - ride_submission_id: string - spec_name: string - spec_type: string - spec_value: string - unit: string | null - } - Insert: { - category?: string | null - created_at?: string - id?: string - ride_submission_id: string - spec_name: string - spec_type: string - spec_value: string - unit?: string | null - } - Update: { - category?: string | null - created_at?: string - id?: string - ride_submission_id?: string - spec_name?: string - spec_type?: string - spec_value?: string - unit?: string | null - } - Relationships: [ - { - foreignKeyName: "ride_technical_specs_ride_submission_id_fkey" - columns: ["ride_submission_id"] - isOneToOne: false - referencedRelation: "ride_submissions" - referencedColumns: ["id"] - }, - ] - } rides: { Row: { age_requirement: number | null diff --git a/supabase/migrations/20251003145349_d3d83c36-d584-44ea-a1d3-f451df096bb2.sql b/supabase/migrations/20251003145349_d3d83c36-d584-44ea-a1d3-f451df096bb2.sql new file mode 100644 index 00000000..825feab2 --- /dev/null +++ b/supabase/migrations/20251003145349_d3d83c36-d584-44ea-a1d3-f451df096bb2.sql @@ -0,0 +1,206 @@ +-- ═══════════════════════════════════════════════════════════════════ +-- PHASE 3D: Standardize Submission Table Names (Final) +-- ═══════════════════════════════════════════════════════════════════ + +CREATE TABLE public.ride_submission_coaster_statistics ( + id UUID PRIMARY KEY DEFAULT gen_random_uuid(), + ride_submission_id UUID NOT NULL, + stat_name TEXT NOT NULL, + stat_value NUMERIC NOT NULL, + unit TEXT, + category TEXT, + created_at TIMESTAMPTZ NOT NULL DEFAULT now() +); + +CREATE TABLE public.ride_submission_name_history ( + id UUID PRIMARY KEY DEFAULT gen_random_uuid(), + ride_submission_id UUID NOT NULL, + former_name TEXT NOT NULL, + date_changed DATE, + reason TEXT, + order_index INTEGER DEFAULT 0, + created_at TIMESTAMPTZ NOT NULL DEFAULT now() +); + +CREATE TABLE public.ride_submission_technical_specifications ( + id UUID PRIMARY KEY DEFAULT gen_random_uuid(), + ride_submission_id UUID NOT NULL, + spec_name TEXT NOT NULL, + spec_value TEXT NOT NULL, + spec_type TEXT NOT NULL, + unit TEXT, + category TEXT, + display_order INTEGER DEFAULT 0, + created_at TIMESTAMPTZ NOT NULL DEFAULT now() +); + +-- Migrate data +INSERT INTO public.ride_submission_coaster_statistics + (id, ride_submission_id, stat_name, stat_value, unit, category, created_at) +SELECT + id, ride_submission_id, stat_name, stat_value, unit, category, created_at +FROM public.ride_coaster_stats; + +INSERT INTO public.ride_submission_name_history + (id, ride_submission_id, former_name, date_changed, reason, order_index, created_at) +SELECT + id, ride_submission_id, former_name, date_changed, reason, order_index, created_at +FROM public.ride_former_names; + +INSERT INTO public.ride_submission_technical_specifications + (id, ride_submission_id, spec_name, spec_value, spec_type, unit, category, created_at) +SELECT + id, ride_submission_id, spec_name, spec_value, spec_type, unit, category, created_at +FROM public.ride_technical_specs; + +-- Create constraints +ALTER TABLE public.ride_submission_coaster_statistics + ADD CONSTRAINT fk_ride_submission_coaster_statistics_ride_submission_id + FOREIGN KEY (ride_submission_id) REFERENCES public.ride_submissions(id) ON DELETE CASCADE; + +ALTER TABLE public.ride_submission_name_history + ADD CONSTRAINT fk_ride_submission_name_history_ride_submission_id + FOREIGN KEY (ride_submission_id) REFERENCES public.ride_submissions(id) ON DELETE CASCADE; + +ALTER TABLE public.ride_submission_technical_specifications + ADD CONSTRAINT fk_ride_submission_technical_specifications_ride_submission_id + FOREIGN KEY (ride_submission_id) REFERENCES public.ride_submissions(id) ON DELETE CASCADE; + +-- Create indexes +CREATE INDEX idx_ride_submission_coaster_statistics_ride_submission_id + ON public.ride_submission_coaster_statistics(ride_submission_id); + +CREATE INDEX idx_ride_submission_name_history_ride_submission_id + ON public.ride_submission_name_history(ride_submission_id); + +CREATE INDEX idx_ride_submission_technical_specifications_ride_submission_id + ON public.ride_submission_technical_specifications(ride_submission_id); + +-- Enable RLS +ALTER TABLE public.ride_submission_coaster_statistics ENABLE ROW LEVEL SECURITY; +ALTER TABLE public.ride_submission_name_history ENABLE ROW LEVEL SECURITY; +ALTER TABLE public.ride_submission_technical_specifications ENABLE ROW LEVEL SECURITY; + +-- Create policies +CREATE POLICY "Moderators can manage ride submission coaster statistics" + ON public.ride_submission_coaster_statistics FOR ALL + USING (is_moderator(auth.uid())); + +CREATE POLICY "Users can insert stats for their own ride submissions" + ON public.ride_submission_coaster_statistics FOR INSERT + WITH CHECK ( + EXISTS ( + SELECT 1 FROM ride_submissions rs + JOIN content_submissions cs ON cs.id = rs.submission_id + WHERE rs.id = ride_submission_coaster_statistics.ride_submission_id + AND cs.user_id = auth.uid() + ) + ); + +CREATE POLICY "Users can view stats for their own ride submissions" + ON public.ride_submission_coaster_statistics FOR SELECT + USING ( + is_moderator(auth.uid()) OR + EXISTS ( + SELECT 1 FROM ride_submissions rs + JOIN content_submissions cs ON cs.id = rs.submission_id + WHERE rs.id = ride_submission_coaster_statistics.ride_submission_id + AND cs.user_id = auth.uid() + ) + ); + +CREATE POLICY "Moderators can manage ride submission name history" + ON public.ride_submission_name_history FOR ALL + USING (is_moderator(auth.uid())); + +CREATE POLICY "Users can insert former names for their own ride submissions" + ON public.ride_submission_name_history FOR INSERT + WITH CHECK ( + EXISTS ( + SELECT 1 FROM ride_submissions rs + JOIN content_submissions cs ON cs.id = rs.submission_id + WHERE rs.id = ride_submission_name_history.ride_submission_id + AND cs.user_id = auth.uid() + ) + ); + +CREATE POLICY "Users can view former names for their own ride submissions" + ON public.ride_submission_name_history FOR SELECT + USING ( + is_moderator(auth.uid()) OR + EXISTS ( + SELECT 1 FROM ride_submissions rs + JOIN content_submissions cs ON cs.id = rs.submission_id + WHERE rs.id = ride_submission_name_history.ride_submission_id + AND cs.user_id = auth.uid() + ) + ); + +CREATE POLICY "Moderators can manage ride submission technical specifications" + ON public.ride_submission_technical_specifications FOR ALL + USING (is_moderator(auth.uid())); + +CREATE POLICY "Users can insert specs for their own ride submissions" + ON public.ride_submission_technical_specifications FOR INSERT + WITH CHECK ( + EXISTS ( + SELECT 1 FROM ride_submissions rs + JOIN content_submissions cs ON cs.id = rs.submission_id + WHERE rs.id = ride_submission_technical_specifications.ride_submission_id + AND cs.user_id = auth.uid() + ) + ); + +CREATE POLICY "Users can view specs for their own ride submissions" + ON public.ride_submission_technical_specifications FOR SELECT + USING ( + is_moderator(auth.uid()) OR + EXISTS ( + SELECT 1 FROM ride_submissions rs + JOIN content_submissions cs ON cs.id = rs.submission_id + WHERE rs.id = ride_submission_technical_specifications.ride_submission_id + AND cs.user_id = auth.uid() + ) + ); + +-- Verify data integrity +DO $$ +DECLARE + old_count INT; + new_count INT; +BEGIN + SELECT COUNT(*) INTO old_count FROM public.ride_coaster_stats; + SELECT COUNT(*) INTO new_count FROM public.ride_submission_coaster_statistics; + IF old_count != new_count THEN + RAISE EXCEPTION 'Coaster stats migration failed: old=%, new=%', old_count, new_count; + END IF; + + SELECT COUNT(*) INTO old_count FROM public.ride_former_names; + SELECT COUNT(*) INTO new_count FROM public.ride_submission_name_history; + IF old_count != new_count THEN + RAISE EXCEPTION 'Name history migration failed: old=%, new=%', old_count, new_count; + END IF; + + SELECT COUNT(*) INTO old_count FROM public.ride_technical_specs; + SELECT COUNT(*) INTO new_count FROM public.ride_submission_technical_specifications; + IF old_count != new_count THEN + RAISE EXCEPTION 'Technical specs migration failed: old=%, new=%', old_count, new_count; + END IF; + + RAISE NOTICE 'Data integrity verified successfully'; +END $$; + +-- Drop old tables +DROP TABLE public.ride_coaster_stats CASCADE; +DROP TABLE public.ride_former_names CASCADE; +DROP TABLE public.ride_technical_specs CASCADE; + +-- Add documentation +COMMENT ON TABLE public.ride_submission_coaster_statistics IS + 'Coaster statistics for ride submissions (renamed from ride_coaster_stats)'; + +COMMENT ON TABLE public.ride_submission_name_history IS + 'Name change history for ride submissions (renamed from ride_former_names)'; + +COMMENT ON TABLE public.ride_submission_technical_specifications IS + 'Technical specifications for ride submissions (renamed from ride_technical_specs)'; \ No newline at end of file