#!/usr/bin/env node // Apply Drizzle migrations against the DATABASE_URL sqlite file using the runtime // migrator (drizzle-orm/better-sqlite3/migrator) — NOT drizzle-kit. This lets the // container run migrations on boot with only runtime deps installed (drizzle-kit is a // devDep, pruned out of the production image). Same migration set + folder the test // helper uses (packages/db/src/testing.ts), so the schema matches production exactly. // // Usage: DATABASE_URL=/data/parking.sqlite node scripts/migrate-runtime.mjs import { dirname, resolve } from "node:path"; import { fileURLToPath } from "node:url"; import { mkdirSync } from "node:fs"; import Database from "better-sqlite3"; import { drizzle } from "drizzle-orm/better-sqlite3"; import { migrate } from "drizzle-orm/better-sqlite3/migrator"; const url = process.env.DATABASE_URL; if (!url) { console.error("[migrate] DATABASE_URL is required"); process.exit(1); } // Migrations folder ships beside this package (packages/db/drizzle); from scripts/ that's ../drizzle. const migrationsFolder = resolve(dirname(fileURLToPath(import.meta.url)), "../drizzle"); // Ensure the DB's parent dir exists (a fresh mounted volume may be empty). try { mkdirSync(dirname(resolve(url)), { recursive: true }); } catch { /* dir already exists (or url has no dir) — fine */ } const sqlite = new Database(url); sqlite.pragma("journal_mode = WAL"); sqlite.pragma("foreign_keys = ON"); const db = drizzle(sqlite); console.log(`[migrate] applying migrations from ${migrationsFolder} → ${url}`); migrate(db, { migrationsFolder }); sqlite.close(); console.log("[migrate] done");