Files
lolympus-tournament-support/apps/api/src/modules/global/database/migrations/run-tournament-code-unique-indexes.ts
T

319 lines
9.7 KiB
TypeScript

import 'reflect-metadata';
import { Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { NestFactory } from '@nestjs/core';
import mongoose from 'mongoose';
import { readFile } from 'node:fs/promises';
import { resolve } from 'node:path';
import { ConnectionService, resolveMongoDbName } from '../connection.service';
import { SecretModule } from '../../secret/secret.module';
import {
migrateTournamentCodeUniqueIndexes,
TournamentCodeDuplicateGroupSummary,
TournamentCodeUniqueIndexMigrationProgress,
} from './20260627-tournament-code-unique-indexes';
interface CliOptions {
allowUsedDuplicateRelease: boolean;
createIndexes: boolean;
dryRun: boolean;
duplicateSampleLimit: number;
indexCommitQuorum?: number | string;
indexMaxTimeMS?: number;
listDuplicates: boolean;
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() {
const homeDir = process.env.HOME ?? process.env.USERPROFILE;
if (!homeDir) return;
const machineEnvFile = resolve(homeDir, 'source/global-env-vars.txt');
try {
const file = await readFile(machineEnvFile, '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 {
// Local machine env is optional for one-off migrations.
}
}
function parseArgs(argv: string[]): CliOptions {
let allowUsedDuplicateRelease = false;
let createIndexes = true;
let dryRun = true;
let duplicateSampleLimit = 20;
let indexCommitQuorum: number | string | undefined;
let indexMaxTimeMS: number | undefined;
let listDuplicates = false;
let nodeEnv = process.env.NODE_ENV ?? 'dev';
for (let index = 0; index < argv.length; index += 1) {
const arg = argv[index];
if (arg === '--allow-used-duplicate-release') {
allowUsedDuplicateRelease = true;
continue;
}
if (arg === '--apply') {
dryRun = false;
continue;
}
if (arg === '--dry-run') {
dryRun = true;
continue;
}
if (arg === '--duplicate-sample-limit') {
duplicateSampleLimit = parsePositiveInteger(
argv[index + 1],
'--duplicate-sample-limit',
);
index += 1;
continue;
}
if (arg.startsWith('--duplicate-sample-limit=')) {
duplicateSampleLimit = parsePositiveInteger(
arg.slice('--duplicate-sample-limit='.length),
'--duplicate-sample-limit',
);
continue;
}
if (arg === '--env') {
const value = argv[index + 1];
if (!value) throw new Error('--env requires dev, staging, or prod.');
nodeEnv = value;
index += 1;
continue;
}
if (arg.startsWith('--env=')) {
nodeEnv = arg.slice('--env='.length);
continue;
}
if (arg === '--index-commit-quorum') {
indexCommitQuorum = parseCommitQuorum(
argv[index + 1],
'--index-commit-quorum',
);
index += 1;
continue;
}
if (arg.startsWith('--index-commit-quorum=')) {
indexCommitQuorum = parseCommitQuorum(
arg.slice('--index-commit-quorum='.length),
'--index-commit-quorum',
);
continue;
}
if (arg === '--index-max-time-ms') {
indexMaxTimeMS = parsePositiveInteger(
argv[index + 1],
'--index-max-time-ms',
);
index += 1;
continue;
}
if (arg.startsWith('--index-max-time-ms=')) {
indexMaxTimeMS = parsePositiveInteger(
arg.slice('--index-max-time-ms='.length),
'--index-max-time-ms',
);
continue;
}
if (arg === '--list-duplicates') {
listDuplicates = true;
continue;
}
if (arg === '--skip-indexes') {
createIndexes = false;
continue;
}
if (arg === '--help' || arg === '-h') {
printHelp();
process.exit(0);
}
throw new Error(`Unknown argument: ${arg}`);
}
if (!['dev', 'staging', 'prod'].includes(nodeEnv)) {
throw new Error('--env must be dev, staging, or prod.');
}
return {
allowUsedDuplicateRelease,
createIndexes,
dryRun,
duplicateSampleLimit,
indexCommitQuorum,
indexMaxTimeMS,
listDuplicates,
nodeEnv,
};
}
function parseCommitQuorum(value: string | undefined, label: string) {
if (!value) {
throw new Error(`${label} requires a positive integer, majority, or votingMembers.`);
}
if (value === 'majority' || value === 'votingMembers') return value;
return parsePositiveInteger(value, label);
}
function parsePositiveInteger(value: string | undefined, label: string) {
const parsed = Number(value);
if (!Number.isInteger(parsed) || parsed < 1) {
throw new Error(`${label} requires a positive integer.`);
}
return parsed;
}
function printHelp() {
console.log(`Usage:
pnpm --filter api exec ts-node src/modules/global/database/migrations/run-tournament-code-unique-indexes.ts [--dry-run]
pnpm --filter api exec ts-node src/modules/global/database/migrations/run-tournament-code-unique-indexes.ts --env staging --apply
Options:
--dry-run Print affected counts without writing. Default.
--apply Release duplicate losers, pull them from matches, and create unique indexes.
--env <env> dev | staging | prod. Default: NODE_ENV or dev.
--list-duplicates
Print duplicate tournamentCode group samples.
--duplicate-sample-limit <n>
Max duplicate groups to print. Default: 20.
--index-commit-quorum <n|majority|votingMembers>
Override MongoDB index build commitQuorum. Use 1 only after checking replica health.
--index-max-time-ms <n>
Abort an individual index command after n milliseconds.
--skip-indexes Apply document cleanup without creating indexes.
--allow-used-duplicate-release
Allow releasing duplicate codes whose state is used. Default refuses this.
`);
}
function printDuplicateGroups(groups: TournamentCodeDuplicateGroupSummary[]) {
if (groups.length === 0) {
console.log('No duplicate tournamentCode groups found.');
return;
}
console.log('Duplicate tournamentCode groups:');
console.table(
groups.map((group) => ({
keeperCode: group.keeper.code ?? '',
keeperId: group.keeper.id,
keeperState: group.keeper.state ?? '',
key: group.key,
kind: group.kind,
loserCodes: group.losers.map((loser) => loser.code ?? '').join(', '),
loserIds: group.losers.map((loser) => loser.id).join(', '),
loserStates: group.losers
.map((loser) => loser.state ?? '')
.join(', '),
})),
);
}
function printProgress(progress: TournamentCodeUniqueIndexMigrationProgress) {
const count =
progress.scanned !== undefined
? ` scanned=${progress.scanned}`
: progress.total !== undefined
? ` total=${progress.total}`
: '';
const index = progress.indexName ? ` index=${progress.indexName}` : '';
const action = progress.action ? ` action=${progress.action}` : '';
console.log(
`[tournament-code unique index migration] ${progress.phase}${action}${index}${count}`,
);
}
async function main() {
const options = parseArgs(process.argv.slice(2));
process.env.NODE_ENV = options.nodeEnv;
process.env.SECRET_SOURCE = process.env.SECRET_SOURCE || 'infisical';
await loadMachineEnvFile();
const app = await NestFactory.createApplicationContext(MigrationModule, {
logger: ['log', 'warn', 'error'],
});
try {
const connectionService = app.get(ConnectionService);
const connection = await connectionService.getConnection();
if (!connection.db) {
throw new Error('MongoDB connection has no db handle.');
}
const dbName = resolveMongoDbName(options.nodeEnv);
console.log(
`[tournament-code unique index migration] mode=${
options.dryRun ? 'dry-run' : 'apply'
} env=${options.nodeEnv} db=${dbName}`,
);
const result = await migrateTournamentCodeUniqueIndexes(
connection.db as Parameters<typeof migrateTournamentCodeUniqueIndexes>[0],
{
allowUsedDuplicateRelease: options.allowUsedDuplicateRelease,
createIndexes: options.createIndexes,
dryRun: options.dryRun,
duplicateSampleLimit: options.duplicateSampleLimit,
indexCommitQuorum: options.indexCommitQuorum,
indexMaxTimeMS: options.indexMaxTimeMS,
onProgress: printProgress,
},
);
console.table(result.stats);
if (options.listDuplicates) {
printDuplicateGroups(result.sampleDuplicateGroups);
}
if (result.stats.unsafeUsedDuplicateCodes > 0 && options.dryRun) {
console.log(
'Apply will refuse used duplicate cleanup unless --allow-used-duplicate-release is provided.',
);
}
if (options.dryRun) {
console.log(
'No writes were made. Re-run with --apply to clean documents and create indexes.',
);
} else if (options.createIndexes) {
console.log('Migration applied and indexes ensured.');
} else {
console.log('Migration applied without index creation.');
}
} finally {
await app.close();
await mongoose.disconnect();
}
}
void main().catch((error: unknown) => {
console.error(error instanceof Error ? error.message : error);
process.exitCode = 1;
});