AI Resource Pro / website / scripts / migrate-learning-categories.sql
| 1 | -- Migration script to update learning_resources table to support multiple categories |
| 2 | -- Run this in your Supabase SQL Editor |
| 3 | |
| 4 | -- First, create helper functions if they don't exist |
| 5 | CREATE OR REPLACE FUNCTION column_exists( |
| 6 | p_table TEXT, |
| 7 | p_column TEXT |
| 8 | ) RETURNS BOOLEAN AS $$ |
| 9 | DECLARE |
| 10 | v_exists BOOLEAN; |
| 11 | BEGIN |
| 12 | SELECT EXISTS ( |
| 13 | SELECT 1 |
| 14 | FROM information_schema.columns |
| 15 | WHERE table_schema = 'public' |
| 16 | AND table_name = p_table |
| 17 | AND column_name = p_column |
| 18 | ) INTO v_exists; |
| 19 | |
| 20 | RETURN v_exists; |
| 21 | END; |
| 22 | $$ LANGUAGE plpgsql; |
| 23 | |
| 24 | CREATE OR REPLACE FUNCTION table_exists( |
| 25 | p_table TEXT |
| 26 | ) RETURNS BOOLEAN AS $$ |
| 27 | DECLARE |
| 28 | v_exists BOOLEAN; |
| 29 | BEGIN |
| 30 | SELECT EXISTS ( |
| 31 | SELECT 1 |
| 32 | FROM information_schema.tables |
| 33 | WHERE table_schema = 'public' |
| 34 | AND table_name = p_table |
| 35 | ) INTO v_exists; |
| 36 | |
| 37 | RETURN v_exists; |
| 38 | END; |
| 39 | $$ LANGUAGE plpgsql; |
| 40 | |
| 41 | -- Create is_admin function if it doesn't exist |
| 42 | DO $$ |
| 43 | BEGIN |
| 44 | IF NOT EXISTS ( |
| 45 | SELECT 1 |
| 46 | FROM pg_proc |
| 47 | WHERE proname = 'is_admin' |
| 48 | AND pronamespace = (SELECT oid FROM pg_namespace WHERE nspname = 'public') |
| 49 | ) THEN |
| 50 | RAISE NOTICE 'is_admin function does not exist, will create it'; |
| 51 | ELSE |
| 52 | RAISE NOTICE 'is_admin function already exists, skipping creation'; |
| 53 | END IF; |
| 54 | END $$; |
| 55 | |
| 56 | -- Create the function conditionally |
| 57 | CREATE OR REPLACE FUNCTION public.is_admin(user_id uuid) |
| 58 | RETURNS boolean AS $$ |
| 59 | DECLARE |
| 60 | admin_status boolean; |
| 61 | BEGIN |
| 62 | -- Direct query without using policies |
| 63 | SELECT is_admin INTO admin_status FROM public.users WHERE id = user_id; |
| 64 | RETURN COALESCE(admin_status, false); |
| 65 | END; |
| 66 | $$ LANGUAGE plpgsql SECURITY DEFINER; |
| 67 | |
| 68 | -- Step 1: Rename the learning_resources table to learning if it exists |
| 69 | DO $$ |
| 70 | BEGIN |
| 71 | IF table_exists('learning_resources') AND NOT table_exists('learning') THEN |
| 72 | ALTER TABLE public.learning_resources RENAME TO learning; |
| 73 | RAISE NOTICE 'Renamed learning_resources table to learning'; |
| 74 | ELSIF NOT table_exists('learning_resources') THEN |
| 75 | RAISE NOTICE 'learning_resources table does not exist'; |
| 76 | ELSIF table_exists('learning') THEN |
| 77 | RAISE NOTICE 'learning table already exists'; |
| 78 | END IF; |
| 79 | END $$; |
| 80 | |
| 81 | -- Step 2: Rename category_id to primary_category_id in the learning table if needed |
| 82 | DO $$ |
| 83 | BEGIN |
| 84 | IF table_exists('learning') AND column_exists('learning', 'category_id') AND NOT column_exists('learning', 'primary_category_id') THEN |
| 85 | ALTER TABLE public.learning |
| 86 | RENAME COLUMN category_id TO primary_category_id; |
| 87 | |
| 88 | RAISE NOTICE 'Renamed category_id to primary_category_id in learning table'; |
| 89 | ELSIF table_exists('learning') AND NOT column_exists('learning', 'primary_category_id') THEN |
| 90 | ALTER TABLE public.learning |
| 91 | ADD COLUMN primary_category_id BIGINT REFERENCES public.categories(id); |
| 92 | |
| 93 | RAISE NOTICE 'Added primary_category_id column to learning table'; |
| 94 | ELSE |
| 95 | RAISE NOTICE 'primary_category_id column already exists in learning table or the table does not exist'; |
| 96 | END IF; |
| 97 | END $$; |
| 98 | |
| 99 | -- Step 3: Create the learning_categories junction table |
| 100 | DO $$ |
| 101 | BEGIN |
| 102 | IF NOT table_exists('learning_categories') THEN |
| 103 | CREATE TABLE public.learning_categories ( |
| 104 | id BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY, |
| 105 | learning_id BIGINT REFERENCES public.learning(id) ON DELETE CASCADE, |
| 106 | category_id BIGINT REFERENCES public.categories(id) ON DELETE CASCADE, |
| 107 | is_primary BOOLEAN DEFAULT false, |
| 108 | created_at TIMESTAMPTZ DEFAULT now(), |
| 109 | updated_at TIMESTAMPTZ DEFAULT now(), |
| 110 | UNIQUE(learning_id, category_id) |
| 111 | ); |
| 112 | |
| 113 | -- Enable RLS |
| 114 | ALTER TABLE public.learning_categories ENABLE ROW LEVEL SECURITY; |
| 115 | |
| 116 | -- Add RLS policies |
| 117 | CREATE POLICY "Enable read access for all users" |
| 118 | ON public.learning_categories |
| 119 | FOR SELECT USING (true); |
| 120 | |
| 121 | CREATE POLICY "Enable insert for authenticated users only" |
| 122 | ON public.learning_categories |
| 123 | FOR INSERT WITH CHECK (auth.uid() IS NOT NULL); |
| 124 | |
| 125 | CREATE POLICY "Enable update for authenticated users based on email" |
| 126 | ON public.learning_categories |
| 127 | FOR UPDATE USING (auth.uid() IS NOT NULL); |
| 128 | |
| 129 | CREATE POLICY "Enable delete for authenticated users based on email" |
| 130 | ON public.learning_categories |
| 131 | FOR DELETE USING (auth.uid() IS NOT NULL); |
| 132 | |
| 133 | -- Add trigger for updated_at |
| 134 | CREATE TRIGGER update_learning_categories_updated_at |
| 135 | BEFORE UPDATE ON public.learning_categories |
| 136 | FOR EACH ROW |
| 137 | EXECUTE FUNCTION public.update_updated_at(); |
| 138 | |
| 139 | RAISE NOTICE 'Created learning_categories table with RLS policies'; |
| 140 | ELSE |
| 141 | RAISE NOTICE 'learning_categories table already exists'; |
| 142 | END IF; |
| 143 | END $$; |
| 144 | |
| 145 | -- Step 4: Create index on relationship table for improved query performance |
| 146 | DO $$ |
| 147 | BEGIN |
| 148 | IF table_exists('learning_categories') THEN |
| 149 | CREATE INDEX IF NOT EXISTS idx_learning_categories_learning_id |
| 150 | ON public.learning_categories(learning_id); |
| 151 | |
| 152 | CREATE INDEX IF NOT EXISTS idx_learning_categories_category_id |
| 153 | ON public.learning_categories(category_id); |
| 154 | |
| 155 | RAISE NOTICE 'Created indexes on learning_categories table'; |
| 156 | END IF; |
| 157 | END $$; |
| 158 | |
| 159 | -- Step 5: Update RLS policies for the learning table |
| 160 | DO $$ |
| 161 | DECLARE |
| 162 | policy_exists BOOLEAN; |
| 163 | BEGIN |
| 164 | -- Check if the table exists |
| 165 | IF table_exists('learning') THEN |
| 166 | -- Check for existing policies |
| 167 | SELECT EXISTS ( |
| 168 | SELECT 1 |
| 169 | FROM pg_policies |
| 170 | WHERE tablename = 'learning' |
| 171 | AND schemaname = 'public' |
| 172 | ) INTO policy_exists; |
| 173 | |
| 174 | -- Drop existing policies if they exist |
| 175 | IF policy_exists THEN |
| 176 | DROP POLICY IF EXISTS "Enable read access for all users" ON public.learning; |
| 177 | DROP POLICY IF EXISTS "Enable insert for authenticated users only" ON public.learning; |
| 178 | DROP POLICY IF EXISTS "Enable update for authenticated users based on email" ON public.learning; |
| 179 | DROP POLICY IF EXISTS "Enable delete for authenticated users based on email" ON public.learning; |
| 180 | RAISE NOTICE 'Dropped existing policies on learning table'; |
| 181 | END IF; |
| 182 | |
| 183 | -- Create new policies |
| 184 | CREATE POLICY "Enable read access for all users" ON public.learning |
| 185 | FOR SELECT USING (true); |
| 186 | |
| 187 | CREATE POLICY "Enable insert for authenticated users only" ON public.learning |
| 188 | FOR INSERT WITH CHECK (auth.uid() IS NOT NULL); |
| 189 | |
| 190 | CREATE POLICY "Enable update for authenticated users based on email" ON public.learning |
| 191 | FOR UPDATE USING (auth.uid() IS NOT NULL); |
| 192 | |
| 193 | CREATE POLICY "Enable delete for authenticated users based on email" ON public.learning |
| 194 | FOR DELETE USING (auth.uid() IS NOT NULL); |
| 195 | |
| 196 | RAISE NOTICE 'Created new policies on learning table'; |
| 197 | ELSE |
| 198 | RAISE NOTICE 'learning table does not exist'; |
| 199 | END IF; |
| 200 | END $$; |
| 201 | |
| 202 | -- Add metadata flag to indicate learning resources have multiple categories |
| 203 | DO $$ |
| 204 | BEGIN |
| 205 | IF table_exists('system_metadata') THEN |
| 206 | INSERT INTO public.system_metadata (key, value, description, updated_at) |
| 207 | VALUES ( |
| 208 | 'learning_have_multiple_categories', |
| 209 | 'true', |
| 210 | 'Flag indicating that learning resources can have multiple categories', |
| 211 | NOW() |
| 212 | ) |
| 213 | ON CONFLICT (key) |
| 214 | DO UPDATE SET |
| 215 | value = 'true', |
| 216 | updated_at = NOW(); |
| 217 | |
| 218 | RAISE NOTICE 'Updated system metadata for multiple categories'; |
| 219 | END IF; |
| 220 | EXCEPTION WHEN OTHERS THEN |
| 221 | -- If system_metadata table doesn't exist, just skip this step |
| 222 | RAISE NOTICE 'Could not update system metadata (table might not exist)'; |
| 223 | END $$; |