import { migrate } from 'drizzle-orm/postgres-js/migrator';
import postgres from 'postgres';
import { drizzle } from 'drizzle-orm/postgres-js';
import { fileURLToPath } from 'node:url';
import path from 'node:path';

const url = process.env.DATABASE_URL ?? 'postgresql://bmm:bmm@localhost:5432/bmm';
const client = postgres(url, { max: 1, prepare: false });
const db = drizzle(client);

const here = path.dirname(fileURLToPath(import.meta.url));
const migrationsFolder = path.resolve(here, '..', 'drizzle');

console.log(`[db] migrating from ${migrationsFolder}...`);
await migrate(db, { migrationsFolder });
console.log('[db] migrations done.');
await client.end();
