interface MigrationCollection { find(filter: Record): { limit(limit: number): AsyncIterable; }; bulkWrite( writes: Array<{ updateOne: { filter: { _id: unknown }; update: { $set?: Record; $unset?: Record }; }; }>, options: { ordered: boolean }, ): Promise; } 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 & { _id: unknown }; type UpdateDoc = { $set?: Record; $unset?: Record }; 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 { 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 { const seen = new Set(); const duplicates = new Set(); 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 = {}; const unset: Record = {}; 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 = {}; const unset: Record = {}; 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): { changed: boolean; value: Record; } { 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, from: string, to: string, set: Record, unset: Record, 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, set: Record, key: string, value: unknown, ) { if (value === undefined || key in doc || key in set) return; set[key] = value; } function buildUpdate( set: Record, unset: Record, ): 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 = {}; 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[] { return Array.isArray(value) ? value.filter(isRecord).map((item) => ({ ...item })) : []; } function isRecord(value: unknown): value is Record { return Boolean(value && typeof value === 'object' && !Array.isArray(value)); }