AI Resource Pro / website / scripts / add-website-column.sql
| 1 | -- Schema Update: Fix website column in tools table |
| 2 | -- This script ensures that the tools table has a 'website' column (not 'url') |
| 3 | -- to match the application code expectations |
| 4 | |
| 5 | -- First check if the table exists |
| 6 | DO $$ |
| 7 | BEGIN |
| 8 | IF NOT EXISTS ( |
| 9 | SELECT FROM information_schema.tables |
| 10 | WHERE table_name = 'tools' AND table_schema = 'public' |
| 11 | ) THEN |
| 12 | RAISE EXCEPTION 'Table public.tools does not exist!'; |
| 13 | END IF; |
| 14 | END |
| 15 | $$; |
| 16 | |
| 17 | -- Step 1: Check for URL column and rename it to website if it exists |
| 18 | DO $$ |
| 19 | BEGIN |
| 20 | IF EXISTS ( |
| 21 | SELECT FROM information_schema.columns |
| 22 | WHERE table_name = 'tools' AND column_name = 'url' |
| 23 | ) THEN |
| 24 | -- Check if website column already exists |
| 25 | IF EXISTS ( |
| 26 | SELECT FROM information_schema.columns |
| 27 | WHERE table_name = 'tools' AND column_name = 'website' |
| 28 | ) THEN |
| 29 | -- Both columns exist, copy data from url to website where website is null |
| 30 | UPDATE public.tools SET website = url WHERE website IS NULL; |
| 31 | RAISE NOTICE 'Copied data from url column to website column where website was null'; |
| 32 | |
| 33 | -- Drop the url column since we now have website with all the data |
| 34 | ALTER TABLE public.tools DROP COLUMN url; |
| 35 | RAISE NOTICE 'Dropped url column as website column now contains all data'; |
| 36 | ELSE |
| 37 | -- Only url exists, we can rename it to website |
| 38 | ALTER TABLE public.tools RENAME COLUMN url TO website; |
| 39 | RAISE NOTICE 'Renamed url column to website'; |
| 40 | END IF; |
| 41 | ELSIF NOT EXISTS ( |
| 42 | SELECT FROM information_schema.columns |
| 43 | WHERE table_name = 'tools' AND column_name = 'website' |
| 44 | ) THEN |
| 45 | -- Neither url nor website exists, add website column |
| 46 | ALTER TABLE public.tools ADD COLUMN website TEXT; |
| 47 | RAISE NOTICE 'Added website column to tools table'; |
| 48 | ELSE |
| 49 | RAISE NOTICE 'Website column already exists in tools table'; |
| 50 | END IF; |
| 51 | END |
| 52 | $$; |
| 53 | |
| 54 | -- Step 2: Ensure the website column has the correct data type |
| 55 | DO $$ |
| 56 | BEGIN |
| 57 | -- Get the current data type of the website column |
| 58 | DECLARE |
| 59 | column_type TEXT; |
| 60 | BEGIN |
| 61 | SELECT data_type INTO column_type |
| 62 | FROM information_schema.columns |
| 63 | WHERE table_name = 'tools' AND column_name = 'website'; |
| 64 | |
| 65 | -- If it's not TEXT, alter it |
| 66 | IF column_type != 'text' THEN |
| 67 | ALTER TABLE public.tools ALTER COLUMN website TYPE TEXT; |
| 68 | RAISE NOTICE 'Changed website column type to TEXT'; |
| 69 | END IF; |
| 70 | END; |
| 71 | END |
| 72 | $$; |
| 73 | |
| 74 | -- Step 3: Refresh the PostgREST schema cache |
| 75 | SELECT pg_notify('pgrst', 'reload schema'); |
| 76 | |
| 77 | -- Step 4: Verify the column changes |
| 78 | SELECT column_name, data_type |
| 79 | FROM information_schema.columns |
| 80 | WHERE table_name = 'tools' AND column_name = 'website'; |
| 81 | |
| 82 | -- Output a final confirmation message |
| 83 | DO $$ |
| 84 | BEGIN |
| 85 | RAISE NOTICE '----------------------------------------'; |
| 86 | RAISE NOTICE 'Schema update completed for tools table'; |
| 87 | RAISE NOTICE 'The tools table now has a website column'; |
| 88 | RAISE NOTICE 'Any former url column has been renamed or its data migrated'; |
| 89 | RAISE NOTICE '----------------------------------------'; |
| 90 | END |
| 91 | $$; |