/** The scripts say pbps_mis (V1), pbps_v2 (V2) and pbps_legacy (the archive). Real names come from the environment. */
const target = new URL(process.env["MIGRATION_DATABASE_URL"] ?? "mysql://x/pbps_v2").pathname.slice(1);
export const NAMES: Record<string, string> = { pbps_mis: process.env["SOURCE_DB"] ?? "pbps_mis", pbps_v2: target, pbps_legacy: process.env["ARCHIVE_DB"] ?? `${target}_v1_archive` };
for (const n of Object.values(NAMES)) if (!/^\w{1,64}$/.test(n)) throw new Error(`Database name ${n} is not allowed.`);
if (new Set(Object.values(NAMES)).size !== 3) throw new Error("V1, V2 and the archive must be three different databases.");
export const named = (sql: string): string => sql.replace(/\bpbps_(mis|v2|legacy)\b/g, (n) => NAMES[n]!);
