Skip to content

The open alternative to Salesforce, designed for AI.

fix(workflow): repair core version parents and skip runs of deleted versions in the 2.42 execution backfill (#26661)

Thomas Trompette5f7743e108a7
diff --git a/packages/twenty-server/src/database/commands/upgrade-version-command/2-42/2-42-workspace-command-1789719131001-backfill-workflow-execution-core-ids.command.ts b/packages/twenty-server/src/database/commands/upgrade-version-command/2-42/2-42-workspace-command-1789719131001-backfill-workflow-execution-core-ids.command.ts
index d1012e0c886..4ab638c0f3a 100644
--- a/packages/twenty-server/src/database/commands/upgrade-version-command/2-42/2-42-workspace-command-1789719131001-backfill-workflow-execution-core-ids.command.ts
+++ b/packages/twenty-server/src/database/commands/upgrade-version-command/2-42/2-42-workspace-command-1789719131001-backfill-workflow-execution-core-ids.command.ts
@@ -79,6 +79,42 @@ export class BackfillWorkflowExecutionCoreIdsCommand extends ProvisionedWorkspac
);
}
+ const [, relinkedCoreVersionCount] = await queryRunner.query(
+ `UPDATE core."workflowVersion" cv
+ SET "coreWorkflowId" = cw.id
+ FROM "${schema}"."workflow" w
+ JOIN core."workflow" cw
+ ON cw.id = w."coreWorkflowId" AND cw."workspaceId" = $1 AND cw."workspaceWorkflowId" = w.id
+ WHERE cv."workspaceId" = $1 AND cv."coreWorkflowId" IS NULL AND cv."workflowId" = w.id
+ AND w."deletedAt" IS NULL`,
+ [workspaceId],
+ );
+
+ if (relinkedCoreVersionCount > 0) {
+ this.logger.log(
+ `${options.dryRun ? '[DRY RUN] Would relink' : 'Relinked'} ${relinkedCoreVersionCount} core workflow version(s) to their core workflow in workspace ${workspaceId}`,
+ );
+ }
+
+ const [, deletedCoreVersionCount] = await queryRunner.query(
+ `DELETE FROM core."workflowVersion" cv
+ WHERE cv."workspaceId" = $1
+ AND NOT EXISTS (
+ SELECT 1 FROM core."workflow" cw WHERE cw.id = cv."coreWorkflowId"
+ )
+ AND NOT EXISTS (
+ SELECT 1 FROM "${schema}"."workflowVersion" wv
+ WHERE wv."coreWorkflowVersionId" = cv.id AND wv."deletedAt" IS NULL
+ )`,
+ [workspaceId],
+ );
+
+ if (deletedCoreVersionCount > 0) {
+ this.logger.log(
+ `${options.dryRun ? '[DRY RUN] Would delete' : 'Deleted'} ${deletedCoreVersionCount} core workflow version(s) without a core workflow in workspace ${workspaceId}`,
+ );
+ }
+
const [, createdCoreVersionCount] = await queryRunner.query(
`WITH "createdCoreVersions" AS (
INSERT INTO core."workflowVersion"
@@ -209,6 +245,10 @@ export class BackfillWorkflowExecutionCoreIdsCommand extends ProvisionedWorkspac
LEFT JOIN core."workflow" cw ON cw.id = r."coreWorkflowId" AND cw."workspaceId" = $1
WHERE r."deletedAt" IS NULL AND r.status IN ('NOT_STARTED', 'ENQUEUED', 'RUNNING')
AND (cv.id IS NULL OR cw.id IS NULL OR cv."coreWorkflowId" <> cw.id)
+ AND EXISTS (
+ SELECT 1 FROM "${schema}"."workflowVersion" wv
+ WHERE wv.id = r."workflowVersionId" AND wv."deletedAt" IS NULL
+ )
LIMIT 10`,
[workspaceId],
);
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.ts
index 7004d3cd769..743043858a0 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
@@ -1652,16 +1652,14 @@ describe('core workflow execution and queue compatibility (e2e)', () => {
);
});
- it('rejects an unmapped pending run without partially updating its ids', async () => {
+ it('leaves a pending run without a workspace version untouched', async () => {
const id = randomUUID();
try {
await global.testDataSource.query(
`INSERT INTO "${schema}"."workflowRun" (id, name, status, position, state) VALUES ($1, 'B-Async unmapped', 'RUNNING', 0, '{}')`,
[id],
);
- await expect(backfill()).rejects.toThrow(
- 'Pending workflow runs have no valid core mapping',
- );
+ await backfill();
expect((await getRun(id)).coreWorkflowId).toBeNull();
} finally {
await global.testDataSource.query(