Skip to content

Commit

Permalink
fix: multiple indexes creations during migrations (#31413)
Browse files Browse the repository at this point in the history
  • Loading branch information
ggazzo authored and sampaiodiego committed Jan 16, 2024
1 parent ebbf210 commit 2a04cc8
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/fresh-maps-rhyme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@rocket.chat/meteor": patch
---

fix: multiple indexes creation error during 304 migration
14 changes: 13 additions & 1 deletion apps/meteor/server/models/raw/BaseRaw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,26 @@ export abstract class BaseRaw<
this.preventSetUpdatedAt = options?.preventSetUpdatedAt ?? false;
}

private pendingIndexes: Promise<void> | undefined;

public async createIndexes() {
const indexes = this.modelIndexes();
if (this.options?._updatedAtIndexOptions) {
indexes?.push({ ...this.options._updatedAtIndexOptions, key: { _updatedAt: 1 } });
}

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>;

void this.pendingIndexes.finally(() => {
this.pendingIndexes = undefined;
});

return this.pendingIndexes;
}
}

Expand Down
10 changes: 10 additions & 0 deletions apps/meteor/server/startup/migrations/v304.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
},
Expand Down

0 comments on commit 2a04cc8

Please sign in to comment.