import { createClient } from '@supabase/supabase-js'; import { readFileSync, existsSync } from 'fs'; import * as path from 'path'; import * as dotenv from 'dotenv'; // Load environment variables from .env.local const envLocalPath = path.resolve(process.cwd(), '.env.local'); if (existsSync(envLocalPath)) { console.log(`Loading environment from: ${envLocalPath}`); dotenv.config({ path: envLocalPath }); } else { console.log(`No .env.local file found at: ${envLocalPath}`); dotenv.config(); // Try loading from default .env } // Get Supabase credentials from environment variables 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'); process.exit(1); } // Create a service role client to bypass RLS policies const supabase = createClient(supabaseUrl, supabaseServiceKey, { auth: { persistSession: false, }, }); async function main() { console.log('Checking blog posts in the database...'); try { // First, get table information console.log('Getting table information...'); const { data: tableInfo, error: tableError } = await supabase .rpc('get_table_info', { table_name: 'blog_posts' }); if (tableError) { console.log('Using alternative method to check table structure...'); // Alternative approach: try to select one record to see column structure const { data: samplePost, error: sampleError } = await supabase .from('blog_posts') .select('*') .limit(1); if (sampleError) { console.error('Error getting sample post:', sampleError); } else if (samplePost && samplePost.length > 0) { console.log('Sample post structure:', Object.keys(samplePost[0])); } else { console.log('No sample post found to determine structure'); } } else { console.log('Table information:', tableInfo); } // Now check if we have any blog posts console.log('\nCounting total blog posts...'); const { count, error: countError } = await supabase .from('blog_posts') .select('*', { count: 'exact', head: true }); if (countError) { console.error('Error counting blog posts:', countError); } else { console.log(`Total blog posts in database: ${count}`); } // List the first 5 blog posts to see what's in there console.log('\nFetching first 5 blog posts...'); const { data: posts, error: postsError } = await supabase .from('blog_posts') .select('id, title, slug, published, created_at') .order('created_at', { ascending: false }) .limit(5); if (postsError) { console.error('Error fetching blog posts:', postsError); } else if (posts && posts.length > 0) { console.log('Latest blog posts:'); posts.forEach((post, i) => { console.log(`${i+1}. ${post.title} (${post.slug}) - Published: ${post.published} - Created: ${post.created_at}`); }); } else { console.log('No blog posts found in the database'); // Check if we have the sample post in the content folder console.log('\nChecking for sample blog post in content/blog directory...'); const contentDir = path.join(process.cwd(), 'content/blog'); if (existsSync(contentDir)) { const files = existsSync(path.join(contentDir, 'hello-world.md')); if (files) { console.log('Sample blog post exists in content directory but not imported to database'); console.log('This suggests the import process might be failing'); } else { console.log('No sample blog post found in content directory'); } } } // Check the published column specifically console.log('\nChecking published column...'); const { count: publishedCount, error: publishedError } = await supabase .from('blog_posts') .select('*', { count: 'exact', head: true }) .eq('published', true); if (publishedError) { console.error('Error checking published column:', publishedError); // If the column doesn't exist, this might explain the issue console.log('\nChecking if published column exists...'); try { // Try a simpler query that might work const { data: simpleData, error: simpleError } = await supabase .from('blog_posts') .select('*') .limit(1); if (simpleError) { console.error('Error with simple query:', simpleError); } else if (simpleData && simpleData.length > 0) { const hasPublishedColumn = 'published' in simpleData[0]; console.log(`Published column exists: ${hasPublishedColumn}`); if (!hasPublishedColumn) { console.log('The published column is missing, which might be why posts are not appearing'); console.log('You may need to run the add-published-column migration'); } } } catch (simpleError) { console.error('Error checking column existence:', simpleError); } } else { console.log(`Total published blog posts: ${publishedCount || 0}`); } } catch (error) { console.error('Error checking blog posts:', error); } } main();