Skip to content

Commit bbc3af0

Browse files
committed
Fix migration to only use existing version ids
1 parent ee7ddff commit bbc3af0

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

packages/@n8n/db/src/migrations/common/1761302979000-AddActiveVersionIdColumn.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,22 @@ export class AddActiveVersionIdColumn1761302979000 implements ReversibleMigratio
2222
);
2323

2424
// For existing ACTIVE workflows, set activeVersionId = versionId
25+
// Only update workflows where the versionId exists in workflow_history to avoid
26+
// foreign key constraint violations. Some users don't have workflow history enabled,
27+
// and their workflows need to have activeVersionId = NULL until we enable history for them.
28+
const workflowHistoryTableName = escape.tableName(WORKFLOW_HISTORY_TABLE_NAME);
29+
const versionIdColumn = escape.columnName('versionId');
30+
const activeColumn = escape.columnName('active');
31+
const activeVersionIdColumn = escape.columnName('activeVersionId');
32+
2533
await queryRunner.query(
26-
`UPDATE ${workflowsTableName} SET activeVersionId = versionId WHERE active = true`,
34+
`UPDATE ${workflowsTableName}
35+
SET ${activeVersionIdColumn} = ${versionIdColumn}
36+
WHERE ${activeColumn} = true
37+
AND EXISTS (
38+
SELECT 1 FROM ${workflowHistoryTableName}
39+
WHERE ${workflowHistoryTableName}.${versionIdColumn} = ${workflowsTableName}.${versionIdColumn}
40+
)`,
2741
);
2842
}
2943

0 commit comments

Comments
 (0)