import 'reflect-metadata'; import { Module } from '@nestjs/common'; import { ConfigModule } from '@nestjs/config'; import { NestFactory } from '@nestjs/core'; import { readFile } from 'node:fs/promises'; import { resolve } from 'node:path'; import { ConnectionService, resolveMongoDbName } from '../connection.service'; import { SecretModule } from '../../secret/secret.module'; import { migrateCompetitionDomain, type CompetitionDomainMigrationDb, } from './competition-domain-runner'; export interface CompetitionDomainCliOptions { dryRun: boolean; help: boolean; limit?: number; afterMatchId?: string; nodeEnv: string; } @Module({ imports: [ ConfigModule.forRoot({ isGlobal: true, envFilePath: [ resolve(__dirname, '../../../../../../project.env'), resolve(process.cwd(), '../project.env'), resolve(process.cwd(), 'apps/project.env'), resolve(process.cwd(), 'project.env'), ], }), SecretModule, ], providers: [ConnectionService], }) class MigrationModule {} async function loadMachineEnvFile(): Promise { const homeDir = process.env.HOME ?? process.env.USERPROFILE; if (!homeDir) return; try { const file = await readFile( resolve(homeDir, 'source/global-env-vars.txt'), 'utf8', ); for (const rawLine of file.split(/\r?\n/)) { const line = rawLine.trim(); if (!line || line.startsWith('#')) continue; const matched = line.match(/^([A-Za-z_][A-Za-z0-9_]*)\s*=\s*(.*)$/); if (!matched) continue; const [, key, rawValue] = matched; process.env[key] = rawValue.replace(/^["']|["']$/g, ''); } } catch { // A local machine env file is optional for one-off migrations. } } export function parseCliOptions(argv: string[]): CompetitionDomainCliOptions { let dryRun = true; let help = false; let limit: number | undefined; let afterMatchId: string | undefined; let nodeEnv = process.env.NODE_ENV ?? 'dev'; for (let index = 0; index < argv.length; index += 1) { const argument = argv[index]; // pnpm preserves the conventional option separator when it forwards // script arguments (`pnpm ... -- --apply`). It is not a CLI option. if (argument === '--') continue; if (argument === '--apply') { dryRun = false; continue; } if (argument === '--dry-run') { dryRun = true; continue; } if (argument === '--env') { const value = argv[index + 1]; if (!value) throw new Error('--env requires dev, staging, or prod.'); nodeEnv = value; index += 1; continue; } if (argument.startsWith('--env=')) { nodeEnv = argument.slice('--env='.length); continue; } if (argument === '--limit') { limit = positiveInteger(argv[index + 1], '--limit'); index += 1; continue; } if (argument.startsWith('--limit=')) { limit = positiveInteger(argument.slice('--limit='.length), '--limit'); continue; } if (argument === '--after') { afterMatchId = matchObjectId(argv[index + 1]); index += 1; continue; } if (argument.startsWith('--after=')) { afterMatchId = matchObjectId(argument.slice('--after='.length)); continue; } if (argument === '--help' || argument === '-h') { help = true; continue; } throw new Error(`Unknown argument: ${argument}`); } if (!['dev', 'staging', 'prod'].includes(nodeEnv)) { throw new Error('--env must be dev, staging, or prod.'); } return { dryRun, help, limit, afterMatchId, nodeEnv }; } function positiveInteger(value: string | undefined, label: string): number { const parsed = Number(value); if (!Number.isInteger(parsed) || parsed < 1) { throw new Error(`${label} requires a positive integer.`); } return parsed; } function matchObjectId(value: string | undefined): string { if (!value || !/^[a-f\d]{24}$/i.test(value)) { throw new Error( '--after requires a 24-character hexadecimal Match ObjectId.', ); } return value.toLowerCase(); } function printHelp(): void { process.stdout.write(`Usage: pnpm --silent --filter api competition:migrate pnpm --silent --filter api competition:migrate -- --env staging --apply Options: --dry-run Plan and report without writes. Default. --apply Apply one transaction per Match. --env dev | staging | prod. Default: NODE_ENV or dev. --limit Scan at most n Matches. Apply resumes from a durable checkpoint. --after Override the checkpoint with a Match ObjectId cursor. The JSON report is written to stdout; the concise human summary is written to stderr. `); } async function main(): Promise { const options = parseCliOptions(process.argv.slice(2)); if (options.help) { printHelp(); return; } await loadMachineEnvFile(); process.env.NODE_ENV = options.nodeEnv; const app = await NestFactory.createApplicationContext(MigrationModule, { logger: false, }); try { const connectionService = app.get(ConnectionService); const connection = await connectionService.getConnection(); if (!connection.db) throw new Error('MongoDB connection has no db handle.'); const result = await migrateCompetitionDomain( connection.db as unknown as CompetitionDomainMigrationDb, { dryRun: options.dryRun, limit: options.limit, afterMatchId: options.afterMatchId, startSession: options.dryRun ? undefined : async () => connection.startSession(), }, ); const { summary } = result; process.stderr.write( `[competition-domain migration] mode=${result.mode} env=${options.nodeEnv} ` + `db=${resolveMongoDbName(options.nodeEnv)} scanned=${summary.scanned} ` + `ready=${summary.ready} applied=${summary.applied} ` + `alreadyApplied=${summary.alreadyApplied} quarantined=${summary.quarantined} ` + `failed=${summary.failed} unclassified=${summary.unclassified} ` + `indexPreflight=${result.indexPreflight.status} ` + `resume=${result.resume.checkpointAfterMatchId ?? 'none'} ` + `completed=${result.resume.completed}\n`, ); process.stdout.write(`${JSON.stringify(result, null, 2)}\n`); if (result.indexPreflight.status === 'blocked') process.exitCode = 1; else if (summary.failed > 0) process.exitCode = 1; else if (summary.quarantined > 0) process.exitCode = 2; } finally { await app.close().catch((error: unknown) => { const message = error instanceof Error ? error.message : String(error); process.stderr.write( `[competition-domain migration] context close warning: ${message}\n`, ); }); } } if (require.main === module) { void main().catch((error: unknown) => { const message = error instanceof Error ? error.message : String(error); process.stderr.write(`[competition-domain migration] failed: ${message}\n`); process.stdout.write( `${JSON.stringify({ migrationRunId: '20260712-competition-domain', mode: process.argv.includes('--apply') ? 'apply' : 'dry-run', error: message, })}\n`, ); process.exitCode = 1; }); }