Files

68 lines
2.9 KiB
JavaScript
Raw Permalink Normal View History

import { createRequire as __bundleCreateRequire } from "node:module"; const require = __bundleCreateRequire(import.meta.url);
import {
esm_default
} from "./chunks/chunk-KIUJQ22G.js";
import {
__name
} from "./chunks/chunk-T6GW54FI.js";
// dist/migrate.js
import { createHash, randomUUID } from "node:crypto";
import { existsSync } from "node:fs";
import { readdir, readFile } from "node:fs/promises";
import path from "node:path";
import { fileURLToPath } from "node:url";
if (existsSync(".env"))
process.loadEnvFile(".env");
var MIGRATIONS_DIR = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "../prisma/migrations");
var LOCK_ID = 72707369;
var client = new esm_default.Client({ connectionString: process.env.DATABASE_URL });
async function main() {
await client.query(`SELECT pg_advisory_lock(${LOCK_ID})`);
await client.query(`CREATE TABLE IF NOT EXISTS "_prisma_migrations" (
"id" VARCHAR(36) PRIMARY KEY NOT NULL,
"checksum" VARCHAR(64) NOT NULL,
"finished_at" TIMESTAMPTZ,
"migration_name" VARCHAR(255) NOT NULL,
"logs" TEXT,
"rolled_back_at" TIMESTAMPTZ,
"started_at" TIMESTAMPTZ NOT NULL DEFAULT now(),
"applied_steps_count" INTEGER NOT NULL DEFAULT 0
)`);
const { rows } = await client.query(`SELECT migration_name, finished_at FROM "_prisma_migrations" WHERE rolled_back_at IS NULL`);
const failed = rows.find((r) => !r.finished_at);
if (failed) {
throw new Error(`migration ${failed.migration_name} failed earlier; fix the database, then mark it with \`prisma migrate resolve\` (see its "logs" column in _prisma_migrations)`);
}
const applied = new Set(rows.map((r) => r.migration_name));
const entries = await readdir(MIGRATIONS_DIR, { withFileTypes: true });
const pending = entries.filter((e) => e.isDirectory() && !applied.has(e.name)).map((e) => e.name).sort();
for (const name of pending) {
const script = await readFile(path.join(MIGRATIONS_DIR, name, "migration.sql"), "utf8");
const id = randomUUID();
await client.query(`INSERT INTO "_prisma_migrations" (id, checksum, migration_name) VALUES ($1, $2, $3)`, [id, createHash("sha256").update(script).digest("hex"), name]);
try {
await client.query(script);
} catch (e) {
await client.query(`UPDATE "_prisma_migrations" SET logs = $2 WHERE id = $1`, [
id,
String(e)
]);
throw new Error(`migration ${name} failed`, { cause: e });
}
await client.query(`UPDATE "_prisma_migrations" SET finished_at = now(), applied_steps_count = 1 WHERE id = $1`, [id]);
console.log(`applied ${name}`);
}
console.log(pending.length ? `${pending.length} migration(s) applied` : "no pending migrations");
}
__name(main, "main");
try {
await client.connect();
await main();
} catch (e) {
console.error(e);
process.exitCode = 1;
} finally {
await client.end();
}