const { Pool } = require('pg');

// Parse connection string from environment or command line
const connectionString = process.env.DATABASE_URL || process.argv[2];
const migrationSQL = process.argv[3];

if (!connectionString || !migrationSQL) {
  console.error('Usage: node run-specific-migration.js "postgresql://..." "SQL"');
  process.exit(1);
}

const pool = new Pool({
  connectionString,
  ssl: {
    rejectUnauthorized: false // For AWS RDS
  }
});

async function runMigration() {
  try {
    console.log('Running migration SQL:', migrationSQL);
    
    // Execute the migration
    const result = await pool.query(migrationSQL);
    
    console.log('✅ Migration completed successfully!');
    console.log('Rows affected:', result.rowCount);
    
  } catch (error) {
    console.error('❌ Migration failed:', error.message);
    process.exit(1);
  } finally {
    await pool.end();
  }
}

runMigration();