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] Database indexes not being created #25101

Merged
merged 2 commits into from
Apr 13, 2022
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
6 changes: 4 additions & 2 deletions app/models/server/raw/Analytics.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import { Random } from 'meteor/random';
import { AggregationCursor, Cursor, SortOptionObject, UpdateWriteOpResult } from 'mongodb';

import { BaseRaw, IndexSpecification } from './BaseRaw';
import { BaseRaw } from './BaseRaw';
import { IAnalytic } from '../../../../definition/IAnalytic';
import { IRoom } from '../../../../definition/IRoom';

type T = IAnalytic;

export class AnalyticsRaw extends BaseRaw<T> {
protected indexes: IndexSpecification[] = [{ key: { date: 1 } }, { key: { 'room._id': 1, 'date': 1 }, unique: true }];
protected modelIndexes() {
return [{ key: { date: 1 } }, { key: { 'room._id': 1, 'date': 1 }, unique: true }];
}

saveMessageSent({ room, date }: { room: IRoom; date: IAnalytic['date'] }): Promise<UpdateWriteOpResult> {
return this.updateMany(
Expand Down
12 changes: 7 additions & 5 deletions app/models/server/raw/Avatars.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { DeleteWriteOpResultObject, UpdateWriteOpResult } from 'mongodb';

import { BaseRaw, IndexSpecification } from './BaseRaw';
import { BaseRaw } from './BaseRaw';
import { IAvatar as T } from '../../../../definition/IAvatar';

export class AvatarsRaw extends BaseRaw<T> {
protected indexes: IndexSpecification[] = [
{ key: { name: 1 }, sparse: true },
{ key: { rid: 1 }, sparse: true },
];
protected modelIndexes() {
return [
{ key: { name: 1 }, sparse: true },
{ key: { rid: 1 }, sparse: true },
];
}

insertAvatarFileInit(name: string, userId: string, store: string, file: { name: string }, extra: object): Promise<UpdateWriteOpResult> {
const fileData = {
Expand Down
10 changes: 3 additions & 7 deletions app/models/server/raw/Banners.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
import { Collection, Cursor, FindOneOptions, UpdateWriteOpResult, WithoutProjection, InsertOneWriteOpResult } from 'mongodb';
import { Cursor, FindOneOptions, UpdateWriteOpResult, WithoutProjection, InsertOneWriteOpResult } from 'mongodb';

import { BannerPlatform, IBanner } from '../../../../definition/IBanner';
import { BaseRaw } from './BaseRaw';

type T = IBanner;
export class BannersRaw extends BaseRaw<T> {
constructor(public readonly col: Collection<T>, trash?: Collection<T>) {
super(col, trash);

this.col.createIndexes([{ key: { platform: 1, startAt: 1, expireAt: 1 } }]);

this.col.createIndexes([{ key: { platform: 1, startAt: 1, expireAt: 1, active: 1 } }]);
protected modelIndexes() {
return [{ key: { platform: 1, startAt: 1, expireAt: 1 } }, { key: { platform: 1, startAt: 1, expireAt: 1, active: 1 } }];
}

create(doc: IBanner): Promise<InsertOneWriteOpResult<IBanner>> {
Expand Down
8 changes: 3 additions & 5 deletions app/models/server/raw/BannersDismiss.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import { Collection, Cursor, FindOneOptions, WithoutProjection } from 'mongodb';
import { Cursor, FindOneOptions, WithoutProjection } from 'mongodb';

import { IBannerDismiss } from '../../../../definition/IBanner';
import { BaseRaw } from './BaseRaw';

export class BannersDismissRaw extends BaseRaw<IBannerDismiss> {
constructor(public readonly col: Collection<IBannerDismiss>, trash?: Collection<IBannerDismiss>) {
super(col, trash);

this.col.createIndexes([{ key: { userId: 1, bannerId: 1 } }]);
modelIndexes() {
return [{ key: { userId: 1, bannerId: 1 } }];
}

findByUserIdAndBannerId(userId: string, bannerIds: string[]): Cursor<IBannerDismiss>;
Expand Down
11 changes: 7 additions & 4 deletions app/models/server/raw/BaseRaw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,6 @@ const warnFields =
export class BaseRaw<T, C extends DefaultFields<T> = undefined> implements IBaseRaw<T> {
public readonly defaultFields: C;

protected indexes?: IndexSpecification[];

protected name: string;

private preventSetUpdatedAt: boolean;
Expand All @@ -85,13 +83,18 @@ export class BaseRaw<T, C extends DefaultFields<T> = undefined> implements IBase
this.name = this.col.collectionName.replace(baseName, '');
this.trash = trash as unknown as Collection<RocketChatRecordDeleted<T>>;

if (this.indexes?.length) {
this.col.createIndexes(this.indexes);
const indexes = this.modelIndexes();
if (indexes?.length) {
this.col.createIndexes(indexes);
}

this.preventSetUpdatedAt = options?.preventSetUpdatedAt ?? false;
}

protected modelIndexes(): IndexSpecification[] | void {
// noop
}

private doNotMixInclusionAndExclusionFields(options: FindOneOptions<T> = {}): FindOneOptions<T> {
const optionsDef = this.ensureDefaultFields(options);
if (optionsDef?.projection === undefined) {
Expand Down
6 changes: 4 additions & 2 deletions app/models/server/raw/CredentialTokens.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { BaseRaw, IndexSpecification } from './BaseRaw';
import { BaseRaw } from './BaseRaw';
import { ICredentialToken as T } from '../../../../definition/ICredentialToken';

export class CredentialTokensRaw extends BaseRaw<T> {
protected indexes: IndexSpecification[] = [{ key: { expireAt: 1 }, sparse: true, expireAfterSeconds: 0 }];
protected modelIndexes() {
return [{ key: { expireAt: 1 }, sparse: true, expireAfterSeconds: 0 }];
}

async create(_id: string, userInfo: T['userInfo']): Promise<T> {
const validForMilliseconds = 60000; // Valid for 60 seconds
Expand Down
6 changes: 4 additions & 2 deletions app/models/server/raw/CustomSounds.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { Cursor, FindOneOptions, InsertOneWriteOpResult, UpdateWriteOpResult, WithId, WithoutProjection } from 'mongodb';

import { BaseRaw, IndexSpecification } from './BaseRaw';
import { BaseRaw } from './BaseRaw';
import { ICustomSound as T } from '../../../../definition/ICustomSound';

export class CustomSoundsRaw extends BaseRaw<T> {
protected indexes: IndexSpecification[] = [{ key: { name: 1 } }];
protected modelIndexes() {
return [{ key: { name: 1 } }];
}

// find
findByName(name: string, options: WithoutProjection<FindOneOptions<T>>): Cursor<T> {
Expand Down
6 changes: 4 additions & 2 deletions app/models/server/raw/CustomUserStatus.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { Cursor, FindOneOptions, InsertOneWriteOpResult, UpdateWriteOpResult, WithId, WithoutProjection } from 'mongodb';

import { BaseRaw, IndexSpecification } from './BaseRaw';
import { BaseRaw } from './BaseRaw';
import { ICustomUserStatus as T } from '../../../../definition/ICustomUserStatus';

export class CustomUserStatusRaw extends BaseRaw<T> {
protected indexes: IndexSpecification[] = [{ key: { name: 1 } }];
protected modelIndexes() {
return [{ key: { name: 1 } }];
}

// find one by name
async findOneByName(name: string, options: WithoutProjection<FindOneOptions<T>>): Promise<T | null> {
Expand Down
6 changes: 4 additions & 2 deletions app/models/server/raw/EmailInbox.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { BaseRaw, IndexSpecification } from './BaseRaw';
import { BaseRaw } from './BaseRaw';
import { IEmailInbox } from '../../../../definition/IEmailInbox';

export class EmailInboxRaw extends BaseRaw<IEmailInbox> {
protected indexes: IndexSpecification[] = [{ key: { email: 1 }, unique: true }];
protected modelIndexes() {
return [{ key: { email: 1 }, unique: true }];
}
}
6 changes: 4 additions & 2 deletions app/models/server/raw/EmailMessageHistory.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { IndexSpecification, InsertOneWriteOpResult, WithId } from 'mongodb';
import { InsertOneWriteOpResult, WithId } from 'mongodb';

import { BaseRaw } from './BaseRaw';
import { IEmailMessageHistory as T } from '../../../../definition/IEmailMessageHistory';

export class EmailMessageHistoryRaw extends BaseRaw<T> {
protected indexes: IndexSpecification[] = [{ key: { createdAt: 1 }, expireAfterSeconds: 60 * 60 * 24 }];
protected modelIndexes() {
return [{ key: { createdAt: 1 }, expireAfterSeconds: 60 * 60 * 24 }];
}

async create({ _id, email }: T): Promise<InsertOneWriteOpResult<WithId<T>>> {
return this.insertOne({
Expand Down
6 changes: 4 additions & 2 deletions app/models/server/raw/EmojiCustom.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { Cursor, FindOneOptions, InsertOneWriteOpResult, UpdateWriteOpResult, WithId, WithoutProjection } from 'mongodb';

import { BaseRaw, IndexSpecification } from './BaseRaw';
import { BaseRaw } from './BaseRaw';
import { IEmojiCustom as T } from '../../../../definition/IEmojiCustom';

export class EmojiCustomRaw extends BaseRaw<T> {
protected indexes: IndexSpecification[] = [{ key: { name: 1 } }, { key: { aliases: 1 } }, { key: { extension: 1 } }];
protected modelIndexes() {
return [{ key: { name: 1 } }, { key: { aliases: 1 } }, { key: { extension: 1 } }];
}

// find
findByNameOrAlias(emojiName: string, options: WithoutProjection<FindOneOptions<T>>): Cursor<T> {
Expand Down
6 changes: 4 additions & 2 deletions app/models/server/raw/ExportOperations.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { Cursor, UpdateWriteOpResult } from 'mongodb';

import { BaseRaw, IndexSpecification } from './BaseRaw';
import { BaseRaw } from './BaseRaw';
import { IExportOperation } from '../../../../definition/IExportOperation';

type T = IExportOperation;

export class ExportOperationsRaw extends BaseRaw<T> {
protected indexes: IndexSpecification[] = [{ key: { userId: 1 } }, { key: { status: 1 } }];
protected modelIndexes() {
return [{ key: { userId: 1 } }, { key: { status: 1 } }];
}

findOnePending(): Promise<T | null> {
const query = {
Expand Down
6 changes: 4 additions & 2 deletions app/models/server/raw/FederationServers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ import { UpdateWriteOpResult } from 'mongodb';

import { Users } from './index';
import { IFederationServer } from '../../../../definition/Federation';
import { BaseRaw, IndexSpecification } from './BaseRaw';
import { BaseRaw } from './BaseRaw';

export class FederationServersRaw extends BaseRaw<IFederationServer> {
protected indexes: IndexSpecification[] = [{ key: { domain: 1 } }];
protected modelIndexes() {
return [{ key: { domain: 1 } }];
}

saveDomain(domain: string): Promise<UpdateWriteOpResult> {
return this.updateOne(
Expand Down
6 changes: 4 additions & 2 deletions app/models/server/raw/IntegrationHistory.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { BaseRaw, IndexSpecification } from './BaseRaw';
import { BaseRaw } from './BaseRaw';
import { IIntegrationHistory } from '../../../../definition/IIntegrationHistory';

export class IntegrationHistoryRaw extends BaseRaw<IIntegrationHistory> {
protected indexes: IndexSpecification[] = [{ key: { 'integration._id': 1, 'integration._createdBy._id': 1 } }];
protected modelIndexes() {
return [{ key: { 'integration._id': 1, 'integration._createdBy._id': 1 } }];
}

removeByIntegrationId(integrationId: string): ReturnType<BaseRaw<IIntegrationHistory>['deleteMany']> {
return this.deleteMany({ 'integration._id': integrationId });
Expand Down
6 changes: 4 additions & 2 deletions app/models/server/raw/Integrations.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { BaseRaw, IndexSpecification } from './BaseRaw';
import { BaseRaw } from './BaseRaw';
import { IIntegration } from '../../../../definition/IIntegration';

export class IntegrationsRaw extends BaseRaw<IIntegration> {
protected indexes: IndexSpecification[] = [{ key: { type: 1 } }];
protected modelIndexes() {
return [{ key: { type: 1 } }];
}

findOneByUrl(url: string): Promise<IIntegration | null> {
return this.findOne({ url });
Expand Down
6 changes: 4 additions & 2 deletions app/models/server/raw/LivechatTrigger.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { Cursor, UpdateWriteOpResult } from 'mongodb';

import { BaseRaw, IndexSpecification } from './BaseRaw';
import { BaseRaw } from './BaseRaw';
import { ILivechatTrigger } from '../../../../definition/ILivechatTrigger';

export class LivechatTriggerRaw extends BaseRaw<ILivechatTrigger> {
protected indexes: IndexSpecification[] = [{ key: { enabled: 1 } }];
protected modelIndexes() {
return [{ key: { enabled: 1 } }];
}

findEnabled(): Cursor<ILivechatTrigger> {
return this.find({ enabled: true });
Expand Down
18 changes: 10 additions & 8 deletions app/models/server/raw/NotificationQueue.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import { UpdateWriteOpResult } from 'mongodb';

import { BaseRaw, IndexSpecification } from './BaseRaw';
import { BaseRaw } from './BaseRaw';
import { INotification } from '../../../../definition/INotification';

export class NotificationQueueRaw extends BaseRaw<INotification> {
protected indexes: IndexSpecification[] = [
{ key: { uid: 1 } },
{ key: { ts: 1 }, expireAfterSeconds: 2 * 60 * 60 },
{ key: { schedule: 1 }, sparse: true },
{ key: { sending: 1 }, sparse: true },
{ key: { error: 1 }, sparse: true },
];
protected modelIndexes() {
return [
{ key: { uid: 1 } },
{ key: { ts: 1 }, expireAfterSeconds: 2 * 60 * 60 },
{ key: { schedule: 1 }, sparse: true },
{ key: { sending: 1 }, sparse: true },
{ key: { error: 1 }, sparse: true },
];
}

unsetSendingById(_id: string): Promise<UpdateWriteOpResult> {
return this.updateOne(
Expand Down
8 changes: 3 additions & 5 deletions app/models/server/raw/Nps.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import { UpdateWriteOpResult, Collection } from 'mongodb';
import { UpdateWriteOpResult } from 'mongodb';

import { BaseRaw } from './BaseRaw';
import { INps, NPSStatus } from '../../../../definition/INps';

type T = INps;
export class NpsRaw extends BaseRaw<T> {
constructor(public readonly col: Collection<T>, trash?: Collection<T>) {
super(col, trash);

this.col.createIndexes([{ key: { status: 1, expireAt: 1 } }]);
modelIndexes() {
return [{ key: { status: 1, expireAt: 1 } }];
}

// get expired surveys still in progress
Expand Down
8 changes: 3 additions & 5 deletions app/models/server/raw/NpsVote.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import { ObjectId, Collection, Cursor, FindOneOptions, UpdateWriteOpResult, WithoutProjection } from 'mongodb';
import { ObjectId, Cursor, FindOneOptions, UpdateWriteOpResult, WithoutProjection } from 'mongodb';

import { INpsVote, INpsVoteStatus } from '../../../../definition/INps';
import { BaseRaw } from './BaseRaw';

type T = INpsVote;
export class NpsVoteRaw extends BaseRaw<T> {
constructor(public readonly col: Collection<T>, trash?: Collection<T>) {
super(col, trash);

this.col.createIndexes([{ key: { npsId: 1, status: 1, sentAt: 1 } }, { key: { npsId: 1, identifier: 1 } }]);
modelIndexes() {
return [{ key: { npsId: 1, status: 1, sentAt: 1 } }, { key: { npsId: 1, identifier: 1 } }];
}

findNotSentByNpsId(npsId: string, options?: WithoutProjection<FindOneOptions<T>>): Cursor<T> {
Expand Down
6 changes: 4 additions & 2 deletions app/models/server/raw/OEmbedCache.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { DeleteWriteOpResultObject } from 'mongodb';

import { BaseRaw, IndexSpecification } from './BaseRaw';
import { BaseRaw } from './BaseRaw';
import { IOEmbedCache } from '../../../../definition/IOEmbedCache';

type T = IOEmbedCache;

export class OEmbedCacheRaw extends BaseRaw<T> {
protected indexes: IndexSpecification[] = [{ key: { updatedAt: 1 } }];
protected modelIndexes() {
return [{ key: { updatedAt: 1 } }];
}

async createWithIdAndData(_id: string, data: any): Promise<T> {
const record = {
Expand Down
6 changes: 4 additions & 2 deletions app/models/server/raw/PbxEvents.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { Cursor } from 'mongodb';

import { BaseRaw, IndexSpecification } from './BaseRaw';
import { BaseRaw } from './BaseRaw';
import { IPbxEvent } from '../../../../definition/IPbxEvent';

export class PbxEventsRaw extends BaseRaw<IPbxEvent> {
protected indexes: IndexSpecification[] = [{ key: { uniqueId: 1 }, unique: true }];
protected modelIndexes() {
return [{ key: { uniqueId: 1 }, unique: true }];
}

findByEvents(callUniqueId: string, events: string[]): Cursor<IPbxEvent> {
return this.find(
Expand Down
6 changes: 4 additions & 2 deletions app/models/server/raw/ReadReceipts.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { Cursor } from 'mongodb';

import { BaseRaw, IndexSpecification } from './BaseRaw';
import { BaseRaw } from './BaseRaw';
import { ReadReceipt } from '../../../../definition/ReadReceipt';

export class ReadReceiptsRaw extends BaseRaw<ReadReceipt> {
protected indexes: IndexSpecification[] = [{ key: { roomId: 1, userId: 1, messageId: 1 }, unique: true }, { key: { messageId: 1 } }];
protected modelIndexes() {
return [{ key: { roomId: 1, userId: 1, messageId: 1 }, unique: true }, { key: { messageId: 1 } }];
}

findByMessageId(messageId: string): Cursor<ReadReceipt> {
return this.find({ messageId });
Expand Down
6 changes: 4 additions & 2 deletions app/models/server/raw/ServerEvents.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { BaseRaw, IndexSpecification } from './BaseRaw';
import { BaseRaw } from './BaseRaw';
import { IServerEvent, IServerEventType } from '../../../../definition/IServerEvent';

export class ServerEventsRaw extends BaseRaw<IServerEvent> {
protected indexes: IndexSpecification[] = [{ key: { t: 1, ip: 1, ts: -1 } }, { key: { 't': 1, 'u.username': 1, 'ts': -1 } }];
protected modelIndexes() {
return [{ key: { t: 1, ip: 1, ts: -1 } }, { key: { 't': 1, 'u.username': 1, 'ts': -1 } }];
}

async findLastFailedAttemptByIp(ip: string): Promise<IServerEvent | null> {
return this.findOne<IServerEvent>(
Expand Down
Loading