From 1c987540aaab4a259042a039719432b4bd0dda2a Mon Sep 17 00:00:00 2001 From: Oli Juhl <59018053+olivermrbl@users.noreply.github.com> Date: Thu, 19 Jun 2025 20:19:01 +0200 Subject: [PATCH] fix(workflow-engine-redis): Ensure PK is set without errors (#12775) * fix(workflow-engine-redis): Align naming of migration * change approach --- .../src/migrations/Migration20250505092459.ts | 18 ++++++++++++++++++ .../src/migrations/Migration20250505101505.ts | 18 ++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/packages/modules/workflow-engine-inmemory/src/migrations/Migration20250505092459.ts b/packages/modules/workflow-engine-inmemory/src/migrations/Migration20250505092459.ts index 84f58aecd2..937dcf6043 100644 --- a/packages/modules/workflow-engine-inmemory/src/migrations/Migration20250505092459.ts +++ b/packages/modules/workflow-engine-inmemory/src/migrations/Migration20250505092459.ts @@ -19,6 +19,24 @@ export class Migration20250505092459 extends Migration { this.addSql( `CREATE UNIQUE INDEX IF NOT EXISTS "IDX_workflow_execution_workflow_id_transaction_id_run_id_unique" ON "workflow_execution" (workflow_id, transaction_id, run_id) WHERE deleted_at IS NULL;` ) + /* + * We mistakenly named this migration differently in the workflow engines; this has caused issues with the migrations. Switching between engines will fail because the primary key is attempted to be set twice. + * The issue happens in the following scenario: + * 1. In memory engine is used + * 2. Migration is run + * 3. Primary is key added + * 3. Redis engine is used + * 4. Migration is run + * 5. Same primary key is attempted to be set again + * 6. Migration fails + * + * The same scenario can happen if you go from Redis to In memory. + * + * With this fix, we ensure the primary key is only ever set once. + */ + this.addSql( + `alter table if exists "workflow_execution" drop constraint if exists "workflow_execution_pkey";` + ) this.addSql( `alter table if exists "workflow_execution" add constraint "workflow_execution_pkey" primary key ("workflow_id", "transaction_id", "run_id");` ) diff --git a/packages/modules/workflow-engine-redis/src/migrations/Migration20250505101505.ts b/packages/modules/workflow-engine-redis/src/migrations/Migration20250505101505.ts index 60680b26aa..9756181fdb 100644 --- a/packages/modules/workflow-engine-redis/src/migrations/Migration20250505101505.ts +++ b/packages/modules/workflow-engine-redis/src/migrations/Migration20250505101505.ts @@ -19,6 +19,24 @@ export class Migration20250505101505 extends Migration { this.addSql( `CREATE UNIQUE INDEX IF NOT EXISTS "IDX_workflow_execution_workflow_id_transaction_id_run_id_unique" ON "workflow_execution" (workflow_id, transaction_id, run_id) WHERE deleted_at IS NULL;` ) + /* + * We mistakenly named this migration differently in the workflow engines; this has caused issues with the migrations. Switching between engines will fail because the primary key is attempted to be set twice. + * The issue happens in the following scenario: + * 1. In memory engine is used + * 2. Migration is run + * 3. Primary is key added + * 3. Redis engine is used + * 4. Migration is run + * 5. Same primary key is attempted to be set again + * 6. Migration fails + * + * The same scenario can happen if you go from Redis to In memory. + * + * With this fix, we ensure the primary key is only ever set once. + */ + this.addSql( + `alter table if exists "workflow_execution" drop constraint if exists "workflow_execution_pkey";` + ) this.addSql( `alter table if exists "workflow_execution" add constraint "workflow_execution_pkey" primary key ("workflow_id", "transaction_id", "run_id");` )