AI Resource Pro / website / scripts / migrate-blog-tables.sql
| 1 | -- Migration script to rename blog_posts and blog_posts_categories tables |
| 2 | -- Run this in your Supabase SQL Editor |
| 3 | |
| 4 | -- First, create helper functions if they don't exist |
| 5 | CREATE OR REPLACE FUNCTION table_exists( |
| 6 | p_table TEXT |
| 7 | ) RETURNS BOOLEAN AS $$ |
| 8 | DECLARE |
| 9 | v_exists BOOLEAN; |
| 10 | BEGIN |
| 11 | SELECT EXISTS ( |
| 12 | SELECT 1 |
| 13 | FROM information_schema.tables |
| 14 | WHERE table_schema = 'public' |
| 15 | AND table_name = p_table |
| 16 | ) INTO v_exists; |
| 17 | |
| 18 | RETURN v_exists; |
| 19 | END; |
| 20 | $$ LANGUAGE plpgsql; |
| 21 | |
| 22 | -- Step 1: Rename the blog_posts table to posts if it exists |
| 23 | DO $$ |
| 24 | BEGIN |
| 25 | IF table_exists('blog_posts') AND NOT table_exists('posts') THEN |
| 26 | ALTER TABLE public.blog_posts RENAME TO posts; |
| 27 | RAISE NOTICE 'Renamed blog_posts table to posts'; |
| 28 | ELSIF NOT table_exists('blog_posts') THEN |
| 29 | RAISE NOTICE 'blog_posts table does not exist'; |
| 30 | ELSIF table_exists('posts') THEN |
| 31 | RAISE NOTICE 'posts table already exists'; |
| 32 | END IF; |
| 33 | END $$; |
| 34 | |
| 35 | -- Step 2: Rename the blog_posts_categories table to post_categories if it exists |
| 36 | DO $$ |
| 37 | BEGIN |
| 38 | IF table_exists('blog_posts_categories') AND NOT table_exists('post_categories') THEN |
| 39 | ALTER TABLE public.blog_posts_categories RENAME TO post_categories; |
| 40 | RAISE NOTICE 'Renamed blog_posts_categories table to post_categories'; |
| 41 | ELSIF NOT table_exists('blog_posts_categories') THEN |
| 42 | RAISE NOTICE 'blog_posts_categories table does not exist'; |
| 43 | ELSIF table_exists('post_categories') THEN |
| 44 | RAISE NOTICE 'post_categories table already exists'; |
| 45 | END IF; |
| 46 | END $$; |
| 47 | |
| 48 | -- Step 3: Rename the column blog_post_id to post_id in the post_categories table |
| 49 | DO $$ |
| 50 | BEGIN |
| 51 | IF table_exists('post_categories') THEN |
| 52 | -- Check if the column blog_post_id exists |
| 53 | IF EXISTS ( |
| 54 | SELECT 1 |
| 55 | FROM information_schema.columns |
| 56 | WHERE table_schema = 'public' |
| 57 | AND table_name = 'post_categories' |
| 58 | AND column_name = 'blog_post_id' |
| 59 | ) THEN |
| 60 | ALTER TABLE public.post_categories RENAME COLUMN blog_post_id TO post_id; |
| 61 | RAISE NOTICE 'Renamed blog_post_id to post_id in post_categories table'; |
| 62 | ELSE |
| 63 | RAISE NOTICE 'blog_post_id column does not exist in post_categories table'; |
| 64 | END IF; |
| 65 | ELSE |
| 66 | RAISE NOTICE 'post_categories table does not exist'; |
| 67 | END IF; |
| 68 | END $$; |
| 69 | |
| 70 | -- Step 4: Update RLS policies for the posts table |
| 71 | DO $$ |
| 72 | DECLARE |
| 73 | policy_exists BOOLEAN; |
| 74 | BEGIN |
| 75 | -- Check if the table exists |
| 76 | IF table_exists('posts') THEN |
| 77 | -- Check for existing policies |
| 78 | SELECT EXISTS ( |
| 79 | SELECT 1 |
| 80 | FROM pg_policies |
| 81 | WHERE tablename = 'posts' |
| 82 | AND schemaname = 'public' |
| 83 | ) INTO policy_exists; |
| 84 | |
| 85 | -- Drop existing policies if they exist |
| 86 | IF policy_exists THEN |
| 87 | DROP POLICY IF EXISTS "Enable read access for all users" ON public.posts; |
| 88 | DROP POLICY IF EXISTS "Enable insert for authenticated users only" ON public.posts; |
| 89 | DROP POLICY IF EXISTS "Enable update for authenticated users based on email" ON public.posts; |
| 90 | DROP POLICY IF EXISTS "Enable delete for authenticated users based on email" ON public.posts; |
| 91 | RAISE NOTICE 'Dropped existing policies on posts table'; |
| 92 | END IF; |
| 93 | |
| 94 | -- Create new policies |
| 95 | CREATE POLICY "Enable read access for all users" ON public.posts |
| 96 | FOR SELECT USING (published = true); |
| 97 | |
| 98 | CREATE POLICY "Enable insert for authenticated users only" ON public.posts |
| 99 | FOR INSERT WITH CHECK (auth.uid() IS NOT NULL); |
| 100 | |
| 101 | CREATE POLICY "Enable update for authenticated users based on email" ON public.posts |
| 102 | FOR UPDATE USING (auth.uid() IS NOT NULL); |
| 103 | |
| 104 | CREATE POLICY "Enable delete for authenticated users based on email" ON public.posts |
| 105 | FOR DELETE USING (auth.uid() IS NOT NULL); |
| 106 | |
| 107 | RAISE NOTICE 'Created new policies on posts table'; |
| 108 | ELSE |
| 109 | RAISE NOTICE 'posts table does not exist'; |
| 110 | END IF; |
| 111 | END $$; |
| 112 | |
| 113 | -- Step 5: Update RLS policies for the post_categories table |
| 114 | DO $$ |
| 115 | DECLARE |
| 116 | policy_exists BOOLEAN; |
| 117 | BEGIN |
| 118 | -- Check if the table exists |
| 119 | IF table_exists('post_categories') THEN |
| 120 | -- Check for existing policies |
| 121 | SELECT EXISTS ( |
| 122 | SELECT 1 |
| 123 | FROM pg_policies |
| 124 | WHERE tablename = 'post_categories' |
| 125 | AND schemaname = 'public' |
| 126 | ) INTO policy_exists; |
| 127 | |
| 128 | -- Drop existing policies if they exist |
| 129 | IF policy_exists THEN |
| 130 | DROP POLICY IF EXISTS "Enable read access for all users" ON public.post_categories; |
| 131 | DROP POLICY IF EXISTS "Enable insert for authenticated users only" ON public.post_categories; |
| 132 | DROP POLICY IF EXISTS "Enable update for authenticated users based on email" ON public.post_categories; |
| 133 | DROP POLICY IF EXISTS "Enable delete for authenticated users based on email" ON public.post_categories; |
| 134 | RAISE NOTICE 'Dropped existing policies on post_categories table'; |
| 135 | END IF; |
| 136 | |
| 137 | -- Create new policies |
| 138 | CREATE POLICY "Enable read access for all users" ON public.post_categories |
| 139 | FOR SELECT USING (true); |
| 140 | |
| 141 | CREATE POLICY "Enable insert for authenticated users only" ON public.post_categories |
| 142 | FOR INSERT WITH CHECK (auth.uid() IS NOT NULL); |
| 143 | |
| 144 | CREATE POLICY "Enable update for authenticated users based on email" ON public.post_categories |
| 145 | FOR UPDATE USING (auth.uid() IS NOT NULL); |
| 146 | |
| 147 | CREATE POLICY "Enable delete for authenticated users based on email" ON public.post_categories |
| 148 | FOR DELETE USING (auth.uid() IS NOT NULL); |
| 149 | |
| 150 | RAISE NOTICE 'Created new policies on post_categories table'; |
| 151 | ELSE |
| 152 | RAISE NOTICE 'post_categories table does not exist'; |
| 153 | END IF; |
| 154 | END $$; |
| 155 | |
| 156 | -- Step 6: Update foreign key constraints |
| 157 | DO $$ |
| 158 | BEGIN |
| 159 | -- Check if the post_categories table exists |
| 160 | IF table_exists('post_categories') THEN |
| 161 | -- Check if the post_id column exists |
| 162 | IF EXISTS ( |
| 163 | SELECT 1 |
| 164 | FROM information_schema.columns |
| 165 | WHERE table_schema = 'public' |
| 166 | AND table_name = 'post_categories' |
| 167 | AND column_name = 'post_id' |
| 168 | ) THEN |
| 169 | -- Check if the constraint exists |
| 170 | IF EXISTS ( |
| 171 | SELECT 1 |
| 172 | FROM information_schema.table_constraints |
| 173 | WHERE constraint_schema = 'public' |
| 174 | AND table_name = 'post_categories' |
| 175 | AND constraint_type = 'FOREIGN KEY' |
| 176 | AND constraint_name LIKE '%blog_post_id%' |
| 177 | ) THEN |
| 178 | -- Drop the old constraint |
| 179 | ALTER TABLE public.post_categories DROP CONSTRAINT IF EXISTS post_categories_blog_post_id_fkey; |
| 180 | RAISE NOTICE 'Dropped old foreign key constraint'; |
| 181 | |
| 182 | -- Add the new constraint |
| 183 | ALTER TABLE public.post_categories |
| 184 | ADD CONSTRAINT post_categories_post_id_fkey |
| 185 | FOREIGN KEY (post_id) |
| 186 | REFERENCES public.posts(id) |
| 187 | ON DELETE CASCADE; |
| 188 | RAISE NOTICE 'Added new foreign key constraint'; |
| 189 | ELSE |
| 190 | RAISE NOTICE 'Foreign key constraint does not exist or is already updated'; |
| 191 | END IF; |
| 192 | ELSE |
| 193 | RAISE NOTICE 'post_id column does not exist in post_categories table'; |
| 194 | END IF; |
| 195 | ELSE |
| 196 | RAISE NOTICE 'post_categories table does not exist'; |
| 197 | END IF; |
| 198 | END $$; |
| 199 | |
| 200 | -- Step 7: Verify the changes |
| 201 | SELECT table_name, column_name |
| 202 | FROM information_schema.columns |
| 203 | WHERE table_schema = 'public' |
| 204 | AND table_name IN ('posts', 'post_categories') |
| 205 | ORDER BY table_name, ordinal_position; |
| 206 | |
| 207 | -- Show a sample post with its categories |
| 208 | SELECT |
| 209 | p.title, |
| 210 | array_agg(c.name) as categories |
| 211 | FROM posts p |
| 212 | LEFT JOIN post_categories pc ON pc.post_id = p.id |
| 213 | LEFT JOIN categories c ON c.id = pc.category_id |
| 214 | GROUP BY p.id, p.title |
| 215 | LIMIT 5; |