#!/usr/bin/env node /** * Apply Supabase migration for active_bots table */ import { createClient } from '@supabase/supabase-js'; import * as fs from 'fs'; import * as path from 'path'; async function applyMigration() { const supabaseUrl = process.env.SUPABASE_URL; const supabaseKey = process.env.SUPABASE_SERVICE_KEY; if (!supabaseUrl || !supabaseKey) { console.error('ERROR: SUPABASE_URL and SUPABASE_SERVICE_KEY must be set'); process.exit(1); } const supabase = createClient(supabaseUrl, supabaseKey); // Read migration file const migrationPath = path.join(__dirname, '../supabase/migrations/20251012_create_active_bots.sql'); const migrationSQL = fs.readFileSync(migrationPath, 'utf8'); console.log('📦 Applying migration: create_active_bots'); console.log(`📄 File: ${migrationPath}`); console.log(); try { // Execute the SQL const { data, error } = await supabase.rpc('exec_sql', { sql: migrationSQL }); if (error) { // Try direct query if rpc doesn't work console.log('⚠️ RPC method failed, trying direct query...'); // Split SQL into individual statements and execute const statements = migrationSQL .split(';') .map(s => s.trim()) .filter(s => s.length > 0 && !s.startsWith('--')); for (const statement of statements) { console.log(`Executing: ${statement.substring(0, 50)}...`); const { error: stmtError } = await (supabase as any).from('_').select('*').limit(0); // Dummy query to test connection if (stmtError) { throw new Error(`Failed to execute statement: ${stmtError.message}`); } } } console.log('✅ Migration applied successfully!'); console.log(); console.log('Table created: active_bots'); console.log('- Columns: bot_id, pid, session_id, room_url, deck_url, status, etc.'); console.log('- Indexes: session_id, status, pid, spawned_at'); console.log('- Triggers: auto-update updated_at'); console.log('- RLS: Enabled with policies for service_role and authenticated users'); } catch (err: any) { console.error('❌ Migration failed:', err.message); console.error(); console.error('Please apply the migration manually:'); console.error('1. Go to Supabase Dashboard → SQL Editor'); console.error(`2. Copy the SQL from: ${migrationPath}`); console.error('3. Run the SQL'); process.exit(1); } } // Run if called directly if (require.main === module) { applyMigration().catch(err => { console.error('Fatal error:', err); process.exit(1); }); }