From 715328b6d50f1102f64622e6e75d395651560bab Mon Sep 17 00:00:00 2001 From: Guilherme Gazzo Date: Fri, 8 Dec 2023 17:02:51 -0300 Subject: [PATCH] fix: Analytics index creation after migration (#31193) --- apps/meteor/server/models/raw/BaseRaw.ts | 20 +++++++++++-------- apps/meteor/server/startup/migrations/v304.ts | 1 + .../model-typings/src/models/IBaseModel.ts | 2 ++ 3 files changed, 15 insertions(+), 8 deletions(-) diff --git a/apps/meteor/server/models/raw/BaseRaw.ts b/apps/meteor/server/models/raw/BaseRaw.ts index bfa5a81ddcf0..ce3e0f224e1e 100644 --- a/apps/meteor/server/models/raw/BaseRaw.ts +++ b/apps/meteor/server/models/raw/BaseRaw.ts @@ -66,23 +66,27 @@ export abstract class BaseRaw< * @param trash Trash collection instance * @param options Model options */ - constructor(private db: Db, protected name: string, protected trash?: Collection, options?: ModelOptions) { + constructor(private db: Db, protected name: string, protected trash?: Collection, private options?: ModelOptions) { this.collectionName = options?.collectionNameResolver ? options.collectionNameResolver(name) : getCollectionName(name); this.col = this.db.collection(this.collectionName, options?.collection || {}); + void this.createIndexes().catch((e) => { + console.warn(`Some indexes for collection '${this.collectionName}' could not be created:\n\t${e.message}`); + }); + + this.preventSetUpdatedAt = options?.preventSetUpdatedAt ?? false; + } + + public async createIndexes() { const indexes = this.modelIndexes(); - if (options?._updatedAtIndexOptions) { - indexes?.push({ ...options._updatedAtIndexOptions, key: { _updatedAt: 1 } }); + if (this.options?._updatedAtIndexOptions) { + indexes?.push({ ...this.options._updatedAtIndexOptions, key: { _updatedAt: 1 } }); } if (indexes?.length) { - this.col.createIndexes(indexes).catch((e) => { - console.warn(`Some indexes for collection '${this.collectionName}' could not be created:\n\t${e.message}`); - }); + return this.col.createIndexes(indexes); } - - this.preventSetUpdatedAt = options?.preventSetUpdatedAt ?? false; } protected modelIndexes(): IndexDescription[] | undefined { diff --git a/apps/meteor/server/startup/migrations/v304.ts b/apps/meteor/server/startup/migrations/v304.ts index e5d6484446f0..db9aa44b4ee0 100644 --- a/apps/meteor/server/startup/migrations/v304.ts +++ b/apps/meteor/server/startup/migrations/v304.ts @@ -7,5 +7,6 @@ addMigration({ name: 'Drop wrong index from analytics collection', async up() { await Analytics.col.dropIndex('room._id_1_date_1'); + await Analytics.createIndexes(); }, }); diff --git a/packages/model-typings/src/models/IBaseModel.ts b/packages/model-typings/src/models/IBaseModel.ts index e0ae05cba0c5..36f34a008508 100644 --- a/packages/model-typings/src/models/IBaseModel.ts +++ b/packages/model-typings/src/models/IBaseModel.ts @@ -45,6 +45,8 @@ export interface IBaseModel< > { col: Collection; + createIndexes(): Promise; + getCollectionName(): string; findOneAndUpdate(query: Filter, update: UpdateFilter | T, options?: FindOneAndUpdateOptions): Promise>;