AI Resource Pro / website / scripts / add-published-column.js
| 1 | // Script to add the 'published' column to 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 addPublishedColumn() { |
| 25 | try { |
| 26 | console.log('Adding published column to blog_posts table...'); |
| 27 | |
| 28 | // First, let's try a direct SQL query to add the column |
| 29 | const { data: sqlData, error: sqlError } = await supabase |
| 30 | .from('blog_posts') |
| 31 | .select('*') |
| 32 | .limit(1); |
| 33 | |
| 34 | if (sqlError) { |
| 35 | console.error('Error accessing blog_posts table:', sqlError.message); |
| 36 | } else { |
| 37 | console.log('Successfully accessed blog_posts table'); |
| 38 | |
| 39 | // Check if the published column already exists |
| 40 | const hasPublishedColumn = sqlData.length > 0 && 'published' in sqlData[0]; |
| 41 | |
| 42 | if (hasPublishedColumn) { |
| 43 | console.log('The published column already exists in the blog_posts table'); |
| 44 | } else { |
| 45 | console.log('The published column does not exist. Attempting to add it...'); |
| 46 | |
| 47 | // Try to execute raw SQL using the REST API |
| 48 | const response = await fetch(`${supabaseUrl}/rest/v1/rpc/execute_sql`, { |
| 49 | method: 'POST', |
| 50 | headers: { |
| 51 | 'Content-Type': 'application/json', |
| 52 | 'apikey': supabaseServiceKey, |
| 53 | 'Authorization': `Bearer ${supabaseServiceKey}` |
| 54 | }, |
| 55 | body: JSON.stringify({ |
| 56 | sql: ` |
| 57 | ALTER TABLE blog_posts ADD COLUMN IF NOT EXISTS published BOOLEAN DEFAULT false; |
| 58 | UPDATE blog_posts SET published = true WHERE published IS NULL; |
| 59 | ` |
| 60 | }) |
| 61 | }); |
| 62 | |
| 63 | if (!response.ok) { |
| 64 | const errorData = await response.json(); |
| 65 | throw new Error(`Failed to execute SQL: ${JSON.stringify(errorData)}`); |
| 66 | } |
| 67 | |
| 68 | console.log('SQL executed successfully'); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | // Verify the column was added by trying to select it |
| 73 | console.log('Verifying the published column exists...'); |
| 74 | const { data: verifyData, error: verifyError } = await supabase |
| 75 | .from('blog_posts') |
| 76 | .select('published') |
| 77 | .limit(1); |
| 78 | |
| 79 | if (verifyError) { |
| 80 | console.warn('Warning: Could not verify column was added:', verifyError.message); |
| 81 | } else { |
| 82 | console.log('Verification successful. Column exists in the schema.'); |
| 83 | } |
| 84 | |
| 85 | } catch (error) { |
| 86 | console.error('Error adding published column:', error.message); |
| 87 | process.exit(1); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | addPublishedColumn(); |