AI Resource Pro / website / scripts / add-news-published-field.sql
| 1 | -- Add published field to news table |
| 2 | -- This migration adds a published field to track whether news items should be displayed on the site |
| 3 | |
| 4 | -- Check if the published column exists in the news table |
| 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 with a default value of false |
| 14 | ALTER TABLE public.news |
| 15 | ADD COLUMN published BOOLEAN NOT NULL DEFAULT false; |
| 16 | |
| 17 | RAISE NOTICE 'Added published column to news table'; |
| 18 | ELSE |
| 19 | RAISE NOTICE 'Published column already exists in news table'; |
| 20 | END IF; |
| 21 | END $$; |
| 22 | |
| 23 | -- Update all existing news records to be published by default |
| 24 | -- This ensures backward compatibility |
| 25 | UPDATE public.news |
| 26 | SET published = true |
| 27 | WHERE published = false; |
| 28 | |
| 29 | -- Add an index to improve query performance for filtering by published status |
| 30 | DO $$ |
| 31 | BEGIN |
| 32 | IF NOT EXISTS ( |
| 33 | SELECT 1 |
| 34 | FROM pg_indexes |
| 35 | WHERE tablename = 'news' |
| 36 | AND indexname = 'idx_news_published' |
| 37 | ) THEN |
| 38 | CREATE INDEX idx_news_published ON public.news (published); |
| 39 | RAISE NOTICE 'Created index on published column'; |
| 40 | ELSE |
| 41 | RAISE NOTICE 'Index on published column already exists'; |
| 42 | END IF; |
| 43 | END $$; |