/** * Check Existing Supabase Tables with Admin Access */ const { createClient } = require('@supabase/supabase-js'); const supabaseUrl = 'https://vsyrpfiwhjvahofxwytr.supabase.co'; const supabaseServiceKey = process.env.SUPABASE_SERVICE_KEY || 'sb_secret_oXy8Diay2J_u8dKPZLxdcQ_hq69Mrb6'; const supabase = createClient(supabaseUrl, supabaseServiceKey, { auth: { autoRefreshToken: false, persistSession: false } }); const newTables = [ 'forum_categories', 'forum_discussions', 'forum_replies', 'p2p_ads', 'p2p_trades', 'notifications', 'referrals' ]; async function checkTables() { console.log('šŸ” Fetching existing tables with admin access...\n'); try { // Use RPC to query information_schema const { data, error } = await supabase.rpc('get_table_list', {}); if (error) { // Fallback: Direct query const query = ` SELECT table_name, (SELECT COUNT(*) FROM information_schema.columns WHERE table_name = t.table_name AND table_schema = 'public') as column_count FROM information_schema.tables t WHERE table_schema = 'public' AND table_type = 'BASE TABLE' ORDER BY table_name; `; const { data: tableData, error: queryError } = await supabase .from('_placeholder') .select(); if (queryError) { console.log('āš ļø Cannot query information_schema directly'); console.log('ā„¹ļø Will check for conflicts manually\n'); await checkConflictsManually(); return; } } // If we got data, process it if (data && Array.isArray(data)) { const existingTables = data.map(row => row.table_name); displayAnalysis(existingTables); } else { await checkConflictsManually(); } } catch (error) { console.log('āš ļø Error:', error.message); console.log('ā„¹ļø Checking conflicts manually...\n'); await checkConflictsManually(); } } async function checkConflictsManually() { console.log('šŸ“‹ Checking each table individually...\n'); const results = { existing: [], missing: [], errors: [] }; for (const tableName of newTables) { try { const { data, error } = await supabase .from(tableName) .select('*') .limit(0); if (error) { if (error.message.includes('does not exist') || error.code === '42P01') { results.missing.push(tableName); console.log(` āœ… ${tableName} - NOT FOUND (will be created)`); } else { results.errors.push({ table: tableName, error: error.message }); console.log(` āš ļø ${tableName} - ERROR: ${error.message}`); } } else { results.existing.push(tableName); console.log(` āš ļø ${tableName} - ALREADY EXISTS`); } } catch (e) { results.errors.push({ table: tableName, error: e.message }); console.log(` āŒ ${tableName} - ERROR: ${e.message}`); } } console.log('\n' + '='.repeat(70)); console.log('\nšŸ“Š CONFLICT ANALYSIS:\n'); if (results.existing.length > 0) { console.log(`āš ļø ${results.existing.length} table(s) ALREADY EXIST:\n`); results.existing.forEach(table => { console.log(` - ${table}`); }); console.log('\n ā„¹ļø These tables will be SKIPPED'); console.log(' ā„¹ļø Existing data will NOT be modified\n'); } if (results.missing.length > 0) { console.log(`āœ… ${results.missing.length} table(s) WILL BE CREATED:\n`); results.missing.forEach(table => { console.log(` - ${table}`); }); console.log(''); } if (results.errors.length > 0) { console.log(`āš ļø ${results.errors.length} error(s) encountered:\n`); results.errors.forEach(({ table, error }) => { console.log(` - ${table}: ${error}`); }); console.log(''); } console.log('='.repeat(70)); console.log('\nšŸŽÆ RECOMMENDATION:\n'); if (results.missing.length === newTables.length) { console.log(' āœ… NO CONFLICTS - All tables are new'); console.log(' āœ… Safe to run SUPABASE_SCHEMA.sql'); console.log(' āœ… All 7 tables will be created\n'); } else if (results.existing.length === newTables.length) { console.log(' ā„¹ļø ALL TABLES ALREADY EXIST'); console.log(' ā„¹ļø SQL will skip all tables (no changes)'); console.log(' āš ļø Check if schemas match mobile app expectations\n'); } else { console.log(' āš ļø PARTIAL CONFLICT'); console.log(` āœ… ${results.missing.length} tables will be created`); console.log(` ā„¹ļø ${results.existing.length} tables will be skipped`); console.log(' āœ… Safe to run SUPABASE_SCHEMA.sql\n'); } } function displayAnalysis(existingTables) { console.log(`šŸ“Š Found ${existingTables.length} existing tables:\n`); existingTables.forEach(table => { const isConflict = newTables.includes(table); console.log(` ${isConflict ? 'āš ļø ' : ' '} ${table}${isConflict ? ' (CONFLICT)' : ''}`); }); console.log('\n' + '='.repeat(70)); console.log('\nšŸ“‹ New tables to create:\n'); const conflicts = []; const newToCreate = []; newTables.forEach(table => { if (existingTables.includes(table)) { conflicts.push(table); console.log(` āš ļø ${table} (ALREADY EXISTS - will be skipped)`); } else { newToCreate.push(table); console.log(` āœ… ${table} (WILL BE CREATED)`); } }); console.log('\n' + '='.repeat(70)); console.log('\nšŸŽÆ SUMMARY:\n'); console.log(` Total existing tables: ${existingTables.length}`); console.log(` Conflicting tables: ${conflicts.length}`); console.log(` New tables to create: ${newToCreate.length}`); console.log(` After SQL: ${existingTables.length + newToCreate.length} total tables\n`); console.log('='.repeat(70)); console.log('\nāœ… RECOMMENDATION:\n'); if (conflicts.length === 0) { console.log(' āœ… NO CONFLICTS'); console.log(' āœ… Safe to run SUPABASE_SCHEMA.sql\n'); } else { console.log(' āš ļø Some tables already exist'); console.log(' āœ… Safe to run SQL (will skip existing tables)'); console.log(' ā„¹ļø Check schemas of existing tables for compatibility\n'); } } checkTables().then(() => { console.log('āœ… Analysis complete\n'); process.exit(0); }).catch(err => { console.error('āŒ Fatal error:', err.message); process.exit(1); });