77import { Logger } from '@n8n/backend-common' ;
88import { WorkflowsConfig } from '@n8n/config' ;
99import type { WorkflowEntity , IWorkflowDb } from '@n8n/db' ;
10- import { WorkflowRepository } from '@n8n/db' ;
10+ import { WorkflowRepository , WorkflowPublishHistoryRepository } from '@n8n/db' ;
1111import { OnLeaderStepdown , OnLeaderTakeover , OnPubSubEvent , OnShutdown } from '@n8n/decorators' ;
1212import { Service } from '@n8n/di' ;
1313import chunk from 'lodash/chunk' ;
@@ -59,6 +59,8 @@ import * as WorkflowExecuteAdditionalData from '@/workflow-execute-additional-da
5959import { WorkflowExecutionService } from '@/workflows/workflow-execution.service' ;
6060import { WorkflowStaticDataService } from '@/workflows/workflow-static-data.service' ;
6161import { formatWorkflow } from '@/workflows/workflow.formatter' ;
62+ import { PubSubCommandMap } from './scaling/pubsub/pubsub.event-map' ;
63+ import { User } from '@n8n/api-types' ;
6264
6365interface QueuedActivation {
6466 activationMode : WorkflowActivateMode ;
@@ -80,6 +82,7 @@ export class ActiveWorkflowManager {
8082 private readonly nodeTypes : NodeTypes ,
8183 private readonly webhookService : WebhookService ,
8284 private readonly workflowRepository : WorkflowRepository ,
85+ private readonly workflowPublishHistoryRepository : WorkflowPublishHistoryRepository ,
8386 private readonly activationErrorsService : ActivationErrorsService ,
8487 private readonly executionService : ExecutionService ,
8588 private readonly workflowStaticDataService : WorkflowStaticDataService ,
@@ -561,6 +564,7 @@ export class ActiveWorkflowManager {
561564 activationMode : WorkflowActivateMode ,
562565 existingWorkflow ?: WorkflowEntity ,
563566 { shouldPublish } = { shouldPublish : true } ,
567+ userId : string | null = null ,
564568 ) {
565569 const added = { webhooks : false , triggersAndPollers : false } ;
566570
@@ -655,6 +659,13 @@ export class ActiveWorkflowManager {
655659
656660 const triggerCount = this . countTriggers ( workflow , additionalData ) ;
657661 await this . workflowRepository . updateWorkflowTriggerCount ( workflow . id , triggerCount ) ;
662+ await this . workflowPublishHistoryRepository . addRecord ( {
663+ workflowId,
664+ versionId : dbWorkflow . versionId ,
665+ status : 'activated' ,
666+ mode : activationMode ,
667+ userId,
668+ } ) ;
658669 } catch ( e ) {
659670 const error = e instanceof Error ? e : new Error ( `${ e } ` ) ;
660671 await this . activationErrorsService . register ( workflowId , error . message ) ;
@@ -670,7 +681,7 @@ export class ActiveWorkflowManager {
670681 }
671682
672683 @OnPubSubEvent ( 'display-workflow-activation' , { instanceType : 'main' } )
673- handleDisplayWorkflowActivation ( { workflowId } : { workflowId : string } ) {
684+ handleDisplayWorkflowActivation ( { workflowId } : PubSubCommandMap [ 'display-workflow-activation' ] ) {
674685 this . push . broadcast ( { type : 'workflowActivated' , data : { workflowId } } ) ;
675686 }
676687
@@ -697,7 +708,9 @@ export class ActiveWorkflowManager {
697708 instanceType : 'main' ,
698709 instanceRole : 'leader' ,
699710 } )
700- async handleAddWebhooksTriggersAndPollers ( { workflowId } : { workflowId : string } ) {
711+ async handleAddWebhooksTriggersAndPollers ( {
712+ workflowId,
713+ } : PubSubCommandMap [ 'add-webhooks-triggers-and-pollers' ] ) {
701714 try {
702715 await this . add ( workflowId , 'activate' , undefined , {
703716 shouldPublish : false , // prevent leader from re-publishing message
@@ -881,7 +894,7 @@ export class ActiveWorkflowManager {
881894 */
882895 // TODO: this should happen in a transaction
883896 // maybe, see: https://github.com/n8n-io/n8n/pull/8904#discussion_r1530150510
884- async remove ( workflowId : WorkflowId ) {
897+ async remove ( workflowId : WorkflowId , userId ?: User [ 'id' ] , reason ?: 'update' | 'deactivate' ) {
885898 if ( this . instanceSettings . isMultiMain ) {
886899 try {
887900 await this . clearWebhooks ( workflowId ) ;
@@ -918,6 +931,14 @@ export class ActiveWorkflowManager {
918931 // if it's active in memory then it's a trigger
919932 // so remove from list of actives workflows
920933 await this . removeWorkflowTriggersAndPollers ( workflowId ) ;
934+
935+ await this . workflowPublishHistoryRepository . addRecord ( {
936+ workflowId,
937+ versionId : null ,
938+ status : 'deactivated' ,
939+ mode : reason ?? null ,
940+ userId : userId ?? null ,
941+ } ) ;
921942 }
922943
923944 @OnPubSubEvent ( 'remove-triggers-and-pollers' , { instanceType : 'main' , instanceRole : 'leader' } )
0 commit comments