Skip to content

Commit

Permalink
fix: Analytics index creation after migration (#31193)
Browse files Browse the repository at this point in the history
  • Loading branch information
ggazzo authored Dec 8, 2023
1 parent c12f15d commit 715328b
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 8 deletions.
20 changes: 12 additions & 8 deletions apps/meteor/server/models/raw/BaseRaw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<TDeleted>, options?: ModelOptions) {
constructor(private db: Db, protected name: string, protected trash?: Collection<TDeleted>, 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 {
Expand Down
1 change: 1 addition & 0 deletions apps/meteor/server/startup/migrations/v304.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
},
});
2 changes: 2 additions & 0 deletions packages/model-typings/src/models/IBaseModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ export interface IBaseModel<
> {
col: Collection<T>;

createIndexes(): Promise<string[] | void>;

getCollectionName(): string;

findOneAndUpdate(query: Filter<T>, update: UpdateFilter<T> | T, options?: FindOneAndUpdateOptions): Promise<ModifyResult<T>>;
Expand Down

0 comments on commit 715328b

Please sign in to comment.