Files
lolympus-tournament-support/apps/api/src/modules/global/database/migrations/run-auction-redis-field-rename.ts
T

365 lines
11 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 { migrateAuctionRedisFieldRename } from './20260622-auction-redis-field-rename';
interface CliOptions {
duplicateSampleLimit: number;
dryRun: boolean;
limit?: number;
listDuplicates: boolean;
nodeEnv: string;
repairDuplicateSequences: boolean;
}
interface DuplicateSequenceRepairStats {
affectedAuctions: number;
changedAuctionSequenceFields: number;
changedHistoryEntries: number;
scannedHistoryEntries: number;
}
@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 {
// 로컬 machine env 파일이 없으면 그냥 지나간다.
}
}
function parseArgs(argv: string[]): CliOptions {
let dryRun = true;
let duplicateSampleLimit = 20;
let limit: number | undefined;
let listDuplicates = false;
let nodeEnv = process.env.NODE_ENV ?? 'dev';
let repairDuplicateSequences = false;
for (let index = 0; index < argv.length; index += 1) {
const arg = argv[index];
if (arg === '--apply') {
dryRun = false;
continue;
}
if (arg === '--dry-run') {
dryRun = true;
continue;
}
if (arg === '--list-duplicates') {
listDuplicates = true;
continue;
}
if (arg === '--repair-duplicate-sequences') {
repairDuplicateSequences = 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 === '--limit') {
limit = parsePositiveInteger(argv[index + 1], '--limit');
index += 1;
continue;
}
if (arg.startsWith('--limit=')) {
limit = parsePositiveInteger(arg.slice('--limit='.length), '--limit');
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 {
duplicateSampleLimit,
dryRun,
limit,
listDuplicates,
nodeEnv,
repairDuplicateSequences,
};
}
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-auction-redis-field-rename.ts [--dry-run]
pnpm --filter api exec ts-node src/modules/global/database/migrations/run-auction-redis-field-rename.ts --env staging --apply
Options:
--dry-run Print affected counts without writing. Default.
--apply Apply auction and auctionHistory field updates.
--env <env> dev | staging | prod. Default: NODE_ENV or dev.
--limit <n> Only scan first n documents per collection.
--list-duplicates
Print duplicate auctionHistory auctionId+sequence samples.
--duplicate-sample-limit <n>
Max duplicate groups to print. Default: 20.
--repair-duplicate-sequences
Resequence affected auctionHistory rows by takenAt/createdAt/_id before migration.
`);
}
async function findAuctionIdsWithDuplicateHistorySequences(
db: NonNullable<mongoose.Connection['db']>,
): Promise<unknown[]> {
const groups = await db
.collection('auctionHistory')
.aggregate([
{
$group: {
_id: { auctionId: '$auctionId', sequence: '$sequence' },
count: { $sum: 1 },
},
},
{ $match: { count: { $gt: 1 } } },
{ $group: { _id: '$_id.auctionId' } },
])
.toArray();
return groups.map((group) => group._id).filter(Boolean);
}
async function listDuplicateHistorySequences(
db: NonNullable<mongoose.Connection['db']>,
sampleLimit: number,
) {
return db
.collection('auctionHistory')
.aggregate([
{
$group: {
_id: { auctionId: '$auctionId', sequence: '$sequence' },
actions: { $push: '$action' },
count: { $sum: 1 },
createdAtValues: { $push: '$createdAt' },
historyIds: { $push: '$_id' },
matchIds: { $addToSet: '$matchId' },
takenAtValues: { $push: '$takenAt' },
},
},
{ $match: { count: { $gt: 1 } } },
{ $sort: { count: -1, '_id.sequence': 1 } },
{ $limit: sampleLimit },
])
.toArray();
}
function printDuplicateHistorySequences(
groups: Awaited<ReturnType<typeof listDuplicateHistorySequences>>,
) {
if (groups.length === 0) {
console.log('No duplicate auctionHistory auctionId+sequence groups found.');
return;
}
console.log('Duplicate auctionHistory auctionId+sequence groups:');
console.table(
groups.map((group) => ({
actions: (group.actions as unknown[]).map(String).join(', '),
auctionId: String(group._id?.auctionId ?? ''),
count: group.count,
createdAt: (group.createdAtValues as unknown[]).map(String).join(', '),
historyIds: (group.historyIds as unknown[]).map(String).join(', '),
matchIds: (group.matchIds as unknown[]).map(String).join(', '),
sequence: group._id?.sequence,
takenAt: (group.takenAtValues as unknown[]).map(String).join(', '),
})),
);
}
async function repairDuplicateHistorySequences(
db: NonNullable<mongoose.Connection['db']>,
dryRun: boolean,
): Promise<DuplicateSequenceRepairStats> {
const historyCollection = db.collection('auctionHistory');
const auctionCollection = db.collection('auction');
const affectedAuctionIds =
await findAuctionIdsWithDuplicateHistorySequences(db);
const stats: DuplicateSequenceRepairStats = {
affectedAuctions: affectedAuctionIds.length,
changedAuctionSequenceFields: 0,
changedHistoryEntries: 0,
scannedHistoryEntries: 0,
};
for (const auctionId of affectedAuctionIds) {
const histories = await historyCollection
.find({ auctionId })
.project({ _id: 1, createdAt: 1, sequence: 1, takenAt: 1 })
.sort({ takenAt: 1, createdAt: 1, _id: 1 })
.toArray();
stats.scannedHistoryEntries += histories.length;
const writes = histories
.map((history, index) => ({
history,
nextSequence: index + 1,
}))
.filter(({ history, nextSequence }) => history.sequence !== nextSequence);
stats.changedHistoryEntries += writes.length;
stats.changedAuctionSequenceFields += 1;
if (dryRun) continue;
if (writes.length > 0) {
await historyCollection.bulkWrite(
writes.map(({ history, nextSequence }) => ({
updateOne: {
filter: { _id: history._id },
update: { $set: { sequence: nextSequence } },
},
})),
{ ordered: true },
);
}
await auctionCollection.updateOne(
{ _id: auctionId } as Record<string, unknown>,
{ $set: { currentAuctionHistorySequence: histories.length } },
);
}
return stats;
}
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(
`[auction redis field rename migration] mode=${
options.dryRun ? 'dry-run' : 'apply'
} env=${options.nodeEnv} db=${dbName}`,
);
if (options.repairDuplicateSequences) {
const repairStats = await repairDuplicateHistorySequences(
connection.db,
options.dryRun,
);
console.log('Duplicate auctionHistory sequence repair:');
console.table(repairStats);
}
const stats = await migrateAuctionRedisFieldRename(
connection.db as Parameters<typeof migrateAuctionRedisFieldRename>[0],
{
dryRun: options.dryRun,
limit: options.limit,
},
);
console.table(stats);
if (options.listDuplicates && stats.duplicateHistorySequences > 0) {
const duplicateGroups = await listDuplicateHistorySequences(
connection.db,
options.duplicateSampleLimit,
);
printDuplicateHistorySequences(duplicateGroups);
}
if (options.dryRun) {
console.log(
'No writes were made. Re-run with --apply to update documents.',
);
} else {
console.log('Migration applied.');
}
} finally {
await app.close();
await mongoose.disconnect();
}
}
void main().catch((error: unknown) => {
console.error(error instanceof Error ? error.message : error);
process.exitCode = 1;
});