Branchwell

AI Resource Pro / website / scripts / check-news-table.js

1import 'dotenv/config';
2import { createClient } from '@supabase/supabase-js';
3
4// Initialize Supabase client
5const supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL;
6const supabaseServiceKey = process.env.SUPABASE_SERVICE_ROLE_KEY;
7
8if (!supabaseUrl || !supabaseServiceKey) {
9 console.error('Supabase URL or Service Role Key is missing from .env.local');
10 process.exit(1);
11}
12
13const supabase = createClient(supabaseUrl, supabaseServiceKey);
14
15async function checkNewsTable() {
16 console.log('Checking news table structure...');
17
18 try {
19 // Check if the news table exists
20 const { data: tableExists, error: tableError } = await supabase
21 .from('news')
22 .select('id')
23 .limit(1);
24
25 if (tableError) {
26 console.error('Error accessing news table:', tableError.message);
27 return;
28 }
29
30 console.log('Successfully accessed news table');
31
32 // Check if the published column exists
33 const { data: tableInfo, error: infoError } = await supabase.rpc('exec_sql', {
34 sql: `
35 SELECT column_name, data_type
36 FROM information_schema.columns
37 WHERE table_name = 'news'
38 `
39 });
40
41 if (infoError) {
42 console.error('Error checking news table schema:', infoError.message);
43 return;
44 }
45
46 console.log('tableInfo:', tableInfo);
47
48 // Parse the response - exec_sql returns data differently
49 let columns = [];
50 if (tableInfo && Array.isArray(tableInfo)) {
51 columns = tableInfo;
52 } else if (tableInfo && tableInfo.result) {
53 columns = tableInfo.result;
54 }
55
56 console.log('News table columns:', columns.map(col => col.column_name));
57
58 const hasPublishedColumn = columns.some(col => col.column_name === 'published');
59
60 if (hasPublishedColumn) {
61 console.log('The published column already exists in the news table');
62 } else {
63 console.log('Adding published column to news table...');
64
65 // Add the published column
66 const { data: addColumnResult, error: addColumnError } = await supabase.rpc('exec_sql', {
67 sql: `
68 ALTER TABLE public.news
69 ADD COLUMN published BOOLEAN NOT NULL DEFAULT true
70 `
71 });
72
73 if (addColumnError) {
74 console.error('Error adding published column to news table:', addColumnError.message);
75 return;
76 }
77
78 console.log('Published column added successfully');
79
80 // Update all existing records to be published by default
81 const { data: updateResult, error: updateError } = await supabase.rpc('exec_sql', {
82 sql: `
83 UPDATE public.news
84 SET published = true
85 WHERE published IS NULL
86 `
87 });
88
89 if (updateError) {
90 console.error('Error updating existing news records:', updateError.message);
91 return;
92 }
93
94 console.log('Existing news records updated to published=true');
95
96 // Create an index for better performance
97 const { data: indexResult, error: indexError } = await supabase.rpc('exec_sql', {
98 sql: `
99 CREATE INDEX IF NOT EXISTS idx_news_published
100 ON public.news (published)
101 `
102 });
103
104 if (indexError) {
105 console.error('Error creating index on published column:', indexError.message);
106 return;
107 }
108
109 console.log('Created index on published column');
110 }
111
112 // Verify the column exists
113 const { data: verifyData, error: verifyError } = await supabase
114 .from('news')
115 .select('published')
116 .limit(1);
117
118 if (verifyError) {
119 console.error('Error verifying published column:', verifyError.message);
120 return;
121 }
122
123 console.log('Verification successful. Published column exists in the schema.');
124
125 } catch (error) {
126 console.error('Unexpected error:', error);
127 }
128}
129
130checkNewsTable();