From e5e2e68e5d15c4ec5e2b86aaf3540c58d98cb2cb Mon Sep 17 00:00:00 2001 From: Matt Date: Mon, 11 May 2026 12:25:22 +0200 Subject: [PATCH] fix(documents): backfill CLI --port arg guard --port without a value (or with a --flag value) previously silently fell back to all-ports mode because process.argv[indexOf+1] was undefined. Now exits 1 with an explicit error. Hardens the script before it gets wired into deploy in Task 17. Co-Authored-By: Claude Opus 4.7 (1M context) --- scripts/backfill-document-folders.ts | 45 ++++++++++++++-------------- 1 file changed, 22 insertions(+), 23 deletions(-) diff --git a/scripts/backfill-document-folders.ts b/scripts/backfill-document-folders.ts index 5ac1610e..7de949ea 100644 --- a/scripts/backfill-document-folders.ts +++ b/scripts/backfill-document-folders.ts @@ -49,9 +49,7 @@ export async function runBackfill(opts: BackfillOptions = {}): Promise { await db.transaction(async (tx) => { // Serialize concurrent runs on a per-port lock so two simultaneous // backfills can't race on folder inserts. - await tx.execute( - sql`SELECT pg_advisory_xact_lock(hashtext(${portId})::bigint)`, - ); + await tx.execute(sql`SELECT pg_advisory_xact_lock(hashtext(${portId})::bigint)`); // ── Step 1: Ensure system roots exist for this port ────────────────── await ensureSystemRoots(portId, systemUser); @@ -108,11 +106,7 @@ export async function runBackfill(opts: BackfillOptions = {}): Promise { .update(files) .set(update) .where( - and( - eq(files.id, d.signedFileId), - eq(files.portId, portId), - isNull(matchingFkColumn), - ), + and(eq(files.id, d.signedFileId), eq(files.portId, portId), isNull(matchingFkColumn)), ); } @@ -156,20 +150,25 @@ export async function runBackfill(opts: BackfillOptions = {}): Promise { // is the standard guard. The test suite imports `runBackfill` as a named // export; the CLI invocation hits this block and runs main(). -async function main(): Promise { - const portIdArg = process.argv.indexOf('--port'); - const portId = portIdArg !== -1 ? process.argv[portIdArg + 1] : undefined; - - await runBackfill({ portId }); - // eslint-disable-next-line no-console - console.log('Backfill complete'); - process.exit(0); -} - -// eslint-disable-next-line @typescript-eslint/no-require-imports if (require.main === module) { - main().catch((err) => { - logger.error({ err }, 'Backfill failed'); - process.exit(1); - }); + const portIdArg = process.argv.indexOf('--port'); + let portId: string | undefined; + if (portIdArg !== -1) { + const next = process.argv[portIdArg + 1]; + if (!next || next.startsWith('--')) { + logger.error('--port requires a value'); + process.exit(1); + } + portId = next; + } + runBackfill({ portId }) + .then(() => { + // eslint-disable-next-line no-console + console.log('Backfill complete'); + process.exit(0); + }) + .catch((err) => { + logger.error({ err }, 'Backfill failed'); + process.exit(1); + }); }