From 7eb40fc9121e1457fcbf696bfcfb790c534c8aaa Mon Sep 17 00:00:00 2001 From: Bartosz Nowicki <116367402+bn-pass@users.noreply.github.com> Date: Wed, 24 Apr 2024 11:44:10 +0200 Subject: [PATCH] BC-7179 - add missing indexes (#4952) * replace the old compound index on the studentId and homeworkId fields with the new one that checks for the existence of the studentId field * add indexes to the contextId and contextType fields in the boardnodes entity * add index on the createdAt field for the deletion request entity * add more indexes for the File entity * add index for the contents.user field in the Lesson entity * add indexes for the creatorId and updaterId fields in the News entity * add indexes for the teamMembers and courseGroupId fields in the Submission entity * remove contents.user index as mikro-orm cannot make one this way * move index declaration to class level * change compound index to a separate fields indexes --- .../deletion/repo/entity/deletion-request.entity.ts | 1 + apps/server/src/modules/files/entity/file.entity.ts | 3 +++ .../domain/entity/boardnode/column-board-node.entity.ts | 4 +++- .../boardnode/media-board/media-board-node.entity.ts | 4 +++- apps/server/src/shared/domain/entity/news.entity.ts | 2 ++ apps/server/src/shared/domain/entity/submission.entity.ts | 7 ++++++- 6 files changed, 18 insertions(+), 3 deletions(-) diff --git a/apps/server/src/modules/deletion/repo/entity/deletion-request.entity.ts b/apps/server/src/modules/deletion/repo/entity/deletion-request.entity.ts index 3b856e6fb17..618152d987f 100644 --- a/apps/server/src/modules/deletion/repo/entity/deletion-request.entity.ts +++ b/apps/server/src/modules/deletion/repo/entity/deletion-request.entity.ts @@ -16,6 +16,7 @@ export interface DeletionRequestEntityProps { @Entity({ tableName: 'deletionrequests' }) @Unique({ properties: ['targetRefId', 'targetRefDomain'] }) +@Index({ properties: ['createdAt'] }) export class DeletionRequestEntity extends BaseEntityWithTimestamps { @Property() @Index({ options: { expireAfterSeconds: SECONDS_OF_90_DAYS } }) diff --git a/apps/server/src/modules/files/entity/file.entity.ts b/apps/server/src/modules/files/entity/file.entity.ts index 8b4fb8f2aa6..7d118aabc85 100644 --- a/apps/server/src/modules/files/entity/file.entity.ts +++ b/apps/server/src/modules/files/entity/file.entity.ts @@ -37,10 +37,12 @@ export interface FileEntityProps { @Index({ options: { 'permissions.refId': 1 } }) export class FileEntity extends BaseEntityWithTimestamps { @Property({ nullable: true }) + @Index() deletedAt?: Date; // you have to set the type explicitly to boolean, otherwise metadata will be wrong @Property({ type: 'boolean' }) + @Index() deleted = false; // you have to set the type explicitly to boolean, otherwise metadata will be wrong @@ -95,6 +97,7 @@ export class FileEntity extends BaseEntityWithTimestamps { } @Enum({ nullable: false }) + @Index() refOwnerModel: FileOwnerModel; @Property({ fieldName: 'creator', nullable: true }) diff --git a/apps/server/src/shared/domain/entity/boardnode/column-board-node.entity.ts b/apps/server/src/shared/domain/entity/boardnode/column-board-node.entity.ts index 237ae44bd61..c3417942074 100644 --- a/apps/server/src/shared/domain/entity/boardnode/column-board-node.entity.ts +++ b/apps/server/src/shared/domain/entity/boardnode/column-board-node.entity.ts @@ -1,4 +1,4 @@ -import { Entity, Property } from '@mikro-orm/core'; +import { Entity, Index, Property } from '@mikro-orm/core'; import { ObjectId } from '@mikro-orm/mongodb'; import { AnyBoardDo, @@ -13,6 +13,8 @@ import { BoardDoBuilder, BoardNodeType } from './types'; // TODO Use an abstract base class for root nodes that have a contextId and a contextType. Multiple STI abstract base classes are blocked by MikroORM 6.1.2 (issue #3745) @Entity({ discriminatorValue: BoardNodeType.COLUMN_BOARD }) +@Index({ properties: ['_contextId'] }) +@Index({ properties: ['_contextType'] }) export class ColumnBoardNode extends BoardNode implements LearnroomElement { constructor(props: ColumnBoardNodeProps) { super(props); diff --git a/apps/server/src/shared/domain/entity/boardnode/media-board/media-board-node.entity.ts b/apps/server/src/shared/domain/entity/boardnode/media-board/media-board-node.entity.ts index 733013eee26..14ec7ad573e 100644 --- a/apps/server/src/shared/domain/entity/boardnode/media-board/media-board-node.entity.ts +++ b/apps/server/src/shared/domain/entity/boardnode/media-board/media-board-node.entity.ts @@ -1,4 +1,4 @@ -import { Entity, Property } from '@mikro-orm/core'; +import { Entity, Index, Property } from '@mikro-orm/core'; import { ObjectId } from '@mikro-orm/mongodb'; import { type AnyBoardDo, @@ -12,6 +12,8 @@ import { type BoardDoBuilder, BoardNodeType } from '../types'; // TODO Use an abstract base class for root nodes that have a contextId and a contextType. Multiple STI abstract base classes are blocked by MikroORM 6.1.2 (issue #3745) @Entity({ discriminatorValue: BoardNodeType.MEDIA_BOARD }) +@Index({ properties: ['_contextId'] }) +@Index({ properties: ['_contextType'] }) export class MediaBoardNode extends BoardNode { constructor(props: RootBoardNodeProps) { super(props); diff --git a/apps/server/src/shared/domain/entity/news.entity.ts b/apps/server/src/shared/domain/entity/news.entity.ts index 0fc1d12dc29..e50db3fc4f4 100644 --- a/apps/server/src/shared/domain/entity/news.entity.ts +++ b/apps/server/src/shared/domain/entity/news.entity.ts @@ -61,9 +61,11 @@ export abstract class News extends BaseEntityWithTimestamps { school!: SchoolEntity; @ManyToOne('User', { fieldName: 'creatorId', nullable: true }) + @Index() creator?: User; @ManyToOne('User', { fieldName: 'updaterId', nullable: true }) + @Index() updater?: User; permissions: string[] = []; diff --git a/apps/server/src/shared/domain/entity/submission.entity.ts b/apps/server/src/shared/domain/entity/submission.entity.ts index 66ad4c5e542..09b3473ed7c 100644 --- a/apps/server/src/shared/domain/entity/submission.entity.ts +++ b/apps/server/src/shared/domain/entity/submission.entity.ts @@ -23,7 +23,10 @@ export interface SubmissionProperties { @Entity({ tableName: 'submissions' }) @Index({ properties: ['student', 'teamMembers'] }) -@Unique({ properties: ['student', 'task'] }) +@Unique({ + properties: ['student', 'task'], + options: { partialFilterExpression: { studentId: { $exists: true } } }, +}) export class Submission extends BaseEntityWithTimestamps { @ManyToOne(() => SchoolEntity, { fieldName: 'schoolId' }) @Index() @@ -37,9 +40,11 @@ export class Submission extends BaseEntityWithTimestamps { student?: User; @ManyToOne('CourseGroup', { fieldName: 'courseGroupId', nullable: true }) + @Index() courseGroup?: CourseGroup; @ManyToMany('User', undefined, { fieldName: 'teamMembers' }) + @Index() teamMembers = new Collection(this); @Property({ nullable: true })