Branchwell

AI Resource Pro / website / scripts / check-blog-posts.ts

1import { createClient } from '@supabase/supabase-js';
2import { readFileSync, existsSync } from 'fs';
3import * as path from 'path';
4import * as dotenv from 'dotenv';
5
6// Load environment variables from .env.local
7const envLocalPath = path.resolve(process.cwd(), '.env.local');
8if (existsSync(envLocalPath)) {
9 console.log(`Loading environment from: ${envLocalPath}`);
10 dotenv.config({ path: envLocalPath });
11} else {
12 console.log(`No .env.local file found at: ${envLocalPath}`);
13 dotenv.config(); // Try loading from default .env
14}
15
16// Get Supabase credentials from environment variables
17const supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL || '';
18const supabaseServiceKey = process.env.SUPABASE_SERVICE_ROLE_KEY || '';
19
20if (!supabaseUrl || !supabaseServiceKey) {
21 console.error('Missing Supabase environment variables');
22 process.exit(1);
23}
24
25// Create a service role client to bypass RLS policies
26const supabase = createClient(supabaseUrl, supabaseServiceKey, {
27 auth: {
28 persistSession: false,
29 },
30});
31
32async function main() {
33 console.log('Checking blog posts in the database...');
34
35 try {
36 // First, get table information
37 console.log('Getting table information...');
38 const { data: tableInfo, error: tableError } = await supabase
39 .rpc('get_table_info', { table_name: 'blog_posts' });
40
41 if (tableError) {
42 console.log('Using alternative method to check table structure...');
43 // Alternative approach: try to select one record to see column structure
44 const { data: samplePost, error: sampleError } = await supabase
45 .from('blog_posts')
46 .select('*')
47 .limit(1);
48
49 if (sampleError) {
50 console.error('Error getting sample post:', sampleError);
51 } else if (samplePost && samplePost.length > 0) {
52 console.log('Sample post structure:', Object.keys(samplePost[0]));
53 } else {
54 console.log('No sample post found to determine structure');
55 }
56 } else {
57 console.log('Table information:', tableInfo);
58 }
59
60 // Now check if we have any blog posts
61 console.log('\nCounting total blog posts...');
62 const { count, error: countError } = await supabase
63 .from('blog_posts')
64 .select('*', { count: 'exact', head: true });
65
66 if (countError) {
67 console.error('Error counting blog posts:', countError);
68 } else {
69 console.log(`Total blog posts in database: ${count}`);
70 }
71
72 // List the first 5 blog posts to see what's in there
73 console.log('\nFetching first 5 blog posts...');
74 const { data: posts, error: postsError } = await supabase
75 .from('blog_posts')
76 .select('id, title, slug, published, created_at')
77 .order('created_at', { ascending: false })
78 .limit(5);
79
80 if (postsError) {
81 console.error('Error fetching blog posts:', postsError);
82 } else if (posts && posts.length > 0) {
83 console.log('Latest blog posts:');
84 posts.forEach((post, i) => {
85 console.log(`${i+1}. ${post.title} (${post.slug}) - Published: ${post.published} - Created: ${post.created_at}`);
86 });
87 } else {
88 console.log('No blog posts found in the database');
89
90 // Check if we have the sample post in the content folder
91 console.log('\nChecking for sample blog post in content/blog directory...');
92 const contentDir = path.join(process.cwd(), 'content/blog');
93 if (existsSync(contentDir)) {
94 const files = existsSync(path.join(contentDir, 'hello-world.md'));
95 if (files) {
96 console.log('Sample blog post exists in content directory but not imported to database');
97 console.log('This suggests the import process might be failing');
98 } else {
99 console.log('No sample blog post found in content directory');
100 }
101 }
102 }
103
104 // Check the published column specifically
105 console.log('\nChecking published column...');
106 const { count: publishedCount, error: publishedError } = await supabase
107 .from('blog_posts')
108 .select('*', { count: 'exact', head: true })
109 .eq('published', true);
110
111 if (publishedError) {
112 console.error('Error checking published column:', publishedError);
113
114 // If the column doesn't exist, this might explain the issue
115 console.log('\nChecking if published column exists...');
116 try {
117 // Try a simpler query that might work
118 const { data: simpleData, error: simpleError } = await supabase
119 .from('blog_posts')
120 .select('*')
121 .limit(1);
122
123 if (simpleError) {
124 console.error('Error with simple query:', simpleError);
125 } else if (simpleData && simpleData.length > 0) {
126 const hasPublishedColumn = 'published' in simpleData[0];
127 console.log(`Published column exists: ${hasPublishedColumn}`);
128
129 if (!hasPublishedColumn) {
130 console.log('The published column is missing, which might be why posts are not appearing');
131 console.log('You may need to run the add-published-column migration');
132 }
133 }
134 } catch (simpleError) {
135 console.error('Error checking column existence:', simpleError);
136 }
137 } else {
138 console.log(`Total published blog posts: ${publishedCount || 0}`);
139 }
140
141 } catch (error) {
142 console.error('Error checking blog posts:', error);
143 }
144}
145
146main();