import { AppDataSource } from '../data-source';

async function runMigrations() {
  try {
    console.log('Initializing datasource...');
    await AppDataSource.initialize();
    console.log('Datasource initialized. Running migrations...');
    const migrations = await AppDataSource.runMigrations();
    console.log(`Applied ${migrations.length} migrations.`);
    await AppDataSource.destroy();
    process.exit(0);
  } catch (err) {
    console.error('Migration run failed:', err);
    try {
      await AppDataSource.destroy();
    } catch {}
    process.exit(1);
  }
}

runMigrations();
