Files
Nexus/scripts/db-target-guard.mjs
Hartmut 19aeb2ba04
CI / Lint (push) Successful in 3m4s
CI / Typecheck (push) Successful in 3m6s
CI / Architecture Guardrails (push) Successful in 3m8s
CI / Assistant Split Regression (push) Successful in 3m48s
CI / Build (push) Has been cancelled
CI / E2E Tests (push) Has been cancelled
CI / Fresh-Linux Docker Deploy (push) Has been cancelled
CI / Release Images (push) Has been cancelled
CI / Unit Tests (push) Has been cancelled
rename(phase 3): compose/DB/infra + stray code refs capakraken → nexus (#62)
rename(phase 3): compose/DB/infra + stray code refs capakraken → nexus (#62)

Co-authored-by: Hartmut Nörenberg <hn@hartmut-noerenberg.com>
Co-committed-by: Hartmut Nörenberg <hn@hartmut-noerenberg.com>
2026-05-21 20:07:18 +02:00

87 lines
2.0 KiB
JavaScript

import { URL } from "node:url";
export function formatDatabaseTarget(parsedUrl, databaseName) {
return `${parsedUrl.protocol}//${decodeURIComponent(parsedUrl.username)}@${parsedUrl.hostname}${parsedUrl.port ? `:${parsedUrl.port}` : ""}/${databaseName}`;
}
export function inspectDatabaseUrl(rawUrl, expectedDatabase = "nexus") {
if (!rawUrl) {
throw new Error("DATABASE_URL is not configured.");
}
let parsedUrl;
try {
parsedUrl = new URL(rawUrl);
} catch (error) {
throw new Error(`DATABASE_URL is invalid: ${error instanceof Error ? error.message : String(error)}`);
}
const databaseName = parsedUrl.pathname.replace(/^\/+/, "");
const target = formatDatabaseTarget(parsedUrl, databaseName);
if (!databaseName) {
throw new Error(`DATABASE_URL does not contain a database name. Target=${target}`);
}
if (databaseName !== expectedDatabase) {
throw new Error(`Unexpected database target '${databaseName}'. Expected '${expectedDatabase}'. Target=${target}`);
}
return {
databaseName,
expectedDatabase,
target,
};
}
function collectPrismaCommandTokens(args) {
const tokens = [];
for (let index = 0; index < args.length; index += 1) {
const arg = args[index];
if (arg === "--") {
break;
}
if (arg === "--schema" || arg === "--url" || arg === "--telemetry-information") {
index += 1;
continue;
}
if (arg.startsWith("-")) {
continue;
}
tokens.push(arg);
}
return tokens;
}
export function shouldGuardPrismaCommand(args) {
if (args.includes("--help") || args.includes("-h")) {
return false;
}
const [command, subcommand] = collectPrismaCommandTokens(args);
if (!command) {
return false;
}
if (command === "db") {
return subcommand === "push" || subcommand === "pull" || subcommand === "execute";
}
if (command === "migrate") {
return true;
}
return command === "studio" || command === "introspect";
}
export function getExpectedDatabaseName() {
return process.env.NEXUS_EXPECTED_DB_NAME?.trim() || "nexus";
}