-- Migration script to update learning_resources table to support multiple categories -- Run this in your Supabase SQL Editor -- First, create helper functions if they don'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; CREATE OR REPLACE FUNCTION table_exists( p_table TEXT ) RETURNS BOOLEAN AS $$ DECLARE v_exists BOOLEAN; BEGIN SELECT EXISTS ( SELECT 1 FROM information_schema.tables WHERE table_schema = 'public' AND table_name = p_table ) INTO v_exists; RETURN v_exists; END; $$ LANGUAGE plpgsql; -- Create is_admin function if it doesn't exist DO $$ BEGIN IF NOT EXISTS ( SELECT 1 FROM pg_proc WHERE proname = 'is_admin' AND pronamespace = (SELECT oid FROM pg_namespace WHERE nspname = 'public') ) THEN RAISE NOTICE 'is_admin function does not exist, will create it'; ELSE RAISE NOTICE 'is_admin function already exists, skipping creation'; END IF; END $$; -- Create the function conditionally CREATE OR REPLACE FUNCTION public.is_admin(user_id uuid) RETURNS boolean AS $$ DECLARE admin_status boolean; BEGIN -- Direct query without using policies SELECT is_admin INTO admin_status FROM public.users WHERE id = user_id; RETURN COALESCE(admin_status, false); END; $$ LANGUAGE plpgsql SECURITY DEFINER; -- Step 1: Rename the learning_resources table to learning if it exists DO $$ BEGIN IF table_exists('learning_resources') AND NOT table_exists('learning') THEN ALTER TABLE public.learning_resources RENAME TO learning; RAISE NOTICE 'Renamed learning_resources table to learning'; ELSIF NOT table_exists('learning_resources') THEN RAISE NOTICE 'learning_resources table does not exist'; ELSIF table_exists('learning') THEN RAISE NOTICE 'learning table already exists'; END IF; END $$; -- Step 2: Rename category_id to primary_category_id in the learning table if needed DO $$ BEGIN IF table_exists('learning') AND column_exists('learning', 'category_id') AND NOT column_exists('learning', 'primary_category_id') THEN ALTER TABLE public.learning RENAME COLUMN category_id TO primary_category_id; RAISE NOTICE 'Renamed category_id to primary_category_id in learning table'; ELSIF table_exists('learning') AND NOT column_exists('learning', 'primary_category_id') THEN ALTER TABLE public.learning ADD COLUMN primary_category_id BIGINT REFERENCES public.categories(id); RAISE NOTICE 'Added primary_category_id column to learning table'; ELSE RAISE NOTICE 'primary_category_id column already exists in learning table or the table does not exist'; END IF; END $$; -- Step 3: Create the learning_categories junction table DO $$ BEGIN IF NOT table_exists('learning_categories') THEN CREATE TABLE public.learning_categories ( id BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY, learning_id BIGINT REFERENCES public.learning(id) ON DELETE CASCADE, category_id BIGINT REFERENCES public.categories(id) ON DELETE CASCADE, is_primary BOOLEAN DEFAULT false, created_at TIMESTAMPTZ DEFAULT now(), updated_at TIMESTAMPTZ DEFAULT now(), UNIQUE(learning_id, category_id) ); -- Enable RLS ALTER TABLE public.learning_categories ENABLE ROW LEVEL SECURITY; -- Add RLS policies CREATE POLICY "Enable read access for all users" ON public.learning_categories FOR SELECT USING (true); CREATE POLICY "Enable insert for authenticated users only" ON public.learning_categories FOR INSERT WITH CHECK (auth.uid() IS NOT NULL); CREATE POLICY "Enable update for authenticated users based on email" ON public.learning_categories FOR UPDATE USING (auth.uid() IS NOT NULL); CREATE POLICY "Enable delete for authenticated users based on email" ON public.learning_categories FOR DELETE USING (auth.uid() IS NOT NULL); -- Add trigger for updated_at CREATE TRIGGER update_learning_categories_updated_at BEFORE UPDATE ON public.learning_categories FOR EACH ROW EXECUTE FUNCTION public.update_updated_at(); RAISE NOTICE 'Created learning_categories table with RLS policies'; ELSE RAISE NOTICE 'learning_categories table already exists'; END IF; END $$; -- Step 4: Create index on relationship table for improved query performance DO $$ BEGIN IF table_exists('learning_categories') THEN CREATE INDEX IF NOT EXISTS idx_learning_categories_learning_id ON public.learning_categories(learning_id); CREATE INDEX IF NOT EXISTS idx_learning_categories_category_id ON public.learning_categories(category_id); RAISE NOTICE 'Created indexes on learning_categories table'; END IF; END $$; -- Step 5: Update RLS policies for the learning table DO $$ DECLARE policy_exists BOOLEAN; BEGIN -- Check if the table exists IF table_exists('learning') THEN -- Check for existing policies SELECT EXISTS ( SELECT 1 FROM pg_policies WHERE tablename = 'learning' AND schemaname = 'public' ) INTO policy_exists; -- Drop existing policies if they exist IF policy_exists THEN DROP POLICY IF EXISTS "Enable read access for all users" ON public.learning; DROP POLICY IF EXISTS "Enable insert for authenticated users only" ON public.learning; DROP POLICY IF EXISTS "Enable update for authenticated users based on email" ON public.learning; DROP POLICY IF EXISTS "Enable delete for authenticated users based on email" ON public.learning; RAISE NOTICE 'Dropped existing policies on learning table'; END IF; -- Create new policies CREATE POLICY "Enable read access for all users" ON public.learning FOR SELECT USING (true); CREATE POLICY "Enable insert for authenticated users only" ON public.learning FOR INSERT WITH CHECK (auth.uid() IS NOT NULL); CREATE POLICY "Enable update for authenticated users based on email" ON public.learning FOR UPDATE USING (auth.uid() IS NOT NULL); CREATE POLICY "Enable delete for authenticated users based on email" ON public.learning FOR DELETE USING (auth.uid() IS NOT NULL); RAISE NOTICE 'Created new policies on learning table'; ELSE RAISE NOTICE 'learning table does not exist'; END IF; END $$; -- Add metadata flag to indicate learning resources have multiple categories DO $$ BEGIN IF table_exists('system_metadata') THEN INSERT INTO public.system_metadata (key, value, description, updated_at) VALUES ( 'learning_have_multiple_categories', 'true', 'Flag indicating that learning resources can have multiple categories', NOW() ) ON CONFLICT (key) DO UPDATE SET value = 'true', updated_at = NOW(); RAISE NOTICE 'Updated system metadata for multiple categories'; END IF; 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 $$;