The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
feat(workflow): require a core parent on every workflow version (2.43) (#26451)
Thomas Trompette13a59d53a323
diff --git a/packages/twenty-server/src/database/commands/upgrade-version-command/2-43/2-43-instance-command-slow-1790323148754-enforce-workflow-version-core-parent.ts b/packages/twenty-server/src/database/commands/upgrade-version-command/2-43/2-43-instance-command-slow-1790323148754-enforce-workflow-version-core-parent.tsnew file mode 100644index 00000000000..3a388cbd698--- /dev/null+++ b/packages/twenty-server/src/database/commands/upgrade-version-command/2-43/2-43-instance-command-slow-1790323148754-enforce-workflow-version-core-parent.ts@@ -0,0 +1,51 @@+import { DataSource, QueryRunner } from 'typeorm';++import { RegisteredInstanceCommand } from 'src/engine/core-modules/upgrade/decorators/registered-instance-command.decorator';+import { SlowInstanceCommand } from 'src/engine/core-modules/upgrade/interfaces/slow-instance-command.interface';+import { WorkspaceCacheService } from 'src/engine/workspace-cache/services/workspace-cache.service';++@RegisteredInstanceCommand('2.43.0', 1790323148754, { type: 'slow' })+export class EnforceWorkflowVersionCoreParentSlowInstanceCommand implements SlowInstanceCommand {+ constructor(private readonly workspaceCacheService: WorkspaceCacheService) {}++ public async runDataMigration(dataSource: DataSource): Promise<void> {+ await dataSource.query(+ `DELETE FROM "core"."workflowVersion" WHERE "coreWorkflowId" IS NULL`,+ );++ const workspaces = await dataSource.query<{ id: string }[]>(+ `SELECT "id" FROM "core"."workspace"`,+ );++ for (const workspace of workspaces) {+ await this.workspaceCacheService.flush(workspace.id, [+ 'flatWorkflowVersionMaps',+ 'workflowAutomatedTriggerMaps',+ ]);+ }+ }++ public async up(queryRunner: QueryRunner): Promise<void> {+ await queryRunner.query(+ `ALTER TABLE "core"."workflowVersion" ALTER COLUMN "coreWorkflowId" SET NOT NULL`,+ );+ await queryRunner.query(+ `DROP INDEX IF EXISTS "core"."IDX_WORKFLOW_VERSION_ONE_ACTIVE_PER_WORKFLOW"`,+ );+ await queryRunner.query(+ `CREATE UNIQUE INDEX "IDX_WORKFLOW_VERSION_ONE_ACTIVE_PER_WORKFLOW" ON "core"."workflowVersion" ("workspaceId", "coreWorkflowId") WHERE "status" = 'ACTIVE'`,+ );+ }++ public async down(queryRunner: QueryRunner): Promise<void> {+ await queryRunner.query(+ `DROP INDEX IF EXISTS "core"."IDX_WORKFLOW_VERSION_ONE_ACTIVE_PER_WORKFLOW"`,+ );+ await queryRunner.query(+ `CREATE UNIQUE INDEX "IDX_WORKFLOW_VERSION_ONE_ACTIVE_PER_WORKFLOW" ON "core"."workflowVersion" ("workspaceId", "workflowId") WHERE "status" = 'ACTIVE'`,+ );+ await queryRunner.query(+ `ALTER TABLE "core"."workflowVersion" ALTER COLUMN "coreWorkflowId" DROP NOT NULL`,+ );+ }+}diff --git a/packages/twenty-server/src/database/commands/upgrade-version-command/instance-commands.constant.ts b/packages/twenty-server/src/database/commands/upgrade-version-command/instance-commands.constant.tsindex 2f206d90799..cb1bcec7432 100644--- a/packages/twenty-server/src/database/commands/upgrade-version-command/instance-commands.constant.ts+++ b/packages/twenty-server/src/database/commands/upgrade-version-command/instance-commands.constant.ts@@ -209,6 +209,7 @@ import { BackfillSettingsMenuItemSlowInstanceCommand } from 'src/database/commanimport { AddChatMessageSenderFastInstanceCommand } from 'src/database/commands/upgrade-version-command/2-43/2-43-instance-command-fast-1790171503074-add-chat-message-sender';import { AddWorkspaceAllowedIframeOriginsFastInstanceCommand } from 'src/database/commands/upgrade-version-command/2-43/2-43-instance-command-fast-1790232481570-add-workspace-allowed-iframe-origins';import { AddUsageLimitInstanceOverrideFastInstanceCommand } from 'src/database/commands/upgrade-version-command/2-43/2-43-instance-command-fast-1790245573467-add-usage-limit-instance-override';+import { EnforceWorkflowVersionCoreParentSlowInstanceCommand } from 'src/database/commands/upgrade-version-command/2-43/2-43-instance-command-slow-1790323148754-enforce-workflow-version-core-parent';export const INSTANCE_COMMANDS = [AddViewFieldGroupIdIndexOnViewFieldFastInstanceCommand,@@ -420,4 +421,5 @@ export const INSTANCE_COMMANDS = [AddChatMessageSenderFastInstanceCommand,AddWorkspaceAllowedIframeOriginsFastInstanceCommand,AddUsageLimitInstanceOverrideFastInstanceCommand,+ EnforceWorkflowVersionCoreParentSlowInstanceCommand,];diff --git a/packages/twenty-server/src/engine/core-modules/workflow/entities/workflow-version.entity.ts b/packages/twenty-server/src/engine/core-modules/workflow/entities/workflow-version.entity.tsindex ffa364c25b9..7030fa0e2fd 100644--- a/packages/twenty-server/src/engine/core-modules/workflow/entities/workflow-version.entity.ts+++ b/packages/twenty-server/src/engine/core-modules/workflow/entities/workflow-version.entity.ts@@ -29,7 +29,7 @@ export enum WorkflowVersionStatus {@Index('IDX_WORKFLOW_VERSION_WORKSPACE_ID', ['workspaceId'])@Index('IDX_WORKFLOW_VERSION_ONE_ACTIVE_PER_WORKFLOW',- ['workspaceId', 'workflowId'],+ ['workspaceId', 'coreWorkflowId'],{unique: true,where: `"status" = 'ACTIVE'`,@@ -69,7 +69,7 @@ export class WorkflowVersionEntity extends SyncableEntity {upgradeCommandName:ADD_CORE_WORKFLOW_ID_TO_WORKFLOW_VERSION_UPGRADE_COMMAND_NAME,})- @Column({ type: 'uuid', nullable: true })+ @Column({ type: 'uuid', nullable: false })coreWorkflowId: string | null;@WasIntroducedInUpgrade({diff --git a/packages/twenty-server/src/engine/core-modules/workflow/services/workflow-version-core-sync.service.ts b/packages/twenty-server/src/engine/core-modules/workflow/services/workflow-version-core-sync.service.tsindex f36b49f42ac..a6a6aa1ca67 100644--- a/packages/twenty-server/src/engine/core-modules/workflow/services/workflow-version-core-sync.service.ts+++ b/packages/twenty-server/src/engine/core-modules/workflow/services/workflow-version-core-sync.service.ts@@ -490,12 +490,13 @@ export class WorkflowVersionCoreSyncService {const candidateCoreVersionId = workflowVersion.coreWorkflowVersionId;const candidateRows = isNonEmptyString(candidateCoreVersionId)? ((await transactionScope.executeRawQuery(- `SELECT "id", "workspaceId", "workflowId"${hasWorkspaceVersionMapping ? ', "workspaceWorkflowVersionId"' : ''} FROM core."workflowVersion" WHERE id = $1 FOR UPDATE`,+ `SELECT "id", "workspaceId", "workflowId", "coreWorkflowId"${hasWorkspaceVersionMapping ? ', "workspaceWorkflowVersionId"' : ''} FROM core."workflowVersion" WHERE id = $1 FOR UPDATE`,[candidateCoreVersionId],)) as {id: string;workspaceId: string;workflowId: string | null;+ coreWorkflowId: string | null;workspaceWorkflowVersionId?: string | null;}[]): [];@@ -515,9 +516,9 @@ export class WorkflowVersionCoreSyncService {const reverseRows = hasWorkspaceVersionMapping? ((await transactionScope.executeRawQuery(- `SELECT id FROM core."workflowVersion" WHERE "workspaceId" = $1 AND "workspaceWorkflowVersionId" = $2 FOR UPDATE`,+ `SELECT id, "coreWorkflowId" FROM core."workflowVersion" WHERE "workspaceId" = $1 AND "workspaceWorkflowVersionId" = $2 FOR UPDATE`,[workspaceId, workflowVersion.id],- )) as { id: string }[])+ )) as { id: string; coreWorkflowId: string | null }[]): [];if (@@ -531,16 +532,26 @@ export class WorkflowVersionCoreSyncService {);}+ const existingCoreRow = reverseRows[0] ?? candidate;+const coreWorkflowVersionId =- reverseRows[0]?.id ?? candidateCoreVersionId ?? uuidv4();+ existingCoreRow?.id ?? candidateCoreVersionId ?? uuidv4();const isNewLink =workflowVersion.coreWorkflowVersionId !== coreWorkflowVersionId;- const coreWorkflowId = await this.resolveCoreWorkflowIdInTransaction({- workspaceId,- workflowId: workflowVersion.workflowId,- transactionScope,- });+ const coreWorkflowId =+ (await this.resolveCoreWorkflowIdInTransaction({+ workspaceId,+ workflowId: workflowVersion.workflowId,+ transactionScope,+ })) ?? existingCoreRow?.coreWorkflowId;++ if (!isNonEmptyString(coreWorkflowId)) {+ throw new CoreWorkflowMetadataException(+ `Core workflow for workflow ${workflowVersion.workflowId} not found in workspace ${workspaceId}`,+ CoreWorkflowMetadataExceptionCode.WORKFLOW_VERSION_MISSING_WORKFLOW,+ );+ }// The conflict target is the primary key alone, so without the workspaceId// predicate a core row owned by another workspace would have its triggers@@ -554,7 +565,7 @@ export class WorkflowVersionCoreSyncService {"triggers" = EXCLUDED."triggers","steps" = EXCLUDED."steps","status" = EXCLUDED."status",- "coreWorkflowId" = COALESCE(EXCLUDED."coreWorkflowId", core."workflowVersion"."coreWorkflowId")+ "coreWorkflowId" = EXCLUDED."coreWorkflowId"WHERE core."workflowVersion"."workspaceId" = EXCLUDED."workspaceId" RETURNING id`,[coreWorkflowVersionId,diff --git a/packages/twenty-server/src/modules/workflow/workflow-builder/workflow-version/workflow-version.module.ts b/packages/twenty-server/src/modules/workflow/workflow-builder/workflow-version/workflow-version.module.tsindex 2aaa6c7e69c..6d537553b81 100644--- a/packages/twenty-server/src/modules/workflow/workflow-builder/workflow-version/workflow-version.module.ts+++ b/packages/twenty-server/src/modules/workflow/workflow-builder/workflow-version/workflow-version.module.ts@@ -2,6 +2,7 @@ import { Module } from '@nestjs/common';import { CacheLockModule } from 'src/engine/core-modules/cache-lock/cache-lock.module';import { RecordPositionModule } from 'src/engine/core-modules/record-position/record-position.module';+import { WorkflowCoreModule } from 'src/engine/core-modules/workflow/workflow-core.module';import { WorkflowVersionCoreModule } from 'src/engine/core-modules/workflow/workflow-version-core.module';import { LogicFunctionModule } from 'src/engine/metadata-modules/logic-function/logic-function.module';import { WorkflowCommonModule } from 'src/modules/workflow/common/workflow-common.module';@@ -17,6 +18,7 @@ import { WorkflowVersionWorkspaceService } from 'src/modules/workflow/workflow-bWorkflowCommonModule,RecordPositionModule,CacheLockModule,+ WorkflowCoreModule,WorkflowVersionCoreModule,],providers: [WorkflowVersionWorkspaceService],diff --git a/packages/twenty-server/src/modules/workflow/workflow-builder/workflow-version/workflow-version.workspace-service.ts b/packages/twenty-server/src/modules/workflow/workflow-builder/workflow-version/workflow-version.workspace-service.tsindex 33b7084398b..e7473af037c 100644--- a/packages/twenty-server/src/modules/workflow/workflow-builder/workflow-version/workflow-version.workspace-service.ts+++ b/packages/twenty-server/src/modules/workflow/workflow-builder/workflow-version/workflow-version.workspace-service.ts@@ -6,6 +6,7 @@ import { TRIGGER_STEP_ID } from 'twenty-shared/workflow';import { WithLock } from 'src/engine/core-modules/cache-lock/with-lock.decorator';import { RecordPositionService } from 'src/engine/core-modules/record-position/services/record-position.service';import { type WorkflowStepPositionUpdateInput } from 'src/engine/core-modules/workflow/dtos/update-workflow-step-position-update.input';+import { WorkflowCoreSyncService } from 'src/engine/core-modules/workflow/services/workflow-core-sync.service';import { WorkflowVersionCoreSyncService } from 'src/engine/core-modules/workflow/services/workflow-version-core-sync.service';import { WorkspaceOrmManager } from 'src/engine/twenty-orm/workspace-orm.manager';import { buildSystemAuthContext } from 'src/engine/twenty-orm/utils/build-system-auth-context.util';@@ -40,6 +41,7 @@ export class WorkflowVersionWorkspaceService {private readonly recordPositionService: RecordPositionService,private readonly workflowCommonWorkspaceService: WorkflowCommonWorkspaceService,private readonly workflowVersionCoreSyncService: WorkflowVersionCoreSyncService,+ private readonly workflowCoreSyncService: WorkflowCoreSyncService,) {}@WithLock('workflowId')@@ -248,6 +250,10 @@ export class WorkflowVersionWorkspaceService {insertWorkflowResult.generatedMaps[0] as WorkflowWorkspaceEntity).id;+ await this.workflowCoreSyncService.upsertToCore(workspaceId, [+ newWorkflowId,+ ]);+const versionPosition =await this.recordPositionService.buildRecordPosition({value: 'first',diff --git a/packages/twenty-server/test/integration/graphql/suites/workflow/core-workflow-execution.integration-spec.ts b/packages/twenty-server/test/integration/graphql/suites/workflow/core-workflow-execution.integration-spec.tsindex 6812fdfbda2..7004d3cd769 100644--- a/packages/twenty-server/test/integration/graphql/suites/workflow/core-workflow-execution.integration-spec.ts+++ b/packages/twenty-server/test/integration/graphql/suites/workflow/core-workflow-execution.integration-spec.ts@@ -1700,7 +1700,7 @@ describe('core workflow execution and queue compatibility (e2e)', () => {const latestVersionId = randomUUID();await global.testDataSource.query(`INSERT INTO core."workflowVersion" (id, "workspaceId", "applicationId", "universalIdentifier", "coreWorkflowId", "workflowId", status, triggers, steps)- SELECT $2, "workspaceId", "applicationId", $2, "coreWorkflowId", NULL, 'ACTIVE', triggers, '[]'::jsonb FROM core."workflowVersion" WHERE id = $1`,+ SELECT $2, "workspaceId", "applicationId", $2, "coreWorkflowId", NULL, 'DEACTIVATED', triggers, '[]'::jsonb FROM core."workflowVersion" WHERE id = $1`,[fixture.coreWorkflowVersionId, latestVersionId],);await global.testDataSource.query(diff --git a/packages/twenty-server/test/integration/upgrade/suites/2-42-workspace-command-1789645879295-relink-workflow-versions-to-core-workflows.integration-spec.ts b/packages/twenty-server/test/integration/upgrade/suites/2-42-workspace-command-1789645879295-relink-workflow-versions-to-core-workflows.integration-spec.tsindex 8891addf18b..f07263f8262 100644--- a/packages/twenty-server/test/integration/upgrade/suites/2-42-workspace-command-1789645879295-relink-workflow-versions-to-core-workflows.integration-spec.ts+++ b/packages/twenty-server/test/integration/upgrade/suites/2-42-workspace-command-1789645879295-relink-workflow-versions-to-core-workflows.integration-spec.ts@@ -11,6 +11,7 @@ describe('RelinkWorkflowVersionsToCoreWorkflowsCommand (integration)', () => {let dataSource: DataSource;let command: RelinkWorkflowVersionsToCoreWorkflowsCommand;let applicationId: string;+ let isCoreWorkflowIdRequired: boolean;const seededCoreWorkflowIds: string[] = [];const seededCoreVersionIds: string[] = [];@@ -106,6 +107,19 @@ describe('RelinkWorkflowVersionsToCoreWorkflowsCommand (integration)', () => {);applicationId = workspace.workspaceCustomApplicationId;++ const [coreWorkflowIdColumn] = await dataSource.query(+ `SELECT "is_nullable" FROM information_schema.columns+ WHERE table_schema = 'core' AND table_name = 'workflowVersion' AND column_name = 'coreWorkflowId'`,+ );++ isCoreWorkflowIdRequired = coreWorkflowIdColumn.is_nullable === 'NO';++ if (isCoreWorkflowIdRequired) {+ await dataSource.query(+ `ALTER TABLE core."workflowVersion" ALTER COLUMN "coreWorkflowId" DROP NOT NULL`,+ );+ }});afterAll(async () => {@@ -123,6 +137,12 @@ describe('RelinkWorkflowVersionsToCoreWorkflowsCommand (integration)', () => {);}+ if (isCoreWorkflowIdRequired) {+ await dataSource.query(+ `ALTER TABLE core."workflowVersion" ALTER COLUMN "coreWorkflowId" SET NOT NULL`,+ );+ }+await dataSource.destroy();});