import 'dotenv/config';
import { migrate } from 'drizzle-orm/node-postgres/migrator';

import { db, pool } from '../db/client';

const run = async (): Promise<void> => {
  await migrate(db, { migrationsFolder: './drizzle' });
  await pool.end();
  console.log('Database migrations applied.');
};

run()
  .then(() => process.exit(0))
  .catch(async (error) => {
    console.error('Failed to apply database migrations', error);
    await pool.end().catch(() => undefined);
    process.exit(1);
  });
