#!/bin/bash
# Run this on Command Center to execute the schema migration

cd /opt/arbiter-3.0/src

node -e "
const db = require('./database');
const fs = require('fs');

async function migrate() {
  try {
    const sql = fs.readFileSync('/tmp/arbiter_schema_migration.sql', 'utf8');
    console.log('Executing schema migration...');
    await db.query(sql);
    console.log('✅ Migration complete!');
    
    // Verify tables exist
    const result = await db.query(\`
      SELECT table_name 
      FROM information_schema.tables 
      WHERE table_schema = 'public' 
      AND table_name IN ('users', 'admin_audit_log', 'server_sync_log')
      ORDER BY table_name
    \`);
    console.log('\\n📊 Created tables:');
    result.rows.forEach(row => console.log('  ✓', row.table_name));
    
    // Show user count
    const userCount = await db.query('SELECT COUNT(*) FROM users');
    console.log('\\n👥 Users in table:', userCount.rows[0].count);
    
    process.exit(0);
  } catch (err) {
    console.error('❌ Migration failed:', err);
    process.exit(1);
  }
}

migrate();
"
