// Script to add the 'published' column to the blog_posts table import { config } from 'dotenv'; import { createClient } from '@supabase/supabase-js'; import { fileURLToPath } from 'url'; import { dirname, resolve } from 'path'; // Get the directory of the current module const __filename = fileURLToPath(import.meta.url); const __dirname = dirname(__filename); // Load environment variables from .env.local config({ path: resolve(__dirname, '../.env.local') }); const supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL; const supabaseServiceKey = process.env.SUPABASE_SERVICE_ROLE_KEY; if (!supabaseUrl || !supabaseServiceKey) { console.error('Missing Supabase environment variables. Please check your .env.local file.'); process.exit(1); } const supabase = createClient(supabaseUrl, supabaseServiceKey); async function addPublishedColumn() { try { console.log('Adding published column to blog_posts table...'); // First, let's try a direct SQL query to add the column const { data: sqlData, error: sqlError } = await supabase .from('blog_posts') .select('*') .limit(1); if (sqlError) { console.error('Error accessing blog_posts table:', sqlError.message); } else { console.log('Successfully accessed blog_posts table'); // Check if the published column already exists const hasPublishedColumn = sqlData.length > 0 && 'published' in sqlData[0]; if (hasPublishedColumn) { console.log('The published column already exists in the blog_posts table'); } else { console.log('The published column does not exist. Attempting to add it...'); // Try to execute raw SQL using the REST API const response = await fetch(`${supabaseUrl}/rest/v1/rpc/execute_sql`, { method: 'POST', headers: { 'Content-Type': 'application/json', 'apikey': supabaseServiceKey, 'Authorization': `Bearer ${supabaseServiceKey}` }, body: JSON.stringify({ sql: ` ALTER TABLE blog_posts ADD COLUMN IF NOT EXISTS published BOOLEAN DEFAULT false; UPDATE blog_posts SET published = true WHERE published IS NULL; ` }) }); if (!response.ok) { const errorData = await response.json(); throw new Error(`Failed to execute SQL: ${JSON.stringify(errorData)}`); } console.log('SQL executed successfully'); } } // Verify the column was added by trying to select it console.log('Verifying the published column exists...'); const { data: verifyData, error: verifyError } = await supabase .from('blog_posts') .select('published') .limit(1); if (verifyError) { console.warn('Warning: Could not verify column was added:', verifyError.message); } else { console.log('Verification successful. Column exists in the schema.'); } } catch (error) { console.error('Error adding published column:', error.message); process.exit(1); } } addPublishedColumn();