import { loadConfig } from '../config.js'; import { closeMySqlPool, createMySqlPool } from './mysql.js'; import { executeMigrationPlan, loadMigrationPlan, type MigrationDirection } from './migration-runner.js'; const command = process.argv[2] ?? 'plan'; const validCommands = new Set(['plan', 'up', 'verify', 'down']); if (!validCommands.has(command)) { console.error('Usage: migrate-cli.js '); process.exitCode = 2; } else { const direction: MigrationDirection = command === 'plan' ? 'up' : command as MigrationDirection; const plan = await loadMigrationPlan(direction); if (command === 'plan') { console.log(JSON.stringify({ mode: 'dry-run', direction: plan.direction, file: plan.file, checksum: plan.checksum, statementCount: plan.statements.length }, null, 2)); } else { const config = loadConfig(); if (!config.mysql.passwordConfigured) { console.error('QIPAI_MYSQL_PASSWORD is required for live migration commands.'); process.exitCode = 2; } else { const pool = createMySqlPool(config); try { const result = await executeMigrationPlan(pool, plan); console.log(JSON.stringify({ mode: 'live', direction: result.direction, file: result.file, checksum: result.checksum, statementCount: result.statements.length, affectedRows: result.affectedRows }, null, 2)); } finally { await closeMySqlPool(pool); } } } }