From ee96f8d48b2bb79fdcf59f1f099e750e05d97cec Mon Sep 17 00:00:00 2001 From: Park Dahyeon Date: Mon, 13 Jul 2026 03:13:28 +0000 Subject: [PATCH] fix: normalize missing legacy match game mode --- .../competition-domain-runner.spec.ts | 55 +++++++++++++++++++ .../migrations/competition-domain-runner.ts | 4 ++ docs/specs/competition-result-domain.md | 7 +++ 3 files changed, 66 insertions(+) diff --git a/apps/api/src/modules/global/database/migrations/competition-domain-runner.spec.ts b/apps/api/src/modules/global/database/migrations/competition-domain-runner.spec.ts index c0d65a78..88e60845 100644 --- a/apps/api/src/modules/global/database/migrations/competition-domain-runner.spec.ts +++ b/apps/api/src/modules/global/database/migrations/competition-domain-runner.spec.ts @@ -39,6 +39,61 @@ describe('migrateCompetitionDomain', () => { expect(db.createdIndexes).toHaveLength(0); }); + it.each([ + ['absent', (match: AnyDoc) => delete match.gameMode], + ['null', (match: AnyDoc) => (match.gameMode = null)], + ])( + 'restores the historical pair default when raw Mongo Match gameMode is %s', + async (_label, omitGameMode) => { + const match = readyMatch(); + omitGameMode(match); + const before = canonical(match); + const db = new MemoryMigrationDb({ + match: [match], + riotMatch: [riotArchive()], + tournamentCode: [usedTournamentCode(match)], + }); + + const result = await migrateCompetitionDomain(db); + + expect(result.summary).toMatchObject({ + scanned: 1, + ready: 1, + quarantined: 0, + failed: 0, + }); + expect(result.matches[0]).toMatchObject({ + status: 'ready', + factCount: 1, + assignmentCount: 1, + }); + expect(canonical(db.docs('match')[0])).toBe(before); + expect(db.writeCount).toBe(0); + }, + ); + + it('keeps an explicitly unknown raw Mongo Match gameMode quarantined', async () => { + const match = readyMatch(); + match.gameMode = 'future-mode'; + const db = new MemoryMigrationDb({ match: [match] }); + + const result = await migrateCompetitionDomain(db); + + expect(result.summary).toMatchObject({ + scanned: 1, + ready: 0, + quarantined: 1, + failed: 0, + }); + expect(result.matches[0]).toMatchObject({ + status: 'quarantined', + reasons: [ + expect.objectContaining({ code: 'UNSUPPORTED_LEGACY_GAME_MODE' }), + ], + }); + expect(db.writeCount).toBe(0); + }); + it('applies one Match atomically and converges on an already-applied replay', async () => { const match = readyMatch(); const originalResult = canonical(match.result); diff --git a/apps/api/src/modules/global/database/migrations/competition-domain-runner.ts b/apps/api/src/modules/global/database/migrations/competition-domain-runner.ts index 542bc458..79bf1bac 100644 --- a/apps/api/src/modules/global/database/migrations/competition-domain-runner.ts +++ b/apps/api/src/modules/global/database/migrations/competition-domain-runner.ts @@ -759,6 +759,10 @@ async function hydrateLegacyMatch( return { ...match, + // Native collection reads bypass the MatchSchema default. Historical + // documents without a persisted gameMode still executed as pair mode via + // the runtime fallback. + gameMode: match.gameMode ?? 'pair', ...(hydratedResult ? { result: hydratedResult } : {}), tournamentCodes, riotMatchArchiveIds: [...archivedIds].sort(), diff --git a/docs/specs/competition-result-domain.md b/docs/specs/competition-result-domain.md index bc536845..d5b33221 100644 --- a/docs/specs/competition-result-domain.md +++ b/docs/specs/competition-result-domain.md @@ -269,6 +269,10 @@ Migration builds new collections without dual canonical writes. - Ambiguous mappings, evidence conflicts, or already-played invalid descendants are quarantined. - Legacy `bracket` format becomes `paired-series` with a warning. +- Native Mongo migration reads do not receive Mongoose defaults. The hydration + boundary restores the historical Match default only when `gameMode` is absent or + `null`, mapping it to `pair`; an explicitly stored unknown mode remains + quarantined instead of being guessed. - Dry-run is the default and emits machine-readable and human-readable reports. - Apply is resumable, idempotent, transactionally scoped per Match, and verifies fact replay equality after every write. @@ -361,6 +365,9 @@ its `teamId` in the same object. - Double-elimination seeds are persisted before the finalized team-build timestamp is exposed, and an expired reserved code can be provisioned again automatically. - Migration dry-run reports zero unclassified documents before apply. +- Historical Matches without a persisted `gameMode` plan as `paired-series`, while + an explicitly invalid legacy mode is still reported as + `UNSUPPORTED_LEGACY_GAME_MODE`. - Competition unique/partial indexes are verified before the first apply write, and repeated limited apply runs advance without rescanning the first page. - API tests and API/web production builds pass.