/** * Check Existing Supabase Tables * * This script connects to Supabase and lists all existing tables * to check for conflicts with new schema */ const { createClient } = require('@supabase/supabase-js'); const supabaseUrl = 'https://vsyrpfiwhjvahofxwytr.supabase.co'; const supabaseKey = 'REDACTED_SUPABASE_ANON_KEY'; const supabase = createClient(supabaseUrl, supabaseKey); // Tables we want to create const newTables = [ 'forum_categories', 'forum_discussions', 'forum_replies', 'p2p_ads', 'p2p_trades', 'notifications', 'referrals' ]; async function checkTables() { console.log('šŸ” Checking existing Supabase tables...\n'); try { // Get list of all tables using information_schema const { data, error } = await supabase .from('information_schema.tables') .select('table_name') .eq('table_schema', 'public') .eq('table_type', 'BASE TABLE'); if (error) { // If we can't query information_schema, try a different approach console.log('āš ļø Cannot access information_schema with anon key'); console.log('ā„¹ļø This is normal - anon key has limited permissions'); console.log('\nšŸ“‹ Tables we will create:\n'); newTables.forEach(table => { console.log(` - ${table}`); }); console.log('\nāœ… Safe to proceed: Using CREATE TABLE IF NOT EXISTS'); console.log(' → Existing tables will NOT be affected'); console.log(' → Only missing tables will be created\n'); return; } const existingTables = data.map(row => row.table_name); console.log(`šŸ“Š Found ${existingTables.length} existing tables in Supabase:\n`); existingTables.forEach(table => { console.log(` - ${table}`); }); console.log('\nšŸ“‹ Tables we want to create:\n'); const conflicts = []; const newToCreate = []; newTables.forEach(table => { if (existingTables.includes(table)) { conflicts.push(table); console.log(` āš ļø ${table} (ALREADY EXISTS)`); } else { newToCreate.push(table); console.log(` āœ… ${table} (WILL BE CREATED)`); } }); console.log('\n' + '='.repeat(60)); if (conflicts.length > 0) { console.log('\nāš ļø CONFLICT ANALYSIS:\n'); console.log(` ${conflicts.length} table(s) already exist:`); conflicts.forEach(table => { console.log(` - ${table}`); }); console.log('\nāœ… SAFE TO PROCEED:'); console.log(' → SQL uses "CREATE TABLE IF NOT EXISTS"'); console.log(' → Existing tables will be SKIPPED'); console.log(' → No data will be modified or deleted'); console.log(' → Only missing tables will be created\n'); } if (newToCreate.length > 0) { console.log(`\nšŸ“ ${newToCreate.length} new table(s) will be created:`); newToCreate.forEach(table => { console.log(` - ${table}`); }); } console.log('\n' + '='.repeat(60)); console.log('\nšŸŽÆ RECOMMENDATION:\n'); if (conflicts.length === 0) { console.log(' āœ… No conflicts found'); console.log(' āœ… Safe to run SUPABASE_SCHEMA.sql'); console.log(' āœ… All 7 tables will be created\n'); } else if (newToCreate.length === 0) { console.log(' ā„¹ļø All tables already exist'); console.log(' ā„¹ļø You can safely run the SQL (it will skip existing tables)'); console.log(' ā„¹ļø Or you can skip running it entirely\n'); } else { console.log(' āš ļø Some tables exist, some don\'t'); console.log(' āœ… Safe to run SUPABASE_SCHEMA.sql'); console.log(` āœ… Will create ${newToCreate.length} missing tables`); console.log(` ā„¹ļø Will skip ${conflicts.length} existing tables\n`); } } catch (error) { console.error('āŒ Error:', error.message); console.log('\nā„¹ļø If you see permission errors, this is expected with anon key'); console.log('āœ… Proceed with SQL execution - it\'s safe!\n'); } } checkTables().then(() => { console.log('āœ… Analysis complete\n'); process.exit(0); }).catch(err => { console.error('āŒ Fatal error:', err); process.exit(1); });