Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Analytics index creation after migration #31193

Merged
merged 3 commits into from
Dec 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading