AI Resource Pro / website / scripts / migrate-learning-resources.sql
| 1 | -- Migration script to add category_id column to learning_resources table |
| 2 | -- Run this in your Supabase SQL Editor |
| 3 | |
| 4 | -- First, create the helper function if it doesn'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 | -- Add category_id column to learning_resources table if it doesn't exist |
| 25 | DO $$ |
| 26 | BEGIN |
| 27 | IF NOT column_exists('learning_resources', 'category_id') THEN |
| 28 | ALTER TABLE public.learning_resources |
| 29 | ADD COLUMN category_id BIGINT REFERENCES public.categories(id); |
| 30 | |
| 31 | RAISE NOTICE 'Added category_id column to learning_resources table'; |
| 32 | ELSE |
| 33 | RAISE NOTICE 'category_id column already exists in learning_resources table'; |
| 34 | END IF; |
| 35 | END $$; |
| 36 | |
| 37 | -- Remove type column from learning_resources table if it exists |
| 38 | DO $$ |
| 39 | BEGIN |
| 40 | IF column_exists('learning_resources', 'type') THEN |
| 41 | ALTER TABLE public.learning_resources |
| 42 | DROP COLUMN type; |
| 43 | |
| 44 | RAISE NOTICE 'Removed type column from learning_resources table'; |
| 45 | ELSE |
| 46 | RAISE NOTICE 'type column does not exist in learning_resources table'; |
| 47 | END IF; |
| 48 | END $$; |
| 49 | |
| 50 | -- Add category field to learning_resources UI for admin use |
| 51 | DO $$ |
| 52 | BEGIN |
| 53 | -- This is just for future reference in case the system needs |
| 54 | -- to know that learning resources have categories |
| 55 | INSERT INTO public.system_metadata (key, value, description, updated_at) |
| 56 | VALUES ( |
| 57 | 'learning_resources_have_categories', |
| 58 | 'true', |
| 59 | 'Flag indicating that learning resources can be categorized', |
| 60 | NOW() |
| 61 | ) |
| 62 | ON CONFLICT (key) |
| 63 | DO UPDATE SET |
| 64 | value = 'true', |
| 65 | updated_at = NOW(); |
| 66 | |
| 67 | EXCEPTION WHEN OTHERS THEN |
| 68 | -- If system_metadata table doesn't exist, just skip this step |
| 69 | RAISE NOTICE 'Could not update system metadata (table might not exist)'; |
| 70 | END $$; |