-- Migration script to add category_id column to learning_resources table -- Run this in your Supabase SQL Editor -- First, create the helper function if it doesn't exist CREATE OR REPLACE FUNCTION column_exists( p_table TEXT, p_column TEXT ) RETURNS BOOLEAN AS $$ DECLARE v_exists BOOLEAN; BEGIN SELECT EXISTS ( SELECT 1 FROM information_schema.columns WHERE table_schema = 'public' AND table_name = p_table AND column_name = p_column ) INTO v_exists; RETURN v_exists; END; $$ LANGUAGE plpgsql; -- Add category_id column to learning_resources table if it doesn't exist DO $$ BEGIN IF NOT column_exists('learning_resources', 'category_id') THEN ALTER TABLE public.learning_resources ADD COLUMN category_id BIGINT REFERENCES public.categories(id); RAISE NOTICE 'Added category_id column to learning_resources table'; ELSE RAISE NOTICE 'category_id column already exists in learning_resources table'; END IF; END $$; -- Remove type column from learning_resources table if it exists DO $$ BEGIN IF column_exists('learning_resources', 'type') THEN ALTER TABLE public.learning_resources DROP COLUMN type; RAISE NOTICE 'Removed type column from learning_resources table'; ELSE RAISE NOTICE 'type column does not exist in learning_resources table'; END IF; END $$; -- Add category field to learning_resources UI for admin use DO $$ BEGIN -- This is just for future reference in case the system needs -- to know that learning resources have categories INSERT INTO public.system_metadata (key, value, description, updated_at) VALUES ( 'learning_resources_have_categories', 'true', 'Flag indicating that learning resources can be categorized', NOW() ) ON CONFLICT (key) DO UPDATE SET value = 'true', updated_at = NOW(); EXCEPTION WHEN OTHERS THEN -- If system_metadata table doesn't exist, just skip this step RAISE NOTICE 'Could not update system metadata (table might not exist)'; END $$;