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); } // Create a Supabase client with admin privileges const supabase = createClient(supabaseUrl, supabaseServiceKey); async function testPublishedFieldUpdate() { try { console.log("Testing published field update..."); // Get a news item to test with const { data: newsItems, error: fetchError } = await supabase .from('news') .select('id, title, published') .limit(1); if (fetchError) { console.error("Error fetching news item:", fetchError); return; } if (!newsItems || newsItems.length === 0) { console.error("No news items found for testing"); return; } const testItem = newsItems[0]; console.log("Test item:", testItem); // Try to update with both true and false values const newValue = !testItem.published; console.log(`Attempting to change published from ${testItem.published} to ${newValue}`); // Method 1: Standard update const { data: updateResult, error: updateError } = await supabase .from('news') .update({ published: newValue }) .eq('id', testItem.id) .select('published'); console.log("Update result:", updateResult); console.log("Update error:", updateError); // Method 2: Fetch the updated record to verify const { data: verifyResult, error: verifyError } = await supabase .from('news') .select('id, title, published') .eq('id', testItem.id) .single(); console.log("Verify result:", verifyResult); console.log("Verify error:", verifyError); // Method 3: Try with direct SQL const { data: sqlResult, error: sqlError } = await supabase.rpc('exec_sql', { sql: ` UPDATE public.news SET published = ${!newValue} WHERE id = '${testItem.id}' RETURNING id, title, published ` }); console.log("SQL update result:", sqlResult); console.log("SQL error:", sqlError); // Final verification const { data: finalCheck, error: finalError } = await supabase .from('news') .select('id, title, published') .eq('id', testItem.id) .single(); console.log("Final check:", finalCheck); console.log("Final error:", finalError); // Check for triggers on the news table const { data: triggerInfo, error: triggerError } = await supabase.rpc('exec_sql', { sql: ` SELECT trigger_name, event_manipulation, action_statement FROM information_schema.triggers WHERE event_object_table = 'news' ` }); console.log("Triggers on news table:", triggerInfo); console.log("Trigger error:", triggerError); // Check for all constraints on the news table const { data: constraintInfo, error: constraintError } = await supabase.rpc('exec_sql', { sql: ` SELECT con.conname AS constraint_name, con.contype AS constraint_type, pg_get_constraintdef(con.oid) AS constraint_definition FROM pg_constraint con JOIN pg_class rel ON rel.oid = con.conrelid JOIN pg_namespace nsp ON nsp.oid = rel.relnamespace WHERE rel.relname = 'news' AND nsp.nspname = 'public' ` }); console.log("Constraints on news table:", constraintInfo); console.log("Constraint error:", constraintError); // Try updating with explicit transaction let trx = null; try { console.log("Trying update with explicit transaction"); // Start a transaction const { data: beginResult, error: beginError } = await supabase.rpc('exec_sql', { sql: 'BEGIN;' }); if (beginError) { console.error("Error starting transaction:", beginError); return; } trx = true; // Do the update within the transaction const { data: txUpdateResult, error: txUpdateError } = await supabase.rpc('exec_sql', { sql: ` UPDATE public.news SET published = ${newValue} WHERE id = '${testItem.id}'; ` }); if (txUpdateError) { console.error("Error in transaction update:", txUpdateError); // Rollback on error await supabase.rpc('exec_sql', { sql: 'ROLLBACK;' }); return; } // Commit the transaction const { data: commitResult, error: commitError } = await supabase.rpc('exec_sql', { sql: 'COMMIT;' }); if (commitError) { console.error("Error committing transaction:", commitError); return; } console.log("Transaction committed successfully"); // Verify after transaction const { data: afterTxCheck, error: afterTxError } = await supabase .from('news') .select('id, title, published') .eq('id', testItem.id) .single(); console.log("After transaction check:", afterTxCheck); } catch (txError) { console.error("Transaction error:", txError); if (trx) { // Attempt to roll back if we had started a transaction await supabase.rpc('exec_sql', { sql: 'ROLLBACK;' }); } } } catch (error) { console.error("Unexpected error in test:", error); } } testPublishedFieldUpdate();