AI Resource Pro / website / scripts / fix-news-published-field.sql
| 1 | -- Fix News Published Field |
| 2 | -- This script fixes any issues with the published field in the news table |
| 3 | |
| 4 | -- First, check if the published column exists |
| 5 | DO $$ |
| 6 | BEGIN |
| 7 | IF NOT EXISTS ( |
| 8 | SELECT 1 |
| 9 | FROM information_schema.columns |
| 10 | WHERE table_name = 'news' |
| 11 | AND column_name = 'published' |
| 12 | ) THEN |
| 13 | -- Add the published column if it doesn't exist |
| 14 | ALTER TABLE public.news |
| 15 | ADD COLUMN published BOOLEAN NOT NULL DEFAULT true; |
| 16 | |
| 17 | RAISE NOTICE 'Added published column to news table'; |
| 18 | ELSE |
| 19 | RAISE NOTICE 'Published column already exists in news table'; |
| 20 | |
| 21 | -- Check the data type of the published column |
| 22 | DECLARE |
| 23 | column_type TEXT; |
| 24 | BEGIN |
| 25 | SELECT data_type INTO column_type |
| 26 | FROM information_schema.columns |
| 27 | WHERE table_name = 'news' |
| 28 | AND column_name = 'published'; |
| 29 | |
| 30 | RAISE NOTICE 'Published column type: %', column_type; |
| 31 | |
| 32 | -- If it's not a boolean, alter it |
| 33 | IF column_type <> 'boolean' THEN |
| 34 | ALTER TABLE public.news |
| 35 | ALTER COLUMN published TYPE BOOLEAN USING (published::boolean); |
| 36 | |
| 37 | RAISE NOTICE 'Changed published column type to BOOLEAN'; |
| 38 | END IF; |
| 39 | END; |
| 40 | |
| 41 | -- Drop any existing constraints that might be preventing updates |
| 42 | BEGIN |
| 43 | ALTER TABLE public.news DROP CONSTRAINT IF EXISTS news_published_check; |
| 44 | RAISE NOTICE 'Dropped any check constraints on published column'; |
| 45 | EXCEPTION |
| 46 | WHEN others THEN |
| 47 | RAISE NOTICE 'No check constraints to drop'; |
| 48 | END; |
| 49 | END IF; |
| 50 | END $$; |
| 51 | |
| 52 | -- Fix existing news records to ensure they have a valid published value |
| 53 | UPDATE public.news |
| 54 | SET published = true |
| 55 | WHERE published IS NULL; |
| 56 | |
| 57 | -- Double-check that RLS policies allow updating the published field |
| 58 | DO $$ |
| 59 | BEGIN |
| 60 | -- Check for existing policies |
| 61 | IF EXISTS ( |
| 62 | SELECT 1 |
| 63 | FROM pg_policies |
| 64 | WHERE tablename = 'news' |
| 65 | AND policyname = 'Admins can update news published status' |
| 66 | ) THEN |
| 67 | DROP POLICY "Admins can update news published status" ON public.news; |
| 68 | RAISE NOTICE 'Dropped existing policy for updating published status'; |
| 69 | END IF; |
| 70 | |
| 71 | -- Create a specific policy for the published field |
| 72 | CREATE POLICY "Admins can update news published status" ON public.news |
| 73 | FOR UPDATE |
| 74 | USING ( |
| 75 | EXISTS ( |
| 76 | SELECT 1 FROM public.users WHERE id = auth.uid() AND is_admin = true |
| 77 | ) |
| 78 | ) |
| 79 | WITH CHECK ( |
| 80 | EXISTS ( |
| 81 | SELECT 1 FROM public.users WHERE id = auth.uid() AND is_admin = true |
| 82 | ) |
| 83 | ); |
| 84 | |
| 85 | RAISE NOTICE 'Created policy for updating published status'; |
| 86 | END $$; |
| 87 | |
| 88 | -- Create a function to test updating the published field |
| 89 | CREATE OR REPLACE FUNCTION test_update_news_published() |
| 90 | RETURNS TABLE (success boolean, message text) |
| 91 | LANGUAGE plpgsql |
| 92 | SECURITY DEFINER |
| 93 | AS $$ |
| 94 | DECLARE |
| 95 | test_id uuid; |
| 96 | result boolean; |
| 97 | BEGIN |
| 98 | -- Get a random news ID to test with |
| 99 | SELECT id INTO test_id FROM public.news LIMIT 1; |
| 100 | |
| 101 | IF test_id IS NULL THEN |
| 102 | RETURN QUERY SELECT false::boolean, 'No news records found for testing'::text; |
| 103 | RETURN; |
| 104 | END IF; |
| 105 | |
| 106 | -- Get the current published status |
| 107 | SELECT published INTO result FROM public.news WHERE id = test_id; |
| 108 | |
| 109 | -- Try to update it to the opposite value |
| 110 | UPDATE public.news SET published = NOT result WHERE id = test_id; |
| 111 | |
| 112 | -- Check if the update worked |
| 113 | IF (SELECT published FROM public.news WHERE id = test_id) = NOT result THEN |
| 114 | RETURN QUERY SELECT true::boolean, 'Successfully updated published field'::text; |
| 115 | ELSE |
| 116 | RETURN QUERY SELECT false::boolean, 'Failed to update published field'::text; |
| 117 | END IF; |
| 118 | |
| 119 | -- Restore the original value |
| 120 | UPDATE public.news SET published = result WHERE id = test_id; |
| 121 | END $$; |
| 122 | |
| 123 | -- Execute the test function |
| 124 | SELECT * FROM test_update_news_published(); |