* first commit for postgres migration * (not working) * sqlite migration * quicksave * fix tests * fix pg test * fix postgres * fix variables import * fix execution saving * add user settings fix * change migration to single lines * patch preferences endpoint * cleanup * improve variable import * cleanup unusued code * Update packages/cli/src/PublicApi/v1/handlers/workflows/workflows.handler.ts Co-authored-by: Omar Ajoue <krynble@gmail.com> * address review notes * fix var update/import * refactor: Separate execution data to its own table (#6323) * wip: Temporary migration process * refactor: Create boilerplate repository methods for executions * fix: Lint issues * refactor: Added search endpoint to repository * refactor: Make the execution list work again * wip: Updating how we create and update executions everywhere * fix: Lint issues and remove most of the direct access to execution model * refactor: Remove includeWorkflowData flag and fix more tests * fix: Lint issues * fix: Fixed ordering of executions for FE, removed transaction when saving execution and removed unnecessary update * refactor: Add comment about missing feature * refactor: Refactor counting executions * refactor: Add migration for other dbms and fix issues found * refactor: Fix lint issues * refactor: Remove unnecessary comment and auto inject repo to internal hooks * refactor: remove type assertion * fix: Fix broken tests * fix: Remove unnecessary import * Remove unnecessary toString() call Co-authored-by: Iván Ovejero <ivov.src@gmail.com> * fix: Address comments after review * refactor: Remove unused import * fix: Lint issues * fix: Add correct migration files --------- Co-authored-by: Iván Ovejero <ivov.src@gmail.com> * remove null values from credential export * fix: Fix an issue with queue mode where all running execution would be returned * fix: Update n8n node to allow for workflow ids with letters * set upstream on set branch * remove typo * add nodeAccess to credentials * fix unsaved run check for undefined id * fix(core): Rename version control feature to source control (#6480) * rename versionControl to sourceControl * fix source control tooltip wording --------- Co-authored-by: Romain Minaud <romain.minaud@gmail.com> * fix(editor): Pay 548 hide the set up version control button (#6485) * feat(DebugHelper Node): Fix and include in main app (#6406) * improve node a bit * fixing continueOnFail() ton contain error in json * improve pairedItem * fix random data returning object results * fix nanoId length typo * update pnpm-lock file --------- Co-authored-by: Marcus <marcus@n8n.io> * fix(editor): Remove setup source control CTA button * fix(editor): Remove setup source control CTA button --------- Co-authored-by: Michael Auerswald <michael.auerswald@gmail.com> Co-authored-by: Marcus <marcus@n8n.io> * fix(editor): Update source control docs links (#6488) * feat(DebugHelper Node): Fix and include in main app (#6406) * improve node a bit * fixing continueOnFail() ton contain error in json * improve pairedItem * fix random data returning object results * fix nanoId length typo * update pnpm-lock file --------- Co-authored-by: Marcus <marcus@n8n.io> * feat(editor): Replace root events with event bus events (no-changelog) (#6454) * feat: replace root events with event bus events * fix: prevent cypress from replacing global with globalThis in import path * feat: remove emitter mixin * fix: replace component events with event bus * fix: fix linting issue * fix: fix breaking expression switch * chore: prettify ndv e2e suite code * fix(editor): Update source control docs links --------- Co-authored-by: Michael Auerswald <michael.auerswald@gmail.com> Co-authored-by: Marcus <marcus@n8n.io> Co-authored-by: Alex Grozav <alex@grozav.com> * fix tag endpoint regex --------- Co-authored-by: Omar Ajoue <krynble@gmail.com> Co-authored-by: Iván Ovejero <ivov.src@gmail.com> Co-authored-by: Romain Minaud <romain.minaud@gmail.com> Co-authored-by: Csaba Tuncsik <csaba@n8n.io> Co-authored-by: Marcus <marcus@n8n.io> Co-authored-by: Alex Grozav <alex@grozav.com>
284 lines
8.1 KiB
TypeScript
284 lines
8.1 KiB
TypeScript
import { flags } from '@oclif/command';
|
|
import type { INode, INodeCredentialsDetails } from 'n8n-workflow';
|
|
import { jsonParse } from 'n8n-workflow';
|
|
import fs from 'fs';
|
|
import glob from 'fast-glob';
|
|
import { Container } from 'typedi';
|
|
import type { EntityManager } from 'typeorm';
|
|
import { v4 as uuid } from 'uuid';
|
|
import * as Db from '@/Db';
|
|
import { SharedWorkflow } from '@db/entities/SharedWorkflow';
|
|
import { WorkflowEntity } from '@db/entities/WorkflowEntity';
|
|
import type { Role } from '@db/entities/Role';
|
|
import type { User } from '@db/entities/User';
|
|
import { setTagsForImport } from '@/TagHelpers';
|
|
import { RoleRepository } from '@db/repositories';
|
|
import { disableAutoGeneratedIds } from '@db/utils/commandHelpers';
|
|
import type { ICredentialsDb, IWorkflowToImport } from '@/Interfaces';
|
|
import { replaceInvalidCredentials } from '@/WorkflowHelpers';
|
|
import { BaseCommand, UM_FIX_INSTRUCTION } from '../BaseCommand';
|
|
import { generateNanoId } from '@/databases/utils/generators';
|
|
|
|
function assertHasWorkflowsToImport(workflows: unknown): asserts workflows is IWorkflowToImport[] {
|
|
if (!Array.isArray(workflows)) {
|
|
throw new Error(
|
|
'File does not seem to contain workflows. Make sure the workflows are contained in an array.',
|
|
);
|
|
}
|
|
|
|
for (const workflow of workflows) {
|
|
if (
|
|
typeof workflow !== 'object' ||
|
|
!Object.prototype.hasOwnProperty.call(workflow, 'nodes') ||
|
|
!Object.prototype.hasOwnProperty.call(workflow, 'connections')
|
|
) {
|
|
throw new Error('File does not seem to contain valid workflows.');
|
|
}
|
|
}
|
|
}
|
|
|
|
export class ImportWorkflowsCommand extends BaseCommand {
|
|
static description = 'Import workflows';
|
|
|
|
static examples = [
|
|
'$ n8n import:workflow --input=file.json',
|
|
'$ n8n import:workflow --separate --input=backups/latest/',
|
|
'$ n8n import:workflow --input=file.json --userId=1d64c3d2-85fe-4a83-a649-e446b07b3aae',
|
|
'$ n8n import:workflow --separate --input=backups/latest/ --userId=1d64c3d2-85fe-4a83-a649-e446b07b3aae',
|
|
];
|
|
|
|
static flags = {
|
|
help: flags.help({ char: 'h' }),
|
|
input: flags.string({
|
|
char: 'i',
|
|
description: 'Input file name or directory if --separate is used',
|
|
}),
|
|
separate: flags.boolean({
|
|
description: 'Imports *.json files from directory provided by --input',
|
|
}),
|
|
userId: flags.string({
|
|
description: 'The ID of the user to assign the imported workflows to',
|
|
}),
|
|
};
|
|
|
|
private ownerWorkflowRole: Role;
|
|
|
|
private transactionManager: EntityManager;
|
|
|
|
async init() {
|
|
disableAutoGeneratedIds(WorkflowEntity);
|
|
await super.init();
|
|
}
|
|
|
|
async run(): Promise<void> {
|
|
// eslint-disable-next-line @typescript-eslint/no-shadow
|
|
const { flags } = this.parse(ImportWorkflowsCommand);
|
|
|
|
if (!flags.input) {
|
|
this.logger.info('An input file or directory with --input must be provided');
|
|
return;
|
|
}
|
|
|
|
if (flags.separate) {
|
|
if (fs.existsSync(flags.input)) {
|
|
if (!fs.lstatSync(flags.input).isDirectory()) {
|
|
this.logger.info('The argument to --input must be a directory');
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
|
|
await this.initOwnerWorkflowRole();
|
|
const user = flags.userId ? await this.getAssignee(flags.userId) : await this.getOwner();
|
|
|
|
const credentials = await Db.collections.Credentials.find();
|
|
const tags = await Db.collections.Tag.find();
|
|
|
|
let totalImported = 0;
|
|
|
|
if (flags.separate) {
|
|
let { input: inputPath } = flags;
|
|
|
|
if (process.platform === 'win32') {
|
|
inputPath = inputPath.replace(/\\/g, '/');
|
|
}
|
|
|
|
const files = await glob('*.json', {
|
|
cwd: inputPath,
|
|
absolute: true,
|
|
});
|
|
|
|
totalImported = files.length;
|
|
this.logger.info(`Importing ${totalImported} workflows...`);
|
|
await Db.getConnection().transaction(async (transactionManager) => {
|
|
this.transactionManager = transactionManager;
|
|
|
|
for (const file of files) {
|
|
const workflow = jsonParse<IWorkflowToImport>(
|
|
fs.readFileSync(file, { encoding: 'utf8' }),
|
|
);
|
|
if (!workflow.id) {
|
|
workflow.id = generateNanoId();
|
|
}
|
|
|
|
if (credentials.length > 0) {
|
|
workflow.nodes.forEach((node: INode) => {
|
|
this.transformCredentials(node, credentials);
|
|
|
|
if (!node.id) {
|
|
// eslint-disable-next-line no-param-reassign
|
|
node.id = uuid();
|
|
}
|
|
});
|
|
}
|
|
|
|
if (Object.prototype.hasOwnProperty.call(workflow, 'tags')) {
|
|
await setTagsForImport(transactionManager, workflow, tags);
|
|
}
|
|
|
|
if (workflow.active) {
|
|
this.logger.info(
|
|
`Deactivating workflow "${workflow.name}" during import, remember to activate it later.`,
|
|
);
|
|
workflow.active = false;
|
|
}
|
|
|
|
await this.storeWorkflow(workflow, user);
|
|
}
|
|
});
|
|
|
|
this.reportSuccess(totalImported);
|
|
process.exit();
|
|
}
|
|
|
|
const workflows = jsonParse<IWorkflowToImport[]>(
|
|
fs.readFileSync(flags.input, { encoding: 'utf8' }),
|
|
);
|
|
|
|
assertHasWorkflowsToImport(workflows);
|
|
|
|
totalImported = workflows.length;
|
|
|
|
await Db.getConnection().transaction(async (transactionManager) => {
|
|
this.transactionManager = transactionManager;
|
|
|
|
for (const workflow of workflows) {
|
|
let oldCredentialFormat = false;
|
|
if (credentials.length > 0) {
|
|
workflow.nodes.forEach((node: INode) => {
|
|
this.transformCredentials(node, credentials);
|
|
if (!node.id) {
|
|
// eslint-disable-next-line no-param-reassign
|
|
node.id = uuid();
|
|
}
|
|
if (!node.credentials?.id) {
|
|
oldCredentialFormat = true;
|
|
}
|
|
});
|
|
}
|
|
if (oldCredentialFormat) {
|
|
try {
|
|
await replaceInvalidCredentials(workflow as unknown as WorkflowEntity);
|
|
} catch (error) {
|
|
this.logger.error('Failed to replace invalid credential', error as Error);
|
|
}
|
|
}
|
|
if (Object.prototype.hasOwnProperty.call(workflow, 'tags')) {
|
|
await setTagsForImport(transactionManager, workflow, tags);
|
|
}
|
|
if (workflow.active) {
|
|
this.logger.info(
|
|
`Deactivating workflow "${workflow.name}" during import, remember to activate it later.`,
|
|
);
|
|
workflow.active = false;
|
|
}
|
|
|
|
await this.storeWorkflow(workflow, user);
|
|
}
|
|
});
|
|
|
|
this.reportSuccess(totalImported);
|
|
}
|
|
|
|
async catch(error: Error) {
|
|
this.logger.error('An error occurred while importing workflows. See log messages for details.');
|
|
this.logger.error(error.message);
|
|
}
|
|
|
|
private reportSuccess(total: number) {
|
|
this.logger.info(`Successfully imported ${total} ${total === 1 ? 'workflow.' : 'workflows.'}`);
|
|
}
|
|
|
|
private async initOwnerWorkflowRole() {
|
|
const ownerWorkflowRole = await Container.get(RoleRepository).findWorkflowOwnerRole();
|
|
|
|
if (!ownerWorkflowRole) {
|
|
throw new Error(`Failed to find owner workflow role. ${UM_FIX_INSTRUCTION}`);
|
|
}
|
|
|
|
this.ownerWorkflowRole = ownerWorkflowRole;
|
|
}
|
|
|
|
private async storeWorkflow(workflow: object, user: User) {
|
|
const result = await this.transactionManager.upsert(WorkflowEntity, workflow, ['id']);
|
|
await this.transactionManager.upsert(
|
|
SharedWorkflow,
|
|
{
|
|
workflowId: result.identifiers[0].id as string,
|
|
userId: user.id,
|
|
roleId: this.ownerWorkflowRole.id,
|
|
},
|
|
['workflowId', 'userId'],
|
|
);
|
|
}
|
|
|
|
private async getOwner() {
|
|
const ownerGlobalRole = await Container.get(RoleRepository).findGlobalOwnerRole();
|
|
|
|
const owner =
|
|
ownerGlobalRole &&
|
|
(await Db.collections.User.findOneBy({ globalRoleId: ownerGlobalRole?.id }));
|
|
|
|
if (!owner) {
|
|
throw new Error(`Failed to find owner. ${UM_FIX_INSTRUCTION}`);
|
|
}
|
|
|
|
return owner;
|
|
}
|
|
|
|
private async getAssignee(userId: string) {
|
|
const user = await Db.collections.User.findOneBy({ id: userId });
|
|
|
|
if (!user) {
|
|
throw new Error(`Failed to find user with ID ${userId}`);
|
|
}
|
|
|
|
return user;
|
|
}
|
|
|
|
private transformCredentials(node: INode, credentialsEntities: ICredentialsDb[]) {
|
|
if (node.credentials) {
|
|
const allNodeCredentials = Object.entries(node.credentials);
|
|
for (const [type, name] of allNodeCredentials) {
|
|
if (typeof name === 'string') {
|
|
const nodeCredentials: INodeCredentialsDetails = {
|
|
id: null,
|
|
name,
|
|
};
|
|
|
|
const matchingCredentials = credentialsEntities.filter(
|
|
(credentials) => credentials.name === name && credentials.type === type,
|
|
);
|
|
|
|
if (matchingCredentials.length === 1) {
|
|
nodeCredentials.id = matchingCredentials[0].id;
|
|
}
|
|
|
|
// eslint-disable-next-line no-param-reassign
|
|
node.credentials[type] = nodeCredentials;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|