import 'dotenv/config'; import { createClient } from '@supabase/supabase-js'; // Initialize Supabase client const supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL; const supabaseServiceKey = process.env.SUPABASE_SERVICE_ROLE_KEY; if (!supabaseUrl || !supabaseServiceKey) { console.error('Supabase URL or Service Role Key is missing from .env.local'); process.exit(1); } const supabase = createClient(supabaseUrl, supabaseServiceKey); async function checkNewsTable() { console.log('Checking news table structure...'); try { // Check if the news table exists const { data: tableExists, error: tableError } = await supabase .from('news') .select('id') .limit(1); if (tableError) { console.error('Error accessing news table:', tableError.message); return; } console.log('Successfully accessed news table'); // Check if the published column exists const { data: tableInfo, error: infoError } = await supabase.rpc('exec_sql', { sql: ` SELECT column_name, data_type FROM information_schema.columns WHERE table_name = 'news' ` }); if (infoError) { console.error('Error checking news table schema:', infoError.message); return; } console.log('tableInfo:', tableInfo); // Parse the response - exec_sql returns data differently let columns = []; if (tableInfo && Array.isArray(tableInfo)) { columns = tableInfo; } else if (tableInfo && tableInfo.result) { columns = tableInfo.result; } console.log('News table columns:', columns.map(col => col.column_name)); const hasPublishedColumn = columns.some(col => col.column_name === 'published'); if (hasPublishedColumn) { console.log('The published column already exists in the news table'); } else { console.log('Adding published column to news table...'); // Add the published column const { data: addColumnResult, error: addColumnError } = await supabase.rpc('exec_sql', { sql: ` ALTER TABLE public.news ADD COLUMN published BOOLEAN NOT NULL DEFAULT true ` }); if (addColumnError) { console.error('Error adding published column to news table:', addColumnError.message); return; } console.log('Published column added successfully'); // Update all existing records to be published by default const { data: updateResult, error: updateError } = await supabase.rpc('exec_sql', { sql: ` UPDATE public.news SET published = true WHERE published IS NULL ` }); if (updateError) { console.error('Error updating existing news records:', updateError.message); return; } console.log('Existing news records updated to published=true'); // Create an index for better performance const { data: indexResult, error: indexError } = await supabase.rpc('exec_sql', { sql: ` CREATE INDEX IF NOT EXISTS idx_news_published ON public.news (published) ` }); if (indexError) { console.error('Error creating index on published column:', indexError.message); return; } console.log('Created index on published column'); } // Verify the column exists const { data: verifyData, error: verifyError } = await supabase .from('news') .select('published') .limit(1); if (verifyError) { console.error('Error verifying published column:', verifyError.message); return; } console.log('Verification successful. Published column exists in the schema.'); } catch (error) { console.error('Unexpected error:', error); } } checkNewsTable();