Files
CubeAdmin/lib/db/migrate.ts
2026-03-08 15:49:34 +01:00

32 lines
1.0 KiB
TypeScript

import { migrate } from "drizzle-orm/bun-sqlite/migrator";
import { existsSync } from "node:fs";
import { resolve } from "node:path";
import { db } from "./index";
const MIGRATIONS_FOLDER = resolve(process.cwd(), "drizzle");
/**
* Run all pending Drizzle migrations at startup.
*
* If the migrations folder does not exist yet (e.g. fresh clone before the
* first `bun run db:generate` has been executed) the function exits silently
* so that the application can still start in development without crashing.
*/
export function runMigrations(): void {
if (!existsSync(MIGRATIONS_FOLDER)) {
console.warn(
`[migrate] Migrations folder not found at "${MIGRATIONS_FOLDER}". ` +
"Skipping migrations — run `bun run db:generate` to create migration files.",
);
return;
}
try {
migrate(db, { migrationsFolder: MIGRATIONS_FOLDER });
console.log("[migrate] Database migrations applied successfully.");
} catch (err) {
console.error("[migrate] Failed to apply database migrations:", err);
throw err;
}
}