Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): Fix update workflow cli command being unable to activate all workflows #8412

Merged
merged 5 commits into from
Jan 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions packages/cli/src/commands/update/workflow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,22 @@ export class UpdateWorkflowCommand extends BaseCommand {
}

const newState = flags.active === 'true';
const action = newState ? 'Activating' : 'Deactivating';

if (flags.id) {
this.logger.info(`Deactivating workflow with ID: ${flags.id}`);
this.logger.info(`${action} workflow with ID: ${flags.id}`);
await Container.get(WorkflowRepository).updateActiveState(flags.id, newState);
} else {
this.logger.info('Deactivating all workflows');
await Container.get(WorkflowRepository).deactivateAll();
this.logger.info(`${action} all workflows`);
if (newState) {
await Container.get(WorkflowRepository).activateAll();
} else {
await Container.get(WorkflowRepository).deactivateAll();
}
}

this.logger.info('Done');
this.logger.info('Activation or deactivation will not take effect if n8n is running.');
this.logger.info('Please restart n8n for changes to take effect if n8n is currently running.');
}

async catch(error: Error) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,10 @@ export class WorkflowRepository extends Repository<WorkflowEntity> {
return await this.update({ active: true }, { active: false });
}

async activateAll() {
return await this.update({ active: false }, { active: true });
}

async findByActiveState(activeState: boolean) {
return await this.findBy({ active: activeState });
}
Expand Down
126 changes: 126 additions & 0 deletions packages/cli/test/integration/commands/update/workflow.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
import { Config } from '@oclif/core';
import { InternalHooks } from '@/InternalHooks';
import { LoadNodesAndCredentials } from '@/LoadNodesAndCredentials';
import { UpdateWorkflowCommand } from '@/commands/update/workflow';

import * as testDb from '../../shared/testDb';
import { createWorkflowWithTrigger, getAllWorkflows } from '../../shared/db/workflows';
import { mockInstance } from '../../../shared/mocking';

beforeAll(async () => {
mockInstance(InternalHooks);
mockInstance(LoadNodesAndCredentials);
await testDb.init();
});

beforeEach(async () => {
await testDb.truncate(['Workflow']);
});

afterAll(async () => {
await testDb.terminate();
});

test('update:workflow can activate all workflows', async () => {
//
// ARRANGE
//
const workflows = await Promise.all([
createWorkflowWithTrigger({}),
createWorkflowWithTrigger({}),
]);
expect(workflows).toMatchObject([{ active: false }, { active: false }]);

//
// ACT
//
const config = new Config({ root: __dirname });
const updater = new UpdateWorkflowCommand(['--all', '--active=true'], config);
await updater.init();
await updater.run();

//
// ASSERT
//
const after = await getAllWorkflows();
expect(after).toMatchObject([{ active: true }, { active: true }]);
});

test('update:workflow can deactivate all workflows', async () => {
//
// ARRANGE
//
const workflows = await Promise.all([
createWorkflowWithTrigger({ active: true }),
createWorkflowWithTrigger({ active: true }),
]);
expect(workflows).toMatchObject([{ active: true }, { active: true }]);

//
// ACT
//
const config = new Config({ root: __dirname });
const updater = new UpdateWorkflowCommand(['--all', '--active=false'], config);
await updater.init();
await updater.run();

//
// ASSERT
//
const after = await getAllWorkflows();
expect(after).toMatchObject([{ active: false }, { active: false }]);
});

test('update:workflow can activate a specific workflow', async () => {
//
// ARRANGE
//
const workflows = (
await Promise.all([
createWorkflowWithTrigger({ active: false }),
createWorkflowWithTrigger({ active: false }),
])
).sort((wf1, wf2) => wf1.id.localeCompare(wf2.id));
expect(workflows).toMatchObject([{ active: false }, { active: false }]);

//
// ACT
//
const config = new Config({ root: __dirname });
const updater = new UpdateWorkflowCommand([`--id=${workflows[0].id}`, '--active=true'], config);
await updater.init();
await updater.run();

//
// ASSERT
//
const after = (await getAllWorkflows()).sort((wf1, wf2) => wf1.id.localeCompare(wf2.id));
expect(after).toMatchObject([{ active: true }, { active: false }]);
});

test('update:workflow can deactivate a specific workflow', async () => {
//
// ARRANGE
//
const workflows = (
await Promise.all([
createWorkflowWithTrigger({ active: true }),
createWorkflowWithTrigger({ active: true }),
])
).sort((wf1, wf2) => wf1.id.localeCompare(wf2.id));
expect(workflows).toMatchObject([{ active: true }, { active: true }]);

//
// ACT
//
const config = new Config({ root: __dirname });
const updater = new UpdateWorkflowCommand([`--id=${workflows[0].id}`, '--active=false'], config);
await updater.init();
await updater.run();

//
// ASSERT
//
const after = (await getAllWorkflows()).sort((wf1, wf2) => wf1.id.localeCompare(wf2.id));
expect(after).toMatchObject([{ active: false }, { active: true }]);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import Container from 'typedi';

import { WorkflowRepository } from '@/databases/repositories/workflow.repository';

import * as testDb from '../../shared/testDb';
import { createWorkflowWithTrigger, getAllWorkflows } from '../../shared/db/workflows';

describe('WorkflowRepository', () => {
beforeAll(async () => {
await testDb.init();
});

beforeEach(async () => {
await testDb.truncate(['Workflow']);
});

afterAll(async () => {
await testDb.terminate();
});

describe('activateAll', () => {
it('should activate all workflows', async () => {
//
// ARRANGE
//
const workflowRepository = Container.get(WorkflowRepository);
const workflows = await Promise.all([
createWorkflowWithTrigger(),
createWorkflowWithTrigger(),
]);
expect(workflows).toMatchObject([{ active: false }, { active: false }]);

//
// ACT
//
await workflowRepository.activateAll();

//
// ASSERT
//
const after = await getAllWorkflows();
expect(after).toMatchObject([{ active: true }, { active: true }]);
});
});

describe('deactivateAll', () => {
it('should deactivate all workflows', async () => {
//
// ARRANGE
//
const workflowRepository = Container.get(WorkflowRepository);
const workflows = await Promise.all([
createWorkflowWithTrigger({ active: true }),
createWorkflowWithTrigger({ active: true }),
]);
expect(workflows).toMatchObject([{ active: true }, { active: true }]);

//
// ACT
//
await workflowRepository.deactivateAll();

//
// ASSERT
//
const after = await getAllWorkflows();
expect(after).toMatchObject([{ active: false }, { active: false }]);
});
});
});
Loading