AI Resource Pro / website / scripts / check-published-column.js
| 1 | // Script to check if the 'published' column exists in the blog_posts table |
| 2 | import { config } from 'dotenv'; |
| 3 | import { createClient } from '@supabase/supabase-js'; |
| 4 | import { fileURLToPath } from 'url'; |
| 5 | import { dirname, resolve } from 'path'; |
| 6 | |
| 7 | // Get the directory of the current module |
| 8 | const __filename = fileURLToPath(import.meta.url); |
| 9 | const __dirname = dirname(__filename); |
| 10 | |
| 11 | // Load environment variables from .env.local |
| 12 | config({ path: resolve(__dirname, '../.env.local') }); |
| 13 | |
| 14 | const supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL; |
| 15 | const supabaseServiceKey = process.env.SUPABASE_SERVICE_ROLE_KEY; |
| 16 | |
| 17 | if (!supabaseUrl || !supabaseServiceKey) { |
| 18 | console.error('Missing Supabase environment variables. Please check your .env.local file.'); |
| 19 | process.exit(1); |
| 20 | } |
| 21 | |
| 22 | const supabase = createClient(supabaseUrl, supabaseServiceKey); |
| 23 | |
| 24 | async function checkPublishedColumn() { |
| 25 | try { |
| 26 | console.log('Checking if the published column exists in the blog_posts table...'); |
| 27 | |
| 28 | // Try to select a post with the published column |
| 29 | const { data, error } = await supabase |
| 30 | .from('blog_posts') |
| 31 | .select('id, title, published') |
| 32 | .limit(1); |
| 33 | |
| 34 | if (error) { |
| 35 | console.error('Error checking published column:', error.message); |
| 36 | |
| 37 | if (error.message.includes("published")) { |
| 38 | console.log('\nThe published column does NOT exist in the blog_posts table.'); |
| 39 | console.log('\nPlease run the following SQL in your Supabase SQL Editor:'); |
| 40 | console.log('\n-----------------------------------------------------------'); |
| 41 | console.log(` |
| 42 | ALTER TABLE blog_posts ADD COLUMN published BOOLEAN DEFAULT false; |
| 43 | UPDATE blog_posts SET published = true; |
| 44 | NOTIFY pgrst, 'reload schema'; |
| 45 | `); |
| 46 | console.log('-----------------------------------------------------------\n'); |
| 47 | } |
| 48 | } else { |
| 49 | console.log('Successfully queried the blog_posts table with the published column.'); |
| 50 | console.log('Sample data:', data); |
| 51 | |
| 52 | if (data.length > 0) { |
| 53 | const hasPublishedProperty = 'published' in data[0]; |
| 54 | console.log(`The published column ${hasPublishedProperty ? 'EXISTS' : 'DOES NOT EXIST'} in the blog_posts table.`); |
| 55 | } else { |
| 56 | console.log('No blog posts found to check the published column.'); |
| 57 | } |
| 58 | } |
| 59 | } catch (error) { |
| 60 | console.error('Error:', error.message); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | checkPublishedColumn(); |