From 4ce78619ee19291b82abeb58f3e4064e0a261ddf Mon Sep 17 00:00:00 2001 From: Charlie Kolb Date: Mon, 17 Nov 2025 15:33:38 +0100 Subject: [PATCH] Add migration --- ...43735-CreateWorkflowPublishHistoryTable.ts | 44 +++++++++++++++++++ packages/@n8n/db/src/migrations/dsl/table.ts | 5 +++ .../db/src/migrations/postgresdb/index.ts | 2 + .../@n8n/db/src/migrations/sqlite/index.ts | 2 + 4 files changed, 53 insertions(+) create mode 100644 packages/@n8n/db/src/migrations/common/1763387043735-CreateWorkflowPublishHistoryTable.ts diff --git a/packages/@n8n/db/src/migrations/common/1763387043735-CreateWorkflowPublishHistoryTable.ts b/packages/@n8n/db/src/migrations/common/1763387043735-CreateWorkflowPublishHistoryTable.ts new file mode 100644 index 00000000000..41951362d20 --- /dev/null +++ b/packages/@n8n/db/src/migrations/common/1763387043735-CreateWorkflowPublishHistoryTable.ts @@ -0,0 +1,44 @@ +import type { MigrationContext, ReversibleMigration } from '../migration-types'; + +const workflowPublishHistoryTableName = 'workflow_publish_history'; + +export class CreateWorkflowPublishHistoryTable1763387043735 implements ReversibleMigration { + async up({ schemaBuilder: { createTable, column }, runQuery }: MigrationContext) { + await createTable(workflowPublishHistoryTableName) + .withColumns( + column('workflowId').varchar(36).notNull, + column('versionId').varchar(36).notNull, + column('status').varchar(36).notNull, + ) + .withUpdatedAt.withForeignKey('workflowId', { + tableName: 'workflow_entity', + columnName: 'id', + onDelete: 'CASCADE', + }) + .withForeignKey('versionId', { + tableName: 'workflow_history', + columnName: 'versionId', + onDelete: 'CASCADE', + }) + .withUniqueConstraintOn(['workflowId', 'versionId']) + .withIndexOn(['workflowId', 'versionId']); + + const activeWorkflows = await runQuery>( + 'SELECT we.id, we.versionId FROM workflow_entity we WHERE we.active;', + ); + console.log(activeWorkflows); + + if (activeWorkflows.length > 0) { + const values = activeWorkflows + .map(({ id, versionId }) => `('${id}', '${versionId}', 'activated')`) + .join(','); + await runQuery( + `INSERT INTO ${workflowPublishHistoryTableName} (workflowId, versionId, status) VALUES ${values};`, + ); + } + } + + async down({ schemaBuilder: { dropTable } }: MigrationContext) { + await dropTable(workflowPublishHistoryTableName); + } +} diff --git a/packages/@n8n/db/src/migrations/dsl/table.ts b/packages/@n8n/db/src/migrations/dsl/table.ts index 06d9f790dfe..d4d4b71ae54 100644 --- a/packages/@n8n/db/src/migrations/dsl/table.ts +++ b/packages/@n8n/db/src/migrations/dsl/table.ts @@ -46,6 +46,11 @@ export class CreateTable extends TableOperation { return this; } + get withUpdatedAt() { + this.columns.push(new Column('updatedAt').timestampTimezone().notNull.default('NOW()')); + return this; + } + withIndexOn(columnName: string | string[], isUnique = false) { const columnNames = Array.isArray(columnName) ? columnName : [columnName]; this.indices.add({ columnNames, isUnique }); diff --git a/packages/@n8n/db/src/migrations/postgresdb/index.ts b/packages/@n8n/db/src/migrations/postgresdb/index.ts index 6e76c106dc7..082f9f00edb 100644 --- a/packages/@n8n/db/src/migrations/postgresdb/index.ts +++ b/packages/@n8n/db/src/migrations/postgresdb/index.ts @@ -113,6 +113,7 @@ import { AddToolsColumnToChatHubTables1761830340990 } from '../common/1761830340 import { AddWorkflowDescriptionColumn1762177736257 } from '../common/1762177736257-AddWorkflowDescriptionColumn'; import { BackfillMissingWorkflowHistoryRecords1762763704614 } from '../common/1762763704614-BackfillMissingWorkflowHistoryRecords'; import { AddWorkflowHistoryAutoSaveFields1762847206508 } from '../common/1762847206508-AddWorkflowHistoryAutoSaveFields'; +import { CreateWorkflowPublishHistoryTable1763387043735 } from '../common/1763387043735-CreateWorkflowPublishHistoryTable'; import type { Migration } from '../migration-types'; export const postgresMigrations: Migration[] = [ @@ -231,4 +232,5 @@ export const postgresMigrations: Migration[] = [ ChangeDefaultForIdInUserTable1762771264000, AddWorkflowHistoryAutoSaveFields1762847206508, AddToolsColumnToChatHubTables1761830340990, + CreateWorkflowPublishHistoryTable1763387043735, ]; diff --git a/packages/@n8n/db/src/migrations/sqlite/index.ts b/packages/@n8n/db/src/migrations/sqlite/index.ts index 5db7f08d5e7..778e3b832c6 100644 --- a/packages/@n8n/db/src/migrations/sqlite/index.ts +++ b/packages/@n8n/db/src/migrations/sqlite/index.ts @@ -109,6 +109,7 @@ import { AddToolsColumnToChatHubTables1761830340990 } from '../common/1761830340 import { AddWorkflowDescriptionColumn1762177736257 } from '../common/1762177736257-AddWorkflowDescriptionColumn'; import { BackfillMissingWorkflowHistoryRecords1762763704614 } from '../common/1762763704614-BackfillMissingWorkflowHistoryRecords'; import { AddWorkflowHistoryAutoSaveFields1762847206508 } from '../common/1762847206508-AddWorkflowHistoryAutoSaveFields'; +import { CreateWorkflowPublishHistoryTable1763387043735 } from '../common/1763387043735-CreateWorkflowPublishHistoryTable'; import type { Migration } from '../migration-types'; const sqliteMigrations: Migration[] = [ @@ -223,6 +224,7 @@ const sqliteMigrations: Migration[] = [ BackfillMissingWorkflowHistoryRecords1762763704614, AddWorkflowHistoryAutoSaveFields1762847206508, AddToolsColumnToChatHubTables1761830340990, + CreateWorkflowPublishHistoryTable1763387043735, ]; export { sqliteMigrations };