File tree Expand file tree Collapse file tree 1 file changed +15
-1
lines changed
packages/@n8n/db/src/migrations/common Expand file tree Collapse file tree 1 file changed +15
-1
lines changed Original file line number Diff line number Diff 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
You can’t perform that action at this time.
0 commit comments