-- Migration SQL for updating categories and news tables -- Run this in your Supabase SQL editor to update an existing database -- 1. Add slug field to categories if it doesn't exist ALTER TABLE public.categories ADD COLUMN IF NOT EXISTS slug TEXT; -- 2. Add description field to categories if it doesn't exist ALTER TABLE public.categories ADD COLUMN IF NOT EXISTS description TEXT; -- 3. Handle case where both LLMs and Large Language Models categories exist -- This will merge them by moving all tools from Large Language Models to LLMs DO $$ DECLARE llms_id BIGINT; large_lm_id BIGINT; large_lm_count INTEGER; BEGIN -- Get IDs of both categories if they exist SELECT id INTO llms_id FROM public.categories WHERE name = 'LLMs'; SELECT id INTO large_lm_id FROM public.categories WHERE name = 'Large Language Models' OR name = 'Large Language Model'; -- If both exist, we need to merge them IF llms_id IS NOT NULL AND large_lm_id IS NOT NULL THEN -- Get tool count from the large language models category SELECT tool_count INTO large_lm_count FROM public.categories WHERE id = large_lm_id; -- Update all tools that reference Large Language Models to point to LLMs UPDATE public.tools SET category_id = llms_id WHERE category_id = large_lm_id; -- Update all news that reference Large Language Models to point to LLMs UPDATE public.news SET category_id = llms_id WHERE category_id = large_lm_id; -- Update the tool count for LLMs to include the tools from Large Language Models UPDATE public.categories SET tool_count = tool_count + large_lm_count WHERE id = llms_id; -- Delete the now-empty Large Language Models category DELETE FROM public.categories WHERE id = large_lm_id; RAISE NOTICE 'Merged Large Language Models category into LLMs category'; -- If only Large Language Models exists, rename it ELSIF llms_id IS NULL AND large_lm_id IS NOT NULL THEN UPDATE public.categories SET name = 'LLMs' WHERE id = large_lm_id; RAISE NOTICE 'Renamed Large Language Models category to LLMs'; END IF; END $$; -- 4. Generate slugs for existing categories -- This uses a function to create slugs from names DO $$ DECLARE cat RECORD; BEGIN FOR cat IN SELECT id, name FROM public.categories LOOP -- Generate slug from name (lowercase, replace non-alphanumeric with dash) UPDATE public.categories SET slug = LOWER(REGEXP_REPLACE(cat.name, '[^a-zA-Z0-9]+', '-', 'g')) WHERE id = cat.id; END LOOP; END $$; -- 5. Add NOT NULL constraint to slug after filling data ALTER TABLE public.categories ALTER COLUMN slug SET NOT NULL; -- 6. Add unique constraints to name and slug if they don't exist DO $$ BEGIN -- Check if name constraint exists before dropping IF EXISTS ( SELECT 1 FROM information_schema.table_constraints WHERE constraint_name = 'categories_name_key' ) THEN ALTER TABLE public.categories DROP CONSTRAINT categories_name_key; END IF; -- Add unique constraint on name if it doesn't exist IF NOT EXISTS ( SELECT 1 FROM information_schema.table_constraints WHERE constraint_name = 'categories_name_key' ) THEN ALTER TABLE public.categories ADD CONSTRAINT categories_name_key UNIQUE (name); END IF; -- Add unique constraint on slug if it doesn't exist IF NOT EXISTS ( SELECT 1 FROM information_schema.table_constraints WHERE constraint_name = 'categories_slug_key' ) THEN ALTER TABLE public.categories ADD CONSTRAINT categories_slug_key UNIQUE (slug); END IF; END $$; -- 7. Change tool_count to have a default of 0 ALTER TABLE public.categories ALTER COLUMN tool_count SET DEFAULT 0; -- 8. Update news table to reference categories -- First add category_id column if it doesn't exist ALTER TABLE public.news ADD COLUMN IF NOT EXISTS category_id BIGINT; -- 9. Rename the existing category column to category_name for clarity -- Only if the column is currently named 'category' DO $$ BEGIN IF EXISTS ( SELECT 1 FROM information_schema.columns WHERE table_name = 'news' AND column_name = 'category' ) THEN ALTER TABLE public.news RENAME COLUMN category TO category_name; END IF; END $$; -- 10. Add foreign key constraint to category_id if it doesn't exist DO $$ BEGIN IF NOT EXISTS ( SELECT 1 FROM information_schema.table_constraints WHERE constraint_name = 'news_category_id_fkey' ) THEN ALTER TABLE public.news ADD CONSTRAINT news_category_id_fkey FOREIGN KEY (category_id) REFERENCES public.categories(id); END IF; END $$; -- 11. Update existing news items to link to categories where possible -- This requires that categories exist with matching names DO $$ DECLARE news_item RECORD; cat_id BIGINT; BEGIN FOR news_item IN SELECT id, category_name FROM public.news WHERE category_id IS NULL LOOP -- Try to find matching category SELECT id INTO cat_id FROM public.categories WHERE name = news_item.category_name; -- If not found, try normalized versions (example for Large Language Model) IF cat_id IS NULL AND (news_item.category_name = 'Large Language Model' OR news_item.category_name = 'Large Language Models') THEN SELECT id INTO cat_id FROM public.categories WHERE name = 'LLMs'; END IF; -- Update if category found IF cat_id IS NOT NULL THEN UPDATE public.news SET category_id = cat_id WHERE id = news_item.id; END IF; END LOOP; END $$;