import "dotenv/config";
import { AppDataSource } from "../data-source";
import { ensurePaymentSchema } from "../config/ensure-schema";

/** `pnpm --filter @edr/payment-api migration:run` — ensure schema, then run pending migrations. */
async function main(): Promise<void> {
  await ensurePaymentSchema();
  await AppDataSource.initialize();
  try {
    const applied = await AppDataSource.runMigrations();
    for (const migration of applied) {
      console.log(`applied: ${migration.name}`);
    }
    if (applied.length === 0) console.log("no pending migrations");
  } finally {
    await AppDataSource.destroy();
  }
}

main().catch((err) => {
  console.error(err);
  process.exit(1);
});
