bidstage/scripts/migrate.ts

49 lines
1.8 KiB
TypeScript

import { createHash } from "node:crypto";
import { readdir, readFile } from "node:fs/promises";
import { resolve } from "node:path";
import { database } from "../lib/db";
const MIGRATION_LOCK_ID = 8_291_937_111;
async function main() {
const directory = resolve(process.cwd(), "db", "migrations");
const files = (await readdir(directory)).filter((file) => /^\d+_.+\.sql$/.test(file)).sort();
const client = await database().connect();
try {
await client.query("SELECT pg_advisory_lock($1)", [MIGRATION_LOCK_ID]);
await client.query(`CREATE TABLE IF NOT EXISTS schema_migrations (
name text PRIMARY KEY,
sha256 text NOT NULL,
applied_at timestamptz NOT NULL DEFAULT now()
)`);
for (const file of files) {
const sql = await readFile(resolve(directory, file), "utf8");
const sha256 = createHash("sha256").update(sql).digest("hex");
const applied = await client.query<{ sha256: string }>("SELECT sha256 FROM schema_migrations WHERE name = $1", [file]);
if (applied.rows[0]) {
if (applied.rows[0].sha256 !== sha256) throw new Error(`Applied migration was modified: ${file}`);
continue;
}
await client.query("BEGIN");
try {
await client.query(sql);
await client.query("INSERT INTO schema_migrations (name, sha256) VALUES ($1, $2)", [file, sha256]);
await client.query("COMMIT");
console.log(`Applied ${file}`);
} catch (error) {
await client.query("ROLLBACK");
throw error;
}
}
} finally {
await client.query("SELECT pg_advisory_unlock($1)", [MIGRATION_LOCK_ID]).catch(() => undefined);
client.release();
await database().end();
}
}
main().catch((error) => {
console.error(error instanceof Error ? error.message : String(error));
process.exitCode = 1;
});