fix: close competition cutover integrity gaps

This commit is contained in:
dahyeon committed 2026-07-12 21:22:22 +00:00
1 parent 7ea2002975
commit 683d602d63
57 files changed
+4805 -1582

No files matched your search

@@ -20,6 +20,7 @@ export const COMPETITION_OUTBOX_STATES = [
'pending',
'published',
'failed',
'dead-letter',
] as const;
export type CompetitionOutboxState = (typeof COMPETITION_OUTBOX_STATES)[number];
@@ -32,6 +33,110 @@ export const TOURNAMENT_CODE_ASSIGNMENT_STATES = [
export type TournamentCodeAssignmentState =
(typeof TOURNAMENT_CODE_ASSIGNMENT_STATES)[number];
export interface CompetitionDomainInvariantIndex {
collection:
| 'match'
| 'competition'
| 'competition_fact'
| 'competition_outbox'
| 'tournament_code_assignment';
key: mongoose.IndexDefinition;
options: mongoose.IndexOptions & { name: string; unique: true };
}
/**
* Database-enforced Competition identities. Runtime schemas and the one-off
* migration consume this same list so production cannot silently run with a
* weaker set of uniqueness guarantees than command handling expects.
*/
export const COMPETITION_DOMAIN_INVARIANT_INDEXES: CompetitionDomainInvariantIndex[] =
[
{
collection: 'match',
key: { competitionId: 1 },
options: {
name: 'competitionId_1',
unique: true,
partialFilterExpression: {
competitionId: { $type: 'objectId' },
},
},
},
{
collection: 'competition',
key: { matchId: 1 },
options: { name: 'matchId_1', unique: true },
},
{
collection: 'competition_fact',
key: { competitionId: 1, revision: 1, ordinalWithinRevision: 1 },
options: {
name: 'competitionId_1_revision_1_ordinalWithinRevision_1',
unique: true,
},
},
{
collection: 'competition_fact',
key: { competitionId: 1, commandId: 1, ordinalWithinRevision: 1 },
options: {
name: 'competitionId_1_commandId_1_ordinalWithinRevision_1',
unique: true,
},
},
{
collection: 'competition_fact',
key: { 'evidence.riotMatchId': 1 },
options: {
name: 'evidence.riotMatchId_1',
unique: true,
partialFilterExpression: {
type: 'game-accepted',
'evidence.riotMatchId': { $type: 'string' },
},
},
},
{
collection: 'competition_outbox',
key: { eventId: 1 },
options: { name: 'eventId_1', unique: true },
},
{
collection: 'competition_outbox',
key: { competitionId: 1, idempotencyKey: 1 },
options: {
name: 'competitionId_1_idempotencyKey_1',
unique: true,
},
},
{
collection: 'tournament_code_assignment',
key: { gameSlotId: 1 },
options: {
name: 'gameSlotId_1',
unique: true,
partialFilterExpression: { state: { $in: ['reserved', 'used'] } },
},
},
{
collection: 'tournament_code_assignment',
key: { tournamentCodeId: 1 },
options: {
name: 'tournamentCodeId_1',
unique: true,
partialFilterExpression: { state: { $in: ['reserved', 'used'] } },
},
},
{
collection: 'tournament_code_assignment',
key: { riotMatchId: 1 },
options: {
name: 'riotMatchId_1',
unique: true,
partialFilterExpression: { riotMatchId: { $type: 'string' } },
},
},
];
export interface CompetitionTeamMemberSnapshot {
userId: mongoose.Types.ObjectId;
displayName: string;
@@ -200,7 +305,7 @@ export const CompetitionSchema: mongoose.Schema<Competition> =
{ timestamps: true, collection: 'competition' },
);
CompetitionSchema.index({ matchId: 1 }, { unique: true });
applyCompetitionDomainInvariantIndexes(CompetitionSchema, 'competition');
CompetitionSchema.index({ communityId: 1, phase: 1, updatedAt: -1 });
export const CompetitionFactSchema = new Schema<CompetitionFact>(
@@ -238,23 +343,9 @@ export const CompetitionFactSchema = new Schema<CompetitionFact>(
{ timestamps: true, collection: 'competition_fact' },
);
CompetitionFactSchema.index(
{ competitionId: 1, revision: 1, ordinalWithinRevision: 1 },
{ unique: true },
);
CompetitionFactSchema.index(
{ competitionId: 1, commandId: 1, ordinalWithinRevision: 1 },
{ unique: true },
);
CompetitionFactSchema.index(
{ 'evidence.riotMatchId': 1 },
{
unique: true,
partialFilterExpression: {
type: 'game-accepted',
'evidence.riotMatchId': { $type: 'string' },
},
},
applyCompetitionDomainInvariantIndexes(
CompetitionFactSchema,
'competition_fact',
);
CompetitionFactSchema.index({ competitionId: 1, seriesId: 1, recordedAt: 1 });
@@ -286,12 +377,11 @@ export const CompetitionOutboxSchema = new Schema<CompetitionOutbox>(
{ timestamps: true, collection: 'competition_outbox' },
);
CompetitionOutboxSchema.index({ eventId: 1 }, { unique: true });
CompetitionOutboxSchema.index({ state: 1, availableAt: 1 });
CompetitionOutboxSchema.index(
{ competitionId: 1, idempotencyKey: 1 },
{ unique: true },
applyCompetitionDomainInvariantIndexes(
CompetitionOutboxSchema,
'competition_outbox',
);
CompetitionOutboxSchema.index({ state: 1, availableAt: 1 });
export const TournamentCodeAssignmentSchema =
new Schema<TournamentCodeAssignment>(
@@ -323,30 +413,22 @@ export const TournamentCodeAssignmentSchema =
{ timestamps: true, collection: 'tournament_code_assignment' },
);
const activeAssignmentFilter = { state: { $in: ['reserved', 'used'] } };
TournamentCodeAssignmentSchema.index(
{ gameSlotId: 1 },
{
unique: true,
partialFilterExpression: activeAssignmentFilter,
},
);
TournamentCodeAssignmentSchema.index(
{ tournamentCodeId: 1 },
{
unique: true,
partialFilterExpression: activeAssignmentFilter,
},
);
TournamentCodeAssignmentSchema.index(
{ riotMatchId: 1 },
{
unique: true,
partialFilterExpression: { riotMatchId: { $type: 'string' } },
},
applyCompetitionDomainInvariantIndexes(
TournamentCodeAssignmentSchema,
'tournament_code_assignment',
);
TournamentCodeAssignmentSchema.index({ competitionId: 1, state: 1 });
export function applyCompetitionDomainInvariantIndexes(
schema: mongoose.Schema,
collection: CompetitionDomainInvariantIndex['collection'],
): void {
for (const index of COMPETITION_DOMAIN_INVARIANT_INDEXES) {
if (index.collection !== collection) continue;
schema.index(index.key, index.options);
}
}
export const competitionModelDefinitions: Array<{
name: string;
schema: mongoose.Schema;