69 lines
2.1 KiB
JavaScript
69 lines
2.1 KiB
JavaScript
/* eslint-disable @typescript-eslint/no-require-imports */
|
|
/**
|
|
* Container entrypoint: apply Prisma migrations, then start Next server.
|
|
*
|
|
* Why:
|
|
* - In real deployments you want schema changes applied automatically per deploy.
|
|
* - `prisma migrate deploy` is safe to run multiple times (idempotent).
|
|
*
|
|
* Controls:
|
|
* - Set `SKIP_PRISMA_MIGRATE=true` to skip migrations (emergency / debugging).
|
|
*/
|
|
const { spawnSync } = require("node:child_process");
|
|
const fs = require("node:fs");
|
|
const path = require("node:path");
|
|
|
|
function run(cmd, args, opts = {}) {
|
|
const res = spawnSync(cmd, args, {
|
|
stdio: "inherit",
|
|
env: process.env,
|
|
...opts,
|
|
});
|
|
|
|
if (res.error) {
|
|
throw res.error;
|
|
}
|
|
if (typeof res.status === "number" && res.status !== 0) {
|
|
// propagate exit code
|
|
process.exit(res.status);
|
|
}
|
|
}
|
|
|
|
const skip = String(process.env.SKIP_PRISMA_MIGRATE || "").toLowerCase() === "true";
|
|
if (!skip) {
|
|
const autoBaseline =
|
|
String(process.env.PRISMA_AUTO_BASELINE || "").toLowerCase() === "true";
|
|
|
|
// Avoid relying on `npx` resolution in minimal runtimes.
|
|
// We copy `node_modules/prisma` into the runtime image.
|
|
if (autoBaseline) {
|
|
try {
|
|
const migrationsDir = path.join(process.cwd(), "prisma", "migrations");
|
|
const entries = fs
|
|
.readdirSync(migrationsDir, { withFileTypes: true })
|
|
.filter((d) => d.isDirectory())
|
|
.map((d) => d.name);
|
|
const initMigration = entries.find((n) => n.endsWith("_init"));
|
|
if (initMigration) {
|
|
// This is the documented "baseline" flow for existing databases:
|
|
// mark the initial migration as already applied.
|
|
run("node", [
|
|
"node_modules/prisma/build/index.js",
|
|
"migrate",
|
|
"resolve",
|
|
"--applied",
|
|
initMigration,
|
|
]);
|
|
}
|
|
} catch (_err) {
|
|
// If baseline fails we continue to migrate deploy, which will surface the real issue.
|
|
}
|
|
}
|
|
run("node", ["node_modules/prisma/build/index.js", "migrate", "deploy"]);
|
|
} else {
|
|
console.log("SKIP_PRISMA_MIGRATE=true -> skipping prisma migrate deploy");
|
|
}
|
|
|
|
run("node", ["server.js"]);
|
|
|