AI Resource Pro / website / scripts / migrate-categories-schema.sql
| 1 | -- Migration SQL for updating categories and news tables |
| 2 | -- Run this in your Supabase SQL editor to update an existing database |
| 3 | |
| 4 | -- 1. Add slug field to categories if it doesn't exist |
| 5 | ALTER TABLE public.categories |
| 6 | ADD COLUMN IF NOT EXISTS slug TEXT; |
| 7 | |
| 8 | -- 2. Add description field to categories if it doesn't exist |
| 9 | ALTER TABLE public.categories |
| 10 | ADD COLUMN IF NOT EXISTS description TEXT; |
| 11 | |
| 12 | -- 3. Handle case where both LLMs and Large Language Models categories exist |
| 13 | -- This will merge them by moving all tools from Large Language Models to LLMs |
| 14 | DO $$ |
| 15 | DECLARE |
| 16 | llms_id BIGINT; |
| 17 | large_lm_id BIGINT; |
| 18 | large_lm_count INTEGER; |
| 19 | BEGIN |
| 20 | -- Get IDs of both categories if they exist |
| 21 | SELECT id INTO llms_id FROM public.categories WHERE name = 'LLMs'; |
| 22 | SELECT id INTO large_lm_id FROM public.categories WHERE name = 'Large Language Models' OR name = 'Large Language Model'; |
| 23 | |
| 24 | -- If both exist, we need to merge them |
| 25 | IF llms_id IS NOT NULL AND large_lm_id IS NOT NULL THEN |
| 26 | -- Get tool count from the large language models category |
| 27 | SELECT tool_count INTO large_lm_count FROM public.categories WHERE id = large_lm_id; |
| 28 | |
| 29 | -- Update all tools that reference Large Language Models to point to LLMs |
| 30 | UPDATE public.tools |
| 31 | SET category_id = llms_id |
| 32 | WHERE category_id = large_lm_id; |
| 33 | |
| 34 | -- Update all news that reference Large Language Models to point to LLMs |
| 35 | UPDATE public.news |
| 36 | SET category_id = llms_id |
| 37 | WHERE category_id = large_lm_id; |
| 38 | |
| 39 | -- Update the tool count for LLMs to include the tools from Large Language Models |
| 40 | UPDATE public.categories |
| 41 | SET tool_count = tool_count + large_lm_count |
| 42 | WHERE id = llms_id; |
| 43 | |
| 44 | -- Delete the now-empty Large Language Models category |
| 45 | DELETE FROM public.categories WHERE id = large_lm_id; |
| 46 | |
| 47 | RAISE NOTICE 'Merged Large Language Models category into LLMs category'; |
| 48 | -- If only Large Language Models exists, rename it |
| 49 | ELSIF llms_id IS NULL AND large_lm_id IS NOT NULL THEN |
| 50 | UPDATE public.categories |
| 51 | SET name = 'LLMs' |
| 52 | WHERE id = large_lm_id; |
| 53 | |
| 54 | RAISE NOTICE 'Renamed Large Language Models category to LLMs'; |
| 55 | END IF; |
| 56 | END $$; |
| 57 | |
| 58 | -- 4. Generate slugs for existing categories |
| 59 | -- This uses a function to create slugs from names |
| 60 | DO $$ |
| 61 | DECLARE |
| 62 | cat RECORD; |
| 63 | BEGIN |
| 64 | FOR cat IN SELECT id, name FROM public.categories |
| 65 | LOOP |
| 66 | -- Generate slug from name (lowercase, replace non-alphanumeric with dash) |
| 67 | UPDATE public.categories |
| 68 | SET slug = LOWER(REGEXP_REPLACE(cat.name, '[^a-zA-Z0-9]+', '-', 'g')) |
| 69 | WHERE id = cat.id; |
| 70 | END LOOP; |
| 71 | END $$; |
| 72 | |
| 73 | -- 5. Add NOT NULL constraint to slug after filling data |
| 74 | ALTER TABLE public.categories |
| 75 | ALTER COLUMN slug SET NOT NULL; |
| 76 | |
| 77 | -- 6. Add unique constraints to name and slug if they don't exist |
| 78 | DO $$ |
| 79 | BEGIN |
| 80 | -- Check if name constraint exists before dropping |
| 81 | IF EXISTS ( |
| 82 | SELECT 1 |
| 83 | FROM information_schema.table_constraints |
| 84 | WHERE constraint_name = 'categories_name_key' |
| 85 | ) THEN |
| 86 | ALTER TABLE public.categories |
| 87 | DROP CONSTRAINT categories_name_key; |
| 88 | END IF; |
| 89 | |
| 90 | -- Add unique constraint on name if it doesn't exist |
| 91 | IF NOT EXISTS ( |
| 92 | SELECT 1 |
| 93 | FROM information_schema.table_constraints |
| 94 | WHERE constraint_name = 'categories_name_key' |
| 95 | ) THEN |
| 96 | ALTER TABLE public.categories |
| 97 | ADD CONSTRAINT categories_name_key UNIQUE (name); |
| 98 | END IF; |
| 99 | |
| 100 | -- Add unique constraint on slug if it doesn't exist |
| 101 | IF NOT EXISTS ( |
| 102 | SELECT 1 |
| 103 | FROM information_schema.table_constraints |
| 104 | WHERE constraint_name = 'categories_slug_key' |
| 105 | ) THEN |
| 106 | ALTER TABLE public.categories |
| 107 | ADD CONSTRAINT categories_slug_key UNIQUE (slug); |
| 108 | END IF; |
| 109 | END $$; |
| 110 | |
| 111 | -- 7. Change tool_count to have a default of 0 |
| 112 | ALTER TABLE public.categories |
| 113 | ALTER COLUMN tool_count SET DEFAULT 0; |
| 114 | |
| 115 | -- 8. Update news table to reference categories |
| 116 | -- First add category_id column if it doesn't exist |
| 117 | ALTER TABLE public.news |
| 118 | ADD COLUMN IF NOT EXISTS category_id BIGINT; |
| 119 | |
| 120 | -- 9. Rename the existing category column to category_name for clarity |
| 121 | -- Only if the column is currently named 'category' |
| 122 | DO $$ |
| 123 | BEGIN |
| 124 | IF EXISTS ( |
| 125 | SELECT 1 |
| 126 | FROM information_schema.columns |
| 127 | WHERE table_name = 'news' |
| 128 | AND column_name = 'category' |
| 129 | ) THEN |
| 130 | ALTER TABLE public.news |
| 131 | RENAME COLUMN category TO category_name; |
| 132 | END IF; |
| 133 | END $$; |
| 134 | |
| 135 | -- 10. Add foreign key constraint to category_id if it doesn't exist |
| 136 | DO $$ |
| 137 | BEGIN |
| 138 | IF NOT EXISTS ( |
| 139 | SELECT 1 |
| 140 | FROM information_schema.table_constraints |
| 141 | WHERE constraint_name = 'news_category_id_fkey' |
| 142 | ) THEN |
| 143 | ALTER TABLE public.news |
| 144 | ADD CONSTRAINT news_category_id_fkey |
| 145 | FOREIGN KEY (category_id) REFERENCES public.categories(id); |
| 146 | END IF; |
| 147 | END $$; |
| 148 | |
| 149 | -- 11. Update existing news items to link to categories where possible |
| 150 | -- This requires that categories exist with matching names |
| 151 | DO $$ |
| 152 | DECLARE |
| 153 | news_item RECORD; |
| 154 | cat_id BIGINT; |
| 155 | BEGIN |
| 156 | FOR news_item IN SELECT id, category_name FROM public.news WHERE category_id IS NULL |
| 157 | LOOP |
| 158 | -- Try to find matching category |
| 159 | SELECT id INTO cat_id FROM public.categories |
| 160 | WHERE name = news_item.category_name; |
| 161 | |
| 162 | -- If not found, try normalized versions (example for Large Language Model) |
| 163 | IF cat_id IS NULL AND (news_item.category_name = 'Large Language Model' OR news_item.category_name = 'Large Language Models') THEN |
| 164 | SELECT id INTO cat_id FROM public.categories |
| 165 | WHERE name = 'LLMs'; |
| 166 | END IF; |
| 167 | |
| 168 | -- Update if category found |
| 169 | IF cat_id IS NOT NULL THEN |
| 170 | UPDATE public.news SET category_id = cat_id WHERE id = news_item.id; |
| 171 | END IF; |
| 172 | END LOOP; |
| 173 | END $$; |