feat: redesign auction engine with redis sot

This commit is contained in:
dahyeon committed 2026-06-23 03:16:04 -07:00
1 parent 737702d78c
commit e8bb20d988
75 files changed
+12737 -542

No files matched your search

@@ -0,0 +1,219 @@
import { migrateAuctionRedisFieldRename } from './20260622-auction-redis-field-rename';
type Doc = Record<string, unknown> & { _id: string };
function createDb(params: { auctions?: Doc[]; history?: Doc[] }) {
const collections = {
auction: params.auctions ?? [],
auctionHistory: params.history ?? [],
};
return {
collections,
db: {
collection(name: string) {
const docs =
name === 'auction'
? collections.auction
: name === 'auctionHistory'
? collections.auctionHistory
: [];
return {
find() {
return {
limit() {
return {
async *[Symbol.asyncIterator]() {
for (const doc of docs) yield doc;
},
};
},
};
},
async bulkWrite(writes: Array<{
updateOne: {
filter: { _id: unknown };
update: {
$set?: Record<string, unknown>;
$unset?: Record<string, ''>;
};
};
}>) {
for (const write of writes) {
const doc = docs.find((item) => item._id === write.updateOne.filter._id);
if (!doc) continue;
Object.assign(doc, write.updateOne.update.$set ?? {});
for (const key of Object.keys(write.updateOne.update.$unset ?? {})) {
delete doc[key];
}
}
},
};
},
},
};
}
describe('migrateAuctionRedisFieldRename', () => {
it('renames auction fields and nested bid/team fields', async () => {
const deadline = new Date('2026-06-22T01:02:03.004Z');
const { db, collections } = createDb({
auctions: [
{
_id: 'auction-1',
teams: [
{
teamIndex: 1,
captainUserId: 'u1',
points: 900,
members: [{ userId: 'u1', assignedPosition: 'TOP', cost: 100 }],
},
],
order: [{ userId: 'p1', passCount: 0 }],
passedQueue: [{ userId: 'p2', passCount: 1 }],
auctionRound: 2,
currentIndex: 3,
bids: [
{
teamIndex: 1,
amount: 120,
bidderUserId: 'u1',
timestamp: deadline,
},
],
currentBid: 120,
currentBidderTeamIndex: 1,
currentDeadline: deadline,
maxTimerSeconds: 30,
bidExtensionSeconds: 5,
initialPoints: 1000,
topTierThreshold: 'DIAMOND',
manualTopTierUserIds: ['u2'],
startedAt: deadline,
completedAt: deadline,
},
],
});
const dryRunStats = await migrateAuctionRedisFieldRename(db, {
dryRun: true,
});
expect(dryRunStats.changedAuctions).toBe(1);
expect(collections.auction[0]).toHaveProperty('teams');
const stats = await migrateAuctionRedisFieldRename(db, { dryRun: false });
expect(stats.changedAuctions).toBe(1);
expect(collections.auction[0]).toMatchObject({
auctionEngineVersion: 'legacy-renamed',
auctionLifecycleId: 'legacy-auction:auction-1',
revision: 0,
currentAuctionHistorySequence: 0,
matchSideEffectStatus: 'none',
matchSideEffectRevision: 0,
auctionTeams: [
{
teamIndex: 1,
captainUserId: 'u1',
remainingBidPointAmount: 900,
members: [
{
userId: 'u1',
assignedPosition: 'TOP',
memberAcquisitionBidPointAmount: 100,
},
],
},
],
auctionPlayerOrder: [{ userId: 'p1', passCount: 0 }],
passedAuctionPlayerQueue: [{ userId: 'p2', passCount: 1 }],
currentAuctionRound: 2,
currentAuctionPlayerOrderIndex: 3,
auctionBidHistory: [
{
bidderTeamIndex: 1,
bidPointAmount: 120,
bidderUserId: 'u1',
bidPlacedAtTimestampMS: deadline.getTime(),
},
],
currentHighestBidPointAmount: 120,
currentHighestBidderTeamIndex: 1,
currentBidRoundDeadlineTimestampMS: deadline.getTime(),
maximumBidRoundDurationSeconds: 30,
bidRoundExtensionSeconds: 5,
initialTeamBidPointAmount: 1000,
topTierAuctionPriorityThreshold: 'DIAMOND',
manualTopTierAuctionPriorityUserIds: ['u2'],
auctionStartedAtTimestampMS: deadline.getTime(),
auctionCompletedAtTimestampMS: deadline.getTime(),
});
expect(collections.auction[0]).not.toHaveProperty('teams');
expect(collections.auction[0]).not.toHaveProperty('bids');
expect(collections.auction[0]).not.toHaveProperty('currentDeadline');
});
it('renames history snapshots and adds transition metadata', async () => {
const { db, collections } = createDb({
history: [
{
_id: 'history-1',
auctionId: 'auction-1',
matchId: 'match-1',
sequence: 2,
action: 'sold',
snapshotBefore: {
currentBid: 100,
bids: [{ teamIndex: 1, amount: 100, timestamp: 123 }],
},
},
],
});
await migrateAuctionRedisFieldRename(db, { dryRun: false });
expect(collections.auctionHistory[0]).toMatchObject({
auctionLifecycleId: 'legacy-auction:auction-1',
auctionStateTransitionId: 'legacy-history:auction-1:2',
revision: 2,
historyMutation: { type: 'append' },
matchSideEffectMutation: { type: 'none' },
snapshotBefore: {
currentHighestBidPointAmount: 100,
auctionBidHistory: [
{
bidderTeamIndex: 1,
bidPointAmount: 100,
bidPlacedAtTimestampMS: 123,
},
],
},
});
});
it('is idempotent after applying updates', async () => {
const { db } = createDb({
auctions: [{ _id: 'auction-1', teams: [] }],
history: [{ _id: 'history-1', auctionId: 'auction-1', sequence: 1 }],
});
await migrateAuctionRedisFieldRename(db, { dryRun: false });
const stats = await migrateAuctionRedisFieldRename(db, { dryRun: false });
expect(stats.changedAuctions).toBe(0);
expect(stats.changedHistoryEntries).toBe(0);
});
it('fails apply when duplicate history sequences would break unique indexes', async () => {
const { db } = createDb({
history: [
{ _id: 'h1', auctionId: 'auction-1', sequence: 1 },
{ _id: 'h2', auctionId: 'auction-1', sequence: 1 },
],
});
await expect(
migrateAuctionRedisFieldRename(db, { dryRun: false }),
).rejects.toThrow('Duplicate auctionHistory sequence');
});
});
@@ -0,0 +1,336 @@
interface MigrationCollection {
find(filter: Record<string, unknown>): {
limit(limit: number): AsyncIterable<AnyDoc>;
};
bulkWrite(
writes: Array<{
updateOne: {
filter: { _id: unknown };
update: { $set?: Record<string, unknown>; $unset?: Record<string, ''> };
};
}>,
options: { ordered: boolean },
): Promise<unknown>;
}
interface MigrationDb {
collection(name: string): MigrationCollection;
}
interface MigrationOptions {
dryRun?: boolean;
limit?: number;
}
interface MigrationStats {
changedAuctions: number;
changedHistoryEntries: number;
duplicateHistorySequences: number;
scannedAuctions: number;
scannedHistoryEntries: number;
}
type AnyDoc = Record<string, unknown> & { _id: unknown };
type UpdateDoc = { $set?: Record<string, unknown>; $unset?: Record<string, ''> };
const AUCTION_FIELD_RENAMES: Array<{
from: string;
to: string;
transform?: (value: unknown) => unknown;
}> = [
{ from: 'teams', to: 'auctionTeams', transform: transformAuctionTeams },
{ from: 'order', to: 'auctionPlayerOrder' },
{ from: 'passedQueue', to: 'passedAuctionPlayerQueue' },
{ from: 'auctionRound', to: 'currentAuctionRound' },
{ from: 'currentIndex', to: 'currentAuctionPlayerOrderIndex' },
{ from: 'bids', to: 'auctionBidHistory', transform: transformAuctionBidHistory },
{ from: 'currentBid', to: 'currentHighestBidPointAmount' },
{ from: 'currentBidderTeamIndex', to: 'currentHighestBidderTeamIndex' },
{
from: 'currentDeadline',
to: 'currentBidRoundDeadlineTimestampMS',
transform: toTimestampMS,
},
{ from: 'maxTimerSeconds', to: 'maximumBidRoundDurationSeconds' },
{ from: 'bidExtensionSeconds', to: 'bidRoundExtensionSeconds' },
{ from: 'initialPoints', to: 'initialTeamBidPointAmount' },
{ from: 'topTierThreshold', to: 'topTierAuctionPriorityThreshold' },
{
from: 'manualTopTierUserIds',
to: 'manualTopTierAuctionPriorityUserIds',
},
{
from: 'startedAt',
to: 'auctionStartedAtTimestampMS',
transform: toTimestampMS,
},
{
from: 'completedAt',
to: 'auctionCompletedAtTimestampMS',
transform: toTimestampMS,
},
];
export async function migrateAuctionRedisFieldRename(
db: MigrationDb,
options: MigrationOptions = {},
): Promise<MigrationStats> {
const dryRun = options.dryRun ?? true;
const stats: MigrationStats = {
changedAuctions: 0,
changedHistoryEntries: 0,
duplicateHistorySequences: 0,
scannedAuctions: 0,
scannedHistoryEntries: 0,
};
stats.duplicateHistorySequences = await countDuplicateHistorySequences(
db.collection('auctionHistory'),
options.limit,
);
if (!dryRun && stats.duplicateHistorySequences > 0) {
throw new Error(
`Duplicate auctionHistory sequence entries found: ${stats.duplicateHistorySequences}`,
);
}
await migrateCollection(
db.collection('auction'),
options.limit,
dryRun,
stats,
'auction',
);
await migrateCollection(
db.collection('auctionHistory'),
options.limit,
dryRun,
stats,
'auctionHistory',
);
return stats;
}
async function migrateCollection(
collection: MigrationCollection,
limit: number | undefined,
dryRun: boolean,
stats: MigrationStats,
kind: 'auction' | 'auctionHistory',
) {
const writes: Array<{
updateOne: {
filter: { _id: unknown };
update: UpdateDoc;
};
}> = [];
for await (const doc of collection.find({}).limit(limit ?? 0)) {
if (kind === 'auction') stats.scannedAuctions += 1;
if (kind === 'auctionHistory') stats.scannedHistoryEntries += 1;
const update =
kind === 'auction' ? migrateAuctionDoc(doc) : migrateHistoryDoc(doc);
if (!update) continue;
if (kind === 'auction') stats.changedAuctions += 1;
if (kind === 'auctionHistory') stats.changedHistoryEntries += 1;
writes.push({
updateOne: {
filter: { _id: doc._id },
update,
},
});
}
if (!dryRun && writes.length > 0) {
await collection.bulkWrite(writes, { ordered: false });
}
}
async function countDuplicateHistorySequences(
collection: MigrationCollection,
limit: number | undefined,
): Promise<number> {
const seen = new Set<string>();
const duplicates = new Set<string>();
for await (const doc of collection.find({}).limit(limit ?? 0)) {
const auctionId = stringifyId(doc.auctionId);
const sequence = typeof doc.sequence === 'number' ? doc.sequence : undefined;
if (!auctionId || sequence === undefined) continue;
const key = `${auctionId}:${sequence}`;
if (seen.has(key)) duplicates.add(key);
seen.add(key);
}
return duplicates.size;
}
function migrateAuctionDoc(doc: AnyDoc): UpdateDoc | null {
const set: Record<string, unknown> = {};
const unset: Record<string, ''> = {};
for (const rename of AUCTION_FIELD_RENAMES) {
renameField(doc, rename.from, rename.to, set, unset, rename.transform);
}
setDefault(doc, set, 'revision', 0);
setDefault(doc, set, 'currentAuctionHistorySequence', 0);
setDefault(doc, set, 'auctionEngineVersion', 'legacy-renamed');
setDefault(doc, set, 'auctionLifecycleId', legacyAuctionLifecycleId(doc._id));
setDefault(doc, set, 'matchSideEffectStatus', 'none');
setDefault(doc, set, 'matchSideEffectRevision', 0);
return buildUpdate(set, unset);
}
function migrateHistoryDoc(doc: AnyDoc): UpdateDoc | null {
const set: Record<string, unknown> = {};
const unset: Record<string, ''> = {};
const auctionId = stringifyId(doc.auctionId);
const sequence = typeof doc.sequence === 'number' ? doc.sequence : 0;
setDefault(
doc,
set,
'auctionLifecycleId',
auctionId ? legacyAuctionLifecycleId(auctionId) : undefined,
);
setDefault(
doc,
set,
'auctionStateTransitionId',
auctionId ? `legacy-history:${auctionId}:${sequence}` : undefined,
);
setDefault(doc, set, 'revision', sequence);
setDefault(doc, set, 'historyMutation', { type: 'append' });
setDefault(doc, set, 'matchSideEffectMutation', { type: 'none' });
const snapshotBefore = isRecord(doc.snapshotBefore)
? migrateAuctionSnapshot(doc.snapshotBefore)
: undefined;
if (snapshotBefore?.changed) {
set.snapshotBefore = snapshotBefore.value;
}
return buildUpdate(set, unset);
}
function migrateAuctionSnapshot(snapshot: Record<string, unknown>): {
changed: boolean;
value: Record<string, unknown>;
} {
const value = { ...snapshot };
let changed = false;
for (const rename of AUCTION_FIELD_RENAMES) {
if (!(rename.from in value)) continue;
if (!(rename.to in value)) {
value[rename.to] = rename.transform
? rename.transform(value[rename.from])
: value[rename.from];
}
delete value[rename.from];
changed = true;
}
return { changed, value };
}
function renameField(
doc: Record<string, unknown>,
from: string,
to: string,
set: Record<string, unknown>,
unset: Record<string, ''>,
transform?: (value: unknown) => unknown,
) {
if (!(from in doc)) return;
if (!(to in doc)) {
set[to] = transform ? transform(doc[from]) : doc[from];
}
unset[from] = '';
}
function setDefault(
doc: Record<string, unknown>,
set: Record<string, unknown>,
key: string,
value: unknown,
) {
if (value === undefined || key in doc || key in set) return;
set[key] = value;
}
function buildUpdate(
set: Record<string, unknown>,
unset: Record<string, ''>,
): UpdateDoc | null {
const update: UpdateDoc = {};
if (Object.keys(set).length > 0) update.$set = set;
if (Object.keys(unset).length > 0) update.$unset = unset;
return update.$set || update.$unset ? update : null;
}
function transformAuctionTeams(value: unknown): unknown[] {
return asArray(value).map((team) => {
const next = { ...team };
if ('points' in next && !('remainingBidPointAmount' in next)) {
next.remainingBidPointAmount = next.points;
}
delete next.points;
next.members = asArray(next.members).map((member) => {
const migratedMember = { ...member };
if (
'cost' in migratedMember &&
!('memberAcquisitionBidPointAmount' in migratedMember)
) {
migratedMember.memberAcquisitionBidPointAmount = migratedMember.cost;
}
delete migratedMember.cost;
return migratedMember;
});
return next;
});
}
function transformAuctionBidHistory(value: unknown): unknown[] {
return asArray(value).map((bid) => {
const next: Record<string, unknown> = {};
if ('teamIndex' in bid) next.bidderTeamIndex = bid.teamIndex;
if ('amount' in bid) next.bidPointAmount = bid.amount;
if ('bidderUserId' in bid) next.bidderUserId = bid.bidderUserId;
if ('timestamp' in bid) {
next.bidPlacedAtTimestampMS = toTimestampMS(bid.timestamp);
}
return next;
});
}
function toTimestampMS(value: unknown): unknown {
if (value instanceof Date) return value.getTime();
if (typeof value === 'number') return value;
if (typeof value === 'string') {
const parsed = Date.parse(value);
return Number.isNaN(parsed) ? value : parsed;
}
return value;
}
function legacyAuctionLifecycleId(value: unknown): string {
return `legacy-auction:${stringifyId(value) ?? 'unknown'}`;
}
function stringifyId(value: unknown): string | undefined {
if (value === undefined || value === null) return undefined;
return typeof value === 'string' ? value : String(value);
}
function asArray(value: unknown): Record<string, unknown>[] {
return Array.isArray(value)
? value.filter(isRecord).map((item) => ({ ...item }))
: [];
}
function isRecord(value: unknown): value is Record<string, unknown> {
return Boolean(value && typeof value === 'object' && !Array.isArray(value));
}
@@ -0,0 +1,363 @@
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;
});