From 5c6754560183fc450281d48e64922ec219bb61a4 Mon Sep 17 00:00:00 2001 From: Mohammad AbuAboud Date: Tue, 13 Aug 2024 12:02:36 +0000 Subject: [PATCH] fix: db index on app event routing --- .../app-event-routing.entity.ts | 4 +- .../app-event-routing.service.ts | 53 +++++++++++-------- ...3549873495-ChangeEventRoutingConstraint.ts | 35 ++++++++++++ .../src/app/database/postgres-connection.ts | 2 + .../api/src/app/database/sqlite-connection.ts | 2 + 5 files changed, 71 insertions(+), 25 deletions(-) create mode 100644 packages/server/api/src/app/database/migration/common/1723549873495-ChangeEventRoutingConstraint.ts diff --git a/packages/server/api/src/app/app-event-routing/app-event-routing.entity.ts b/packages/server/api/src/app/app-event-routing/app-event-routing.entity.ts index 23e3de5e25..485b46811d 100644 --- a/packages/server/api/src/app/app-event-routing/app-event-routing.entity.ts +++ b/packages/server/api/src/app/app-event-routing/app-event-routing.entity.ts @@ -38,8 +38,8 @@ export const AppEventRoutingEntity = new EntitySchema({ unique: false, }, { - name: 'idx_app_event_project_id_appName_identifier_value_event', - columns: ['appName', 'projectId', 'identifierValue', 'event'], + name: 'idx_app_event_flow_id_project_id_appName_identifier_value_event', + columns: ['appName', 'projectId', 'flowId', 'identifierValue', 'event'], unique: true, }, ], diff --git a/packages/server/api/src/app/app-event-routing/app-event-routing.service.ts b/packages/server/api/src/app/app-event-routing/app-event-routing.service.ts index 026d7a80aa..ce2525069f 100644 --- a/packages/server/api/src/app/app-event-routing/app-event-routing.service.ts +++ b/packages/server/api/src/app/app-event-routing/app-event-routing.service.ts @@ -6,20 +6,14 @@ import { AppEventRoutingEntity, } from './app-event-routing.entity' -const appEventRoutingRepo = repoFactory( - AppEventRoutingEntity, -) +const appEventRoutingRepo = repoFactory(AppEventRoutingEntity) export const appEventRoutingService = { async listListeners({ appName, event, identifierValue, - }: { - appName: string - event: string - identifierValue: string - }): Promise { + }: ListParams): Promise { return appEventRoutingRepo().findBy({ appName, event, identifierValue }) }, async createListeners({ @@ -28,16 +22,14 @@ export const appEventRoutingService = { identifierValue, flowId, projectId, - }: { - appName: string - events: string[] - identifierValue: string - flowId: FlowId - projectId: ProjectId - }): Promise { - logger.info( - `Creating listeners for ${appName}, events=${events}, identifierValue=${identifierValue}`, - ) + }: CreateParams): Promise { + logger.info({ + appName, + events, + identifierValue, + flowId, + projectId, + }, '[AppEventRoutingService#createListeners] create') const upsertCommands: Promise[] = [] events.forEach((event) => { const upsert = appEventRoutingRepo().upsert( @@ -49,7 +41,7 @@ export const appEventRoutingService = { flowId, projectId, }, - ['appName', 'event', 'identifierValue', 'projectId'], + ['appName', 'event', 'identifierValue', 'projectId', 'flowId'], ) upsertCommands.push(upsert) }) @@ -58,13 +50,28 @@ export const appEventRoutingService = { async deleteListeners({ projectId, flowId, - }: { - projectId: ProjectId - flowId: FlowId - }): Promise { + }: DeleteParams): Promise { await appEventRoutingRepo().delete({ projectId, flowId, }) }, } + +type ListParams = { + appName: string + event: string + identifierValue: string +} +type DeleteParams = { + projectId: ProjectId + flowId: FlowId +} + +type CreateParams = { + appName: string + events: string[] + identifierValue: string + flowId: FlowId + projectId: ProjectId +} \ No newline at end of file diff --git a/packages/server/api/src/app/database/migration/common/1723549873495-ChangeEventRoutingConstraint.ts b/packages/server/api/src/app/database/migration/common/1723549873495-ChangeEventRoutingConstraint.ts new file mode 100644 index 0000000000..12e16f49e6 --- /dev/null +++ b/packages/server/api/src/app/database/migration/common/1723549873495-ChangeEventRoutingConstraint.ts @@ -0,0 +1,35 @@ +import { MigrationInterface, QueryRunner } from 'typeorm' + +export class ChangeEventRoutingConstraint1723549873495 implements MigrationInterface { + name = 'ChangeEventRoutingConstraint1723549873495' + + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.query(` + DROP INDEX "idx_app_event_project_id_appName_identifier_value_event" + `) + await queryRunner.query(` + CREATE UNIQUE INDEX "idx_app_event_flow_id_project_id_appName_identifier_value_event" ON "app_event_routing" ( + "appName", + "projectId", + "flowId", + "identifierValue", + "event" + ) + `) + } + + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.query(` + DROP INDEX "idx_app_event_flow_id_project_id_appName_identifier_value_event" + `) + await queryRunner.query(` + CREATE UNIQUE INDEX "idx_app_event_project_id_appName_identifier_value_event" ON "app_event_routing" ( + "appName", + "projectId", + "identifierValue", + "event" + ) + `) + } + +} diff --git a/packages/server/api/src/app/database/postgres-connection.ts b/packages/server/api/src/app/database/postgres-connection.ts index b9eb87c3ea..1b99f72297 100644 --- a/packages/server/api/src/app/database/postgres-connection.ts +++ b/packages/server/api/src/app/database/postgres-connection.ts @@ -31,6 +31,7 @@ import { AddTriggerTestStrategy1707087022764 } from './migration/common/17070870 import { MigrateWebhook1709581196563 } from './migration/common/1709581196563-migrate-webhook' import { RemoveShowActivityLog1716105958530 } from './migration/common/1716105958530-RemoveShowActivityLog' import { AddDurationForRuns1716725027424 } from './migration/common/1716725027424-AddDurationForRuns' +import { ChangeEventRoutingConstraint1723549873495 } from './migration/common/1723549873495-ChangeEventRoutingConstraint' import { AddAuthToPiecesMetadata1688922241747 } from './migration/postgres//1688922241747-AddAuthToPiecesMetadata' import { FlowAndFileProjectId1674788714498 } from './migration/postgres/1674788714498-FlowAndFileProjectId' import { initializeSchema1676238396411 } from './migration/postgres/1676238396411-initialize-schema' @@ -230,6 +231,7 @@ const getMigrations = (): (new () => MigrationInterface)[] => { AddUserInvitation1717960689650, AddPremiumPiecesColumnPostgres1717370717678, AddWorkerMachine1720101280025, + ChangeEventRoutingConstraint1723549873495, ] const edition = system.getEdition() diff --git a/packages/server/api/src/app/database/sqlite-connection.ts b/packages/server/api/src/app/database/sqlite-connection.ts index 67e57b51f0..8d29501c9e 100644 --- a/packages/server/api/src/app/database/sqlite-connection.ts +++ b/packages/server/api/src/app/database/sqlite-connection.ts @@ -12,6 +12,7 @@ import { AddTriggerTestStrategy1707087022764 } from './migration/common/17070870 import { MigrateWebhook1709581196563 } from './migration/common/1709581196563-migrate-webhook' import { RemoveShowActivityLog1716105958530 } from './migration/common/1716105958530-RemoveShowActivityLog' import { AddDurationForRuns1716725027424 } from './migration/common/1716725027424-AddDurationForRuns' +import { ChangeEventRoutingConstraint1723549873495 } from './migration/common/1723549873495-ChangeEventRoutingConstraint' import { InitialSql3Migration1690195839899 } from './migration/sqlite/1690195839899-InitialSql3Migration' import { AddAppConnectionTypeToTopLevel1691706020626 } from './migration/sqlite/1691706020626-add-app-connection-type-to-top-level' import { AddTagsToRunSqlite1692056190942 } from './migration/sqlite/1692056190942-AddTagsToRunSqlite' @@ -118,6 +119,7 @@ const getMigrations = (): (new () => MigrationInterface)[] => { AddUserInvitationSqlite1717943564437, AddPremiumPiecesColumnSqlite1717443603235, AddWorkerMachineSqlite1720100928449, + ChangeEventRoutingConstraint1723549873495, ] const edition = system.getEdition() if (edition !== ApEdition.COMMUNITY) {