-- Migration script to rename blog_posts and blog_posts_categories tables -- Run this in your Supabase SQL Editor -- First, create helper functions if they don't exist 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; -- Step 1: Rename the blog_posts table to posts if it exists DO $$ BEGIN IF table_exists('blog_posts') AND NOT table_exists('posts') THEN ALTER TABLE public.blog_posts RENAME TO posts; RAISE NOTICE 'Renamed blog_posts table to posts'; ELSIF NOT table_exists('blog_posts') THEN RAISE NOTICE 'blog_posts table does not exist'; ELSIF table_exists('posts') THEN RAISE NOTICE 'posts table already exists'; END IF; END $$; -- Step 2: Rename the blog_posts_categories table to post_categories if it exists DO $$ BEGIN IF table_exists('blog_posts_categories') AND NOT table_exists('post_categories') THEN ALTER TABLE public.blog_posts_categories RENAME TO post_categories; RAISE NOTICE 'Renamed blog_posts_categories table to post_categories'; ELSIF NOT table_exists('blog_posts_categories') THEN RAISE NOTICE 'blog_posts_categories table does not exist'; ELSIF table_exists('post_categories') THEN RAISE NOTICE 'post_categories table already exists'; END IF; END $$; -- Step 3: Rename the column blog_post_id to post_id in the post_categories table DO $$ BEGIN IF table_exists('post_categories') THEN -- Check if the column blog_post_id exists IF EXISTS ( SELECT 1 FROM information_schema.columns WHERE table_schema = 'public' AND table_name = 'post_categories' AND column_name = 'blog_post_id' ) THEN ALTER TABLE public.post_categories RENAME COLUMN blog_post_id TO post_id; RAISE NOTICE 'Renamed blog_post_id to post_id in post_categories table'; ELSE RAISE NOTICE 'blog_post_id column does not exist in post_categories table'; END IF; ELSE RAISE NOTICE 'post_categories table does not exist'; END IF; END $$; -- Step 4: Update RLS policies for the posts table DO $$ DECLARE policy_exists BOOLEAN; BEGIN -- Check if the table exists IF table_exists('posts') THEN -- Check for existing policies SELECT EXISTS ( SELECT 1 FROM pg_policies WHERE tablename = 'posts' 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.posts; DROP POLICY IF EXISTS "Enable insert for authenticated users only" ON public.posts; DROP POLICY IF EXISTS "Enable update for authenticated users based on email" ON public.posts; DROP POLICY IF EXISTS "Enable delete for authenticated users based on email" ON public.posts; RAISE NOTICE 'Dropped existing policies on posts table'; END IF; -- Create new policies CREATE POLICY "Enable read access for all users" ON public.posts FOR SELECT USING (published = true); CREATE POLICY "Enable insert for authenticated users only" ON public.posts FOR INSERT WITH CHECK (auth.uid() IS NOT NULL); CREATE POLICY "Enable update for authenticated users based on email" ON public.posts FOR UPDATE USING (auth.uid() IS NOT NULL); CREATE POLICY "Enable delete for authenticated users based on email" ON public.posts FOR DELETE USING (auth.uid() IS NOT NULL); RAISE NOTICE 'Created new policies on posts table'; ELSE RAISE NOTICE 'posts table does not exist'; END IF; END $$; -- Step 5: Update RLS policies for the post_categories table DO $$ DECLARE policy_exists BOOLEAN; BEGIN -- Check if the table exists IF table_exists('post_categories') THEN -- Check for existing policies SELECT EXISTS ( SELECT 1 FROM pg_policies WHERE tablename = 'post_categories' 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.post_categories; DROP POLICY IF EXISTS "Enable insert for authenticated users only" ON public.post_categories; DROP POLICY IF EXISTS "Enable update for authenticated users based on email" ON public.post_categories; DROP POLICY IF EXISTS "Enable delete for authenticated users based on email" ON public.post_categories; RAISE NOTICE 'Dropped existing policies on post_categories table'; END IF; -- Create new policies CREATE POLICY "Enable read access for all users" ON public.post_categories FOR SELECT USING (true); CREATE POLICY "Enable insert for authenticated users only" ON public.post_categories FOR INSERT WITH CHECK (auth.uid() IS NOT NULL); CREATE POLICY "Enable update for authenticated users based on email" ON public.post_categories FOR UPDATE USING (auth.uid() IS NOT NULL); CREATE POLICY "Enable delete for authenticated users based on email" ON public.post_categories FOR DELETE USING (auth.uid() IS NOT NULL); RAISE NOTICE 'Created new policies on post_categories table'; ELSE RAISE NOTICE 'post_categories table does not exist'; END IF; END $$; -- Step 6: Update foreign key constraints DO $$ BEGIN -- Check if the post_categories table exists IF table_exists('post_categories') THEN -- Check if the post_id column exists IF EXISTS ( SELECT 1 FROM information_schema.columns WHERE table_schema = 'public' AND table_name = 'post_categories' AND column_name = 'post_id' ) THEN -- Check if the constraint exists IF EXISTS ( SELECT 1 FROM information_schema.table_constraints WHERE constraint_schema = 'public' AND table_name = 'post_categories' AND constraint_type = 'FOREIGN KEY' AND constraint_name LIKE '%blog_post_id%' ) THEN -- Drop the old constraint ALTER TABLE public.post_categories DROP CONSTRAINT IF EXISTS post_categories_blog_post_id_fkey; RAISE NOTICE 'Dropped old foreign key constraint'; -- Add the new constraint ALTER TABLE public.post_categories ADD CONSTRAINT post_categories_post_id_fkey FOREIGN KEY (post_id) REFERENCES public.posts(id) ON DELETE CASCADE; RAISE NOTICE 'Added new foreign key constraint'; ELSE RAISE NOTICE 'Foreign key constraint does not exist or is already updated'; END IF; ELSE RAISE NOTICE 'post_id column does not exist in post_categories table'; END IF; ELSE RAISE NOTICE 'post_categories table does not exist'; END IF; END $$; -- Step 7: Verify the changes SELECT table_name, column_name FROM information_schema.columns WHERE table_schema = 'public' AND table_name IN ('posts', 'post_categories') ORDER BY table_name, ordinal_position; -- Show a sample post with its categories SELECT p.title, array_agg(c.name) as categories FROM posts p LEFT JOIN post_categories pc ON pc.post_id = p.id LEFT JOIN categories c ON c.id = pc.category_id GROUP BY p.id, p.title LIMIT 5;