-- Schema Update: Fix website column in tools table -- This script ensures that the tools table has a 'website' column (not 'url') -- to match the application code expectations -- First check if the table exists DO $$ BEGIN IF NOT EXISTS ( SELECT FROM information_schema.tables WHERE table_name = 'tools' AND table_schema = 'public' ) THEN RAISE EXCEPTION 'Table public.tools does not exist!'; END IF; END $$; -- Step 1: Check for URL column and rename it to website if it exists DO $$ BEGIN IF EXISTS ( SELECT FROM information_schema.columns WHERE table_name = 'tools' AND column_name = 'url' ) THEN -- Check if website column already exists IF EXISTS ( SELECT FROM information_schema.columns WHERE table_name = 'tools' AND column_name = 'website' ) THEN -- Both columns exist, copy data from url to website where website is null UPDATE public.tools SET website = url WHERE website IS NULL; RAISE NOTICE 'Copied data from url column to website column where website was null'; -- Drop the url column since we now have website with all the data ALTER TABLE public.tools DROP COLUMN url; RAISE NOTICE 'Dropped url column as website column now contains all data'; ELSE -- Only url exists, we can rename it to website ALTER TABLE public.tools RENAME COLUMN url TO website; RAISE NOTICE 'Renamed url column to website'; END IF; ELSIF NOT EXISTS ( SELECT FROM information_schema.columns WHERE table_name = 'tools' AND column_name = 'website' ) THEN -- Neither url nor website exists, add website column ALTER TABLE public.tools ADD COLUMN website TEXT; RAISE NOTICE 'Added website column to tools table'; ELSE RAISE NOTICE 'Website column already exists in tools table'; END IF; END $$; -- Step 2: Ensure the website column has the correct data type DO $$ BEGIN -- Get the current data type of the website column DECLARE column_type TEXT; BEGIN SELECT data_type INTO column_type FROM information_schema.columns WHERE table_name = 'tools' AND column_name = 'website'; -- If it's not TEXT, alter it IF column_type != 'text' THEN ALTER TABLE public.tools ALTER COLUMN website TYPE TEXT; RAISE NOTICE 'Changed website column type to TEXT'; END IF; END; END $$; -- Step 3: Refresh the PostgREST schema cache SELECT pg_notify('pgrst', 'reload schema'); -- Step 4: Verify the column changes SELECT column_name, data_type FROM information_schema.columns WHERE table_name = 'tools' AND column_name = 'website'; -- Output a final confirmation message DO $$ BEGIN RAISE NOTICE '----------------------------------------'; RAISE NOTICE 'Schema update completed for tools table'; RAISE NOTICE 'The tools table now has a website column'; RAISE NOTICE 'Any former url column has been renamed or its data migrated'; RAISE NOTICE '----------------------------------------'; END $$;