Branchwell

AI Resource Pro / website / scripts / test-published-field.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
13// Create a Supabase client with admin privileges
14const supabase = createClient(supabaseUrl, supabaseServiceKey);
15
16async function testPublishedFieldUpdate() {
17 try {
18 console.log("Testing published field update...");
19
20 // Get a news item to test with
21 const { data: newsItems, error: fetchError } = await supabase
22 .from('news')
23 .select('id, title, published')
24 .limit(1);
25
26 if (fetchError) {
27 console.error("Error fetching news item:", fetchError);
28 return;
29 }
30
31 if (!newsItems || newsItems.length === 0) {
32 console.error("No news items found for testing");
33 return;
34 }
35
36 const testItem = newsItems[0];
37 console.log("Test item:", testItem);
38
39 // Try to update with both true and false values
40 const newValue = !testItem.published;
41 console.log(`Attempting to change published from ${testItem.published} to ${newValue}`);
42
43 // Method 1: Standard update
44 const { data: updateResult, error: updateError } = await supabase
45 .from('news')
46 .update({ published: newValue })
47 .eq('id', testItem.id)
48 .select('published');
49
50 console.log("Update result:", updateResult);
51 console.log("Update error:", updateError);
52
53 // Method 2: Fetch the updated record to verify
54 const { data: verifyResult, error: verifyError } = await supabase
55 .from('news')
56 .select('id, title, published')
57 .eq('id', testItem.id)
58 .single();
59
60 console.log("Verify result:", verifyResult);
61 console.log("Verify error:", verifyError);
62
63 // Method 3: Try with direct SQL
64 const { data: sqlResult, error: sqlError } = await supabase.rpc('exec_sql', {
65 sql: `
66 UPDATE public.news
67 SET published = ${!newValue}
68 WHERE id = '${testItem.id}'
69 RETURNING id, title, published
70 `
71 });
72
73 console.log("SQL update result:", sqlResult);
74 console.log("SQL error:", sqlError);
75
76 // Final verification
77 const { data: finalCheck, error: finalError } = await supabase
78 .from('news')
79 .select('id, title, published')
80 .eq('id', testItem.id)
81 .single();
82
83 console.log("Final check:", finalCheck);
84 console.log("Final error:", finalError);
85
86 // Check for triggers on the news table
87 const { data: triggerInfo, error: triggerError } = await supabase.rpc('exec_sql', {
88 sql: `
89 SELECT trigger_name, event_manipulation, action_statement
90 FROM information_schema.triggers
91 WHERE event_object_table = 'news'
92 `
93 });
94
95 console.log("Triggers on news table:", triggerInfo);
96 console.log("Trigger error:", triggerError);
97
98 // Check for all constraints on the news table
99 const { data: constraintInfo, error: constraintError } = await supabase.rpc('exec_sql', {
100 sql: `
101 SELECT con.conname AS constraint_name,
102 con.contype AS constraint_type,
103 pg_get_constraintdef(con.oid) AS constraint_definition
104 FROM pg_constraint con
105 JOIN pg_class rel ON rel.oid = con.conrelid
106 JOIN pg_namespace nsp ON nsp.oid = rel.relnamespace
107 WHERE rel.relname = 'news'
108 AND nsp.nspname = 'public'
109 `
110 });
111
112 console.log("Constraints on news table:", constraintInfo);
113 console.log("Constraint error:", constraintError);
114
115 // Try updating with explicit transaction
116 let trx = null;
117 try {
118 console.log("Trying update with explicit transaction");
119
120 // Start a transaction
121 const { data: beginResult, error: beginError } = await supabase.rpc('exec_sql', {
122 sql: 'BEGIN;'
123 });
124
125 if (beginError) {
126 console.error("Error starting transaction:", beginError);
127 return;
128 }
129
130 trx = true;
131
132 // Do the update within the transaction
133 const { data: txUpdateResult, error: txUpdateError } = await supabase.rpc('exec_sql', {
134 sql: `
135 UPDATE public.news
136 SET published = ${newValue}
137 WHERE id = '${testItem.id}';
138 `
139 });
140
141 if (txUpdateError) {
142 console.error("Error in transaction update:", txUpdateError);
143 // Rollback on error
144 await supabase.rpc('exec_sql', { sql: 'ROLLBACK;' });
145 return;
146 }
147
148 // Commit the transaction
149 const { data: commitResult, error: commitError } = await supabase.rpc('exec_sql', {
150 sql: 'COMMIT;'
151 });
152
153 if (commitError) {
154 console.error("Error committing transaction:", commitError);
155 return;
156 }
157
158 console.log("Transaction committed successfully");
159
160 // Verify after transaction
161 const { data: afterTxCheck, error: afterTxError } = await supabase
162 .from('news')
163 .select('id, title, published')
164 .eq('id', testItem.id)
165 .single();
166
167 console.log("After transaction check:", afterTxCheck);
168 } catch (txError) {
169 console.error("Transaction error:", txError);
170 if (trx) {
171 // Attempt to roll back if we had started a transaction
172 await supabase.rpc('exec_sql', { sql: 'ROLLBACK;' });
173 }
174 }
175
176 } catch (error) {
177 console.error("Unexpected error in test:", error);
178 }
179}
180
181testPublishedFieldUpdate();