spb/autoscholar
Public
1import { dirname, join } from "node:path";2import { fileURLToPath } from "node:url";3import { migrate } from "drizzle-orm/node-postgres/migrator";4import { closeDb, getDb } from "./client.js";56export async function runMigrations(connectionString = process.env.DATABASE_URL): Promise<void> {7 const db = getDb(connectionString);8 const here = dirname(fileURLToPath(import.meta.url));9 // works from src (tsx) and from dist (compiled)10 const migrationsFolder = join(here, "..", "drizzle");11 await migrate(db, { migrationsFolder });12}1314if (process.argv[1] && fileURLToPath(import.meta.url) === process.argv[1]) {15 runMigrations()16 .then(async () => {17 console.log("[db] migrations applied");18 await closeDb();19 })20 .catch(async (e) => {21 console.error("[db] migration failed:", e);22 await closeDb();23 process.exit(1);24 });25}26