brackt/scripts/backfill-cli.ts
Chris Parsons 327c7b91ca
fix(backfill-cli): exit 0 on success so pg pool doesn't block
The cached postgres connection pool keeps the Node event loop open after
main() returns. Explicit process.exit(0) on success mirrors the pattern
in scripts/capture-baseline.ts.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-01 21:07:26 +00:00

82 lines
2.5 KiB
TypeScript

/**
* CLI entry point for the Phase 2 canonical backfill.
*
* Usage:
* npm run backfill:canonical -- [--dry-run | --apply] [--sport=<uuid>]
*
* Defaults to --dry-run for safety. No writes will be issued unless
* --apply is passed explicitly.
*/
import { DatabaseContext } from "~/database/context";
import { db } from "../server/db";
import { runBackfill } from "./backfill-canonical-layer";
import type { BackfillOptions } from "./backfill-canonical-layer";
function printHelp(): void {
console.log(
[
"Usage: backfill-cli [options]",
"",
"Options:",
" --dry-run Run without writing (default).",
" --apply Actually write to the database.",
" --sport=<uuid> Only backfill for the given sport id.",
" --help Show this message.",
].join("\n"),
);
}
function parseArgs(): BackfillOptions {
const args = process.argv.slice(2);
const opts: BackfillOptions = { dryRun: true }; // dry-run by default for safety
for (const a of args) {
if (a === "--apply") {
opts.dryRun = false;
} else if (a === "--dry-run") {
opts.dryRun = true;
} else if (a.startsWith("--sport=")) {
opts.sportId = a.slice("--sport=".length);
} else if (a === "--help" || a === "-h") {
printHelp();
process.exit(0);
} else {
console.error(`unknown arg: ${a}`);
process.exit(1);
}
}
return opts;
}
async function main() {
const opts = parseArgs();
console.log(
`Running backfill (dryRun=${opts.dryRun}, sportId=${opts.sportId ?? "all"})`,
);
const report = await DatabaseContext.run(db, () => runBackfill(opts));
console.log("---");
console.log(`tournamentsCreated: ${report.tournamentsCreated}`);
console.log(`tournamentsLinked: ${report.tournamentsLinked}`);
console.log(`participantsCreated: ${report.participantsCreated}`);
console.log(`participantsLinked: ${report.participantsLinked}`);
console.log(`tournamentResultsCreated: ${report.tournamentResultsCreated}`);
console.log(`surfaceElosCreated: ${report.surfaceElosCreated}`);
if (report.warnings.length) {
console.log("\nWARNINGS:");
for (const w of report.warnings) console.log(` ${w}`);
}
if (report.errors.length) {
console.log("\nERRORS:");
for (const e of report.errors) console.log(` ${e}`);
process.exit(2);
}
}
main()
.then(() => process.exit(0))
.catch((e) => {
console.error(e);
process.exit(1);
});