fix: harden competition migration recovery

This commit is contained in:
dahyeon committed 2026-07-13 04:33:35 +00:00
1 parent bbcb2f43dc
commit 6b1c450752
7 files changed
+1394 -129

No files matched your search

@@ -33,12 +33,73 @@ describe('migrateCompetitionDomain', () => {
factCount: 1,
assignmentCount: 1,
});
if (result.matches[0].status !== 'ready') {
throw new Error('Expected a ready dry-run result.');
}
expect(result.matches[0].planHash).toMatch(/^sha256:[a-f0-9]{64}$/);
const repeated = await migrateCompetitionDomain(db);
expect(repeated.matches[0]).toMatchObject({ status: 'ready' });
if (repeated.matches[0].status !== 'ready') {
throw new Error('Expected a repeated ready dry-run result.');
}
expect(repeated.matches[0].planHash).toBe(result.matches[0].planHash);
expect(startSession).not.toHaveBeenCalled();
expect(db.writeCount).toBe(0);
expect(db.docs('competition')).toHaveLength(0);
expect(db.createdIndexes).toHaveLength(0);
});
it('reports apply-time global identity conflicts during dry-run without writes', async () => {
const riotMatchId = 'KR_DRY_RUN_GLOBAL_CONFLICT';
const match = readyMatch(riotMatchId);
const conflictingFactId = objectId();
const db = new MemoryMigrationDb({
match: [match],
riotMatch: [riotArchive(riotMatchId)],
tournamentCode: [usedTournamentCode(match, riotMatchId)],
competition_fact: [
{
_id: conflictingFactId,
competitionId: objectId(),
type: 'game-accepted',
evidence: { type: 'riot', riotMatchId },
},
],
});
const dryRun = await migrateCompetitionDomain(db);
expect(dryRun.summary).toMatchObject({
scanned: 1,
ready: 0,
applied: 0,
quarantined: 1,
failed: 0,
});
expect(dryRun.matches[0]).toMatchObject({ status: 'quarantined' });
if (dryRun.matches[0].status !== 'quarantined') {
throw new Error('Expected a quarantined dry-run result.');
}
const conflict = dryRun.matches[0].reasons.find(
({ code }) => code === 'GLOBAL_UNIQUE_INDEX_CONFLICT',
);
expect(conflict?.details?.collection).toBe('competition_fact');
expect(conflict?.details?.indexName).toBe('evidence.riotMatchId_1');
expect(conflict?.details?.conflictingDocumentId).toBe(
conflictingFactId.toHexString(),
);
expect(db.writeCount).toBe(0);
expect(db.createdIndexes).toHaveLength(0);
const applied = await migrateCompetitionDomain(db, {
dryRun: false,
startSession: () => Promise.resolve(new FakeSession()),
});
expect(applied.summary).toMatchObject({ applied: 0, quarantined: 1 });
expect(applied.matches[0]).toEqual(dryRun.matches[0]);
});
it.each([
['absent', (match: AnyDoc) => delete match.gameMode],
['null', (match: AnyDoc) => (match.gameMode = null)],
@@ -157,6 +218,32 @@ describe('migrateCompetitionDomain', () => {
expect(sessions[1]).toMatchObject({ committed: 1, aborted: 0, ended: 1 });
});
it('persists the source voidedAt timestamp without synthesizing it from usedAt', async () => {
const match = readyMatch('KR_VOIDED_TIMESTAMP');
const code = usedTournamentCode(match, 'KR_VOIDED_TIMESTAMP');
code.state = 'voided';
code.voidedAt = new Date('2026-07-03T00:00:00.000Z');
const db = new MemoryMigrationDb({
match: [match],
riotMatch: [riotArchive('KR_VOIDED_TIMESTAMP')],
tournamentCode: [code],
});
const result = await migrateCompetitionDomain(db, {
dryRun: false,
startSession: () => Promise.resolve(new FakeSession()),
});
expect(result.summary).toMatchObject({ applied: 1, failed: 0 });
expect(db.docs('tournament_code_assignment')).toContainEqual(
expect.objectContaining({
state: 'voided',
voidedAt: new Date('2026-07-03T00:00:00.000Z'),
updatedAt: new Date('2026-07-03T00:00:00.000Z'),
}),
);
});
it('quarantines an ambiguous Match without changing any legacy Match field', async () => {
const match = ambiguousMatch();
const before = canonical(match);
@@ -364,6 +451,46 @@ describe('migrateCompetitionDomain', () => {
expect(db.docs('competition_migration_quarantine')).toHaveLength(1);
});
it('detects the same global identity collision between dry-run plans as sequential apply', async () => {
const riotMatchId = 'KR_SHARED_DRY_RUN_MIGRATION';
const firstMatch = readyMatch(riotMatchId);
const secondMatch = readyMatch(riotMatchId);
const db = new MemoryMigrationDb({
match: [firstMatch, secondMatch],
riotMatch: [riotArchive(riotMatchId)],
tournamentCode: [
usedTournamentCode(firstMatch, riotMatchId),
usedTournamentCode(secondMatch, riotMatchId),
],
});
const dryRun = await migrateCompetitionDomain(db);
expect(dryRun.summary).toMatchObject({ ready: 1, quarantined: 1 });
expect(dryRun.matches.map(({ status }) => status)).toEqual([
'ready',
'quarantined',
]);
expect(dryRun.matches[1]).toMatchObject({ status: 'quarantined' });
if (dryRun.matches[1].status !== 'quarantined') {
throw new Error('Expected the second dry-run result to be quarantined.');
}
expect(
dryRun.matches[1].reasons.some(
({ code }) => code === 'GLOBAL_UNIQUE_INDEX_CONFLICT',
),
).toBe(true);
expect(db.writeCount).toBe(0);
const applied = await migrateCompetitionDomain(db, {
dryRun: false,
startSession: () => Promise.resolve(new FakeSession()),
});
expect(applied.summary).toMatchObject({ applied: 1, quarantined: 1 });
expect(applied.matches[1]).toEqual(dryRun.matches[1]);
});
it('resumes repeated limited apply runs after the durable Match checkpoint', async () => {
const firstMatch = readyMatch('KR_BATCH_1');
const secondMatch = readyMatch('KR_BATCH_2');