Files
ditto.site/packages/db/src/migrate.ts
T
2026-06-29 15:11:48 -07:00

35 lines
1.0 KiB
TypeScript

import { dirname, join } from "node:path";
import { fileURLToPath } from "node:url";
import { migrate } from "drizzle-orm/node-postgres/migrator";
import { createDb } from "./client.js";
const HERE = dirname(fileURLToPath(import.meta.url));
export const MIGRATIONS_DIR = join(HERE, "..", "migrations");
/** Apply all pending Drizzle migrations to the target database. */
export async function runMigrations(connectionString: string, migrationsFolder = MIGRATIONS_DIR): Promise<void> {
const { db, pool } = createDb(connectionString);
try {
await migrate(db, { migrationsFolder });
} finally {
await pool.end();
}
}
async function main(): Promise<void> {
const url = process.env.DATABASE_URL;
if (!url) {
console.error("DATABASE_URL is required");
process.exit(1);
}
await runMigrations(url);
console.log(JSON.stringify({ event: "migrations_applied" }));
}
if (import.meta.url === `file://${process.argv[1]}`) {
main().catch((e) => {
console.error(e);
process.exit(1);
});
}