From b5154d9be5e3adccbdcbb0ad748ed869ba75d4cc Mon Sep 17 00:00:00 2001 From: Omar Ajoue Date: Thu, 2 Feb 2023 17:01:45 +0100 Subject: [PATCH] fix: Prevent unnecessarily touching updatedAt when n8n starts (#5340) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Include MariaDB in exception list Co-authored-by: कारतोफ्फेलस्क्रिप्ट™ --- packages/cli/src/ActiveWorkflowRunner.ts | 3 ++- .../cli/src/workflows/workflows.services.ts | 19 ++++++++++++++++++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/packages/cli/src/ActiveWorkflowRunner.ts b/packages/cli/src/ActiveWorkflowRunner.ts index 4332fe1ad..14961f2c5 100644 --- a/packages/cli/src/ActiveWorkflowRunner.ts +++ b/packages/cli/src/ActiveWorkflowRunner.ts @@ -63,6 +63,7 @@ import { NodeTypes } from '@/NodeTypes'; import { WorkflowRunner } from '@/WorkflowRunner'; import { ExternalHooks } from '@/ExternalHooks'; import { whereClause } from './UserManagement/UserManagementHelper'; +import { WorkflowsService } from './workflows/workflows.services'; const WEBHOOK_PROD_UNREGISTERED_HINT = "The workflow must be active for a production URL to run successfully. You can activate the workflow using the toggle in the top-right of the editor. Note that unlike test URL calls, production URL calls aren't shown on the canvas (only in the executions list)"; @@ -867,7 +868,7 @@ export class ActiveWorkflowRunner { workflowInstance.getPollNodes().length + WebhookHelpers.getWorkflowWebhooks(workflowInstance, additionalData, undefined, true) .length; - await Db.collections.Workflow.update(workflowInstance.id, { triggerCount }); + await WorkflowsService.updateWorkflowTriggerCount(workflowInstance.id, triggerCount); } } catch (error) { // There was a problem activating the workflow diff --git a/packages/cli/src/workflows/workflows.services.ts b/packages/cli/src/workflows/workflows.services.ts index 902896ac1..3b5fd2d92 100644 --- a/packages/cli/src/workflows/workflows.services.ts +++ b/packages/cli/src/workflows/workflows.services.ts @@ -1,7 +1,7 @@ import { validate as jsonSchemaValidate } from 'jsonschema'; import type { INode, IPinData, JsonObject } from 'n8n-workflow'; import { NodeApiError, jsonParse, LoggerProxy, Workflow } from 'n8n-workflow'; -import type { FindOptionsWhere } from 'typeorm'; +import type { FindOptionsWhere, UpdateResult } from 'typeorm'; import { In } from 'typeorm'; import pick from 'lodash.pick'; import { v4 as uuid } from 'uuid'; @@ -459,4 +459,21 @@ export class WorkflowsService { return sharedWorkflow.workflow; } + + static async updateWorkflowTriggerCount(id: string, triggerCount: number): Promise { + const qb = Db.collections.Workflow.createQueryBuilder('workflow'); + return qb + .update() + .set({ + triggerCount, + updatedAt: () => { + if (['mysqldb', 'mariadb'].includes(config.getEnv('database.type'))) { + return 'updatedAt'; + } + return '"updatedAt"'; + }, + }) + .where('id = :id', { id }) + .execute(); + } }