-- Fix News Published Field -- This script fixes any issues with the published field in the news table -- First, check if the published column exists DO $$ BEGIN IF NOT EXISTS ( SELECT 1 FROM information_schema.columns WHERE table_name = 'news' AND column_name = 'published' ) THEN -- Add the published column if it doesn't exist ALTER TABLE public.news ADD COLUMN published BOOLEAN NOT NULL DEFAULT true; RAISE NOTICE 'Added published column to news table'; ELSE RAISE NOTICE 'Published column already exists in news table'; -- Check the data type of the published column DECLARE column_type TEXT; BEGIN SELECT data_type INTO column_type FROM information_schema.columns WHERE table_name = 'news' AND column_name = 'published'; RAISE NOTICE 'Published column type: %', column_type; -- If it's not a boolean, alter it IF column_type <> 'boolean' THEN ALTER TABLE public.news ALTER COLUMN published TYPE BOOLEAN USING (published::boolean); RAISE NOTICE 'Changed published column type to BOOLEAN'; END IF; END; -- Drop any existing constraints that might be preventing updates BEGIN ALTER TABLE public.news DROP CONSTRAINT IF EXISTS news_published_check; RAISE NOTICE 'Dropped any check constraints on published column'; EXCEPTION WHEN others THEN RAISE NOTICE 'No check constraints to drop'; END; END IF; END $$; -- Fix existing news records to ensure they have a valid published value UPDATE public.news SET published = true WHERE published IS NULL; -- Double-check that RLS policies allow updating the published field DO $$ BEGIN -- Check for existing policies IF EXISTS ( SELECT 1 FROM pg_policies WHERE tablename = 'news' AND policyname = 'Admins can update news published status' ) THEN DROP POLICY "Admins can update news published status" ON public.news; RAISE NOTICE 'Dropped existing policy for updating published status'; END IF; -- Create a specific policy for the published field CREATE POLICY "Admins can update news published status" ON public.news FOR UPDATE USING ( EXISTS ( SELECT 1 FROM public.users WHERE id = auth.uid() AND is_admin = true ) ) WITH CHECK ( EXISTS ( SELECT 1 FROM public.users WHERE id = auth.uid() AND is_admin = true ) ); RAISE NOTICE 'Created policy for updating published status'; END $$; -- Create a function to test updating the published field CREATE OR REPLACE FUNCTION test_update_news_published() RETURNS TABLE (success boolean, message text) LANGUAGE plpgsql SECURITY DEFINER AS $$ DECLARE test_id uuid; result boolean; BEGIN -- Get a random news ID to test with SELECT id INTO test_id FROM public.news LIMIT 1; IF test_id IS NULL THEN RETURN QUERY SELECT false::boolean, 'No news records found for testing'::text; RETURN; END IF; -- Get the current published status SELECT published INTO result FROM public.news WHERE id = test_id; -- Try to update it to the opposite value UPDATE public.news SET published = NOT result WHERE id = test_id; -- Check if the update worked IF (SELECT published FROM public.news WHERE id = test_id) = NOT result THEN RETURN QUERY SELECT true::boolean, 'Successfully updated published field'::text; ELSE RETURN QUERY SELECT false::boolean, 'Failed to update published field'::text; END IF; -- Restore the original value UPDATE public.news SET published = result WHERE id = test_id; END $$; -- Execute the test function SELECT * FROM test_update_news_published();