diff --git a/express/migrate.ts b/express/migrate.ts new file mode 100644 index 0000000..5d20333 --- /dev/null +++ b/express/migrate.ts @@ -0,0 +1,44 @@ +// migrate.ts +// CLI script for running database migrations + +import { migrate, migrationStatus, pool } from "./database"; + +async function main(): Promise { + const command = process.argv[2] || "run"; + + try { + switch (command) { + case "run": + await migrate(); + break; + + case "status": + const status = await migrationStatus(); + console.log("Applied migrations:"); + for (const name of status.applied) { + console.log(` ✓ ${name}`); + } + if (status.pending.length > 0) { + console.log("\nPending migrations:"); + for (const name of status.pending) { + console.log(` • ${name}`); + } + } else { + console.log("\nNo pending migrations"); + } + break; + + default: + console.error(`Unknown command: ${command}`); + console.error("Usage: migrate [run|status]"); + process.exit(1); + } + } finally { + await pool.end(); + } +} + +main().catch((err) => { + console.error("Migration failed:", err); + process.exit(1); +}); diff --git a/framework/cmd.d/db b/framework/cmd.d/db new file mode 100755 index 0000000..dd947c0 --- /dev/null +++ b/framework/cmd.d/db @@ -0,0 +1,9 @@ +#!/bin/bash + +set -eu + +DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +ROOT="$DIR/../.." + +# FIXME: don't hard code this of course +PGPASSWORD=diachron psql -U diachron -h localhost diachron \ No newline at end of file diff --git a/framework/cmd.d/migrate b/framework/cmd.d/migrate new file mode 100755 index 0000000..93edf74 --- /dev/null +++ b/framework/cmd.d/migrate @@ -0,0 +1,9 @@ +#!/bin/bash + +set -eu + +DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +ROOT="$DIR/../.." + +cd "$ROOT/express" +"$DIR"/tsx migrate.ts "$@"