diff --git a/.changeset/fresh-maps-rhyme.md b/.changeset/fresh-maps-rhyme.md new file mode 100644 index 000000000000..2a272a3f81a2 --- /dev/null +++ b/.changeset/fresh-maps-rhyme.md @@ -0,0 +1,5 @@ +--- +"@rocket.chat/meteor": patch +--- + +fix: multiple indexes creation error during 304 migration diff --git a/apps/meteor/server/models/raw/BaseRaw.ts b/apps/meteor/server/models/raw/BaseRaw.ts index e43bac39b339..5ab3b9802105 100644 --- a/apps/meteor/server/models/raw/BaseRaw.ts +++ b/apps/meteor/server/models/raw/BaseRaw.ts @@ -78,6 +78,8 @@ export abstract class BaseRaw< this.preventSetUpdatedAt = options?.preventSetUpdatedAt ?? false; } + private pendingIndexes: Promise | undefined; + public async createIndexes() { const indexes = this.modelIndexes(); if (this.options?._updatedAtIndexOptions) { @@ -85,7 +87,17 @@ export abstract class BaseRaw< } if (indexes?.length) { - return this.col.createIndexes(indexes); + if (this.pendingIndexes) { + await this.pendingIndexes; + } + + this.pendingIndexes = this.col.createIndexes(indexes) as unknown as Promise; + + void this.pendingIndexes.finally(() => { + this.pendingIndexes = undefined; + }); + + return this.pendingIndexes; } } diff --git a/apps/meteor/server/startup/migrations/v304.ts b/apps/meteor/server/startup/migrations/v304.ts index db9aa44b4ee0..48cb217643d0 100644 --- a/apps/meteor/server/startup/migrations/v304.ts +++ b/apps/meteor/server/startup/migrations/v304.ts @@ -6,6 +6,16 @@ addMigration({ version: 304, name: 'Drop wrong index from analytics collection', async up() { + const indexes = await Analytics.col.indexes(); + + if ( + indexes.find( + (index) => index.name === 'room._id_1_date_1' && index.partialFilterExpression && index.partialFilterExpression.type === 'rooms', + ) + ) { + return; + } + await Analytics.col.dropIndex('room._id_1_date_1'); await Analytics.createIndexes(); },