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): Upsert credentials and workflows in the import:* commands #5231

Merged
merged 2 commits into from
Jan 24, 2023
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
36 changes: 18 additions & 18 deletions packages/cli/src/commands/import/credentials.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,19 @@
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
/* eslint-disable no-console */
import { Command, flags } from '@oclif/command';

import { Credentials, UserSettings } from 'n8n-core';

import { LoggerProxy } from 'n8n-workflow';

import fs from 'fs';
import glob from 'fast-glob';
import type { EntityManager } from 'typeorm';
import { getLogger } from '@/Logger';
import config from '@/config';
import * as Db from '@/Db';
import { User } from '@db/entities/User';
import { SharedCredentials } from '@db/entities/SharedCredentials';
import { Role } from '@db/entities/Role';
import { CredentialsEntity } from '@db/entities/CredentialsEntity';
import { disableAutoGeneratedIds } from '@db/utils/commandHelpers';

const FIX_INSTRUCTION =
'Please fix the database by running ./packages/cli/bin/n8n user-management:reset';
Expand Down Expand Up @@ -76,6 +75,7 @@ export class ImportCredentialsCommand extends Command {
let totalImported = 0;

try {
disableAutoGeneratedIds(CredentialsEntity);
await Db.init();

await this.initOwnerCredentialRole();
Expand Down Expand Up @@ -165,21 +165,21 @@ export class ImportCredentialsCommand extends Command {
}

private async storeCredential(credential: object, user: User) {
const newCredential = new CredentialsEntity();

Object.assign(newCredential, credential);

const savedCredential = await this.transactionManager.save<CredentialsEntity>(newCredential);

const newSharedCredential = new SharedCredentials();

Object.assign(newSharedCredential, {
credentials: savedCredential,
user,
role: this.ownerCredentialRole,
});

await this.transactionManager.save<SharedCredentials>(newSharedCredential);
const result = await this.transactionManager.upsert(CredentialsEntity, credential, ['id']);
await this.transactionManager.upsert(
SharedCredentials,
{
credentialsId: result.identifiers[0].id,
userId: user.id,
roleId: this.ownerCredentialRole.id,
},
['credentialsId', 'userId'],
);
if (config.getEnv('database.type') === 'postgresdb') {
await this.transactionManager.query(
"SELECT setval('credentials_entity_id_seq', (SELECT MAX(id) from credentials_entity))",
);
}
}

private async getOwner() {
Expand Down
36 changes: 19 additions & 17 deletions packages/cli/src/commands/import/workflow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,22 @@
/* eslint-disable no-console */
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
import { Command, flags } from '@oclif/command';

import { INode, INodeCredentialsDetails, LoggerProxy } from 'n8n-workflow';

import fs from 'fs';
import glob from 'fast-glob';
import { UserSettings } from 'n8n-core';
import type { EntityManager } from 'typeorm';
import { v4 as uuid } from 'uuid';
import { getLogger } from '@/Logger';
import config from '@/config';
import * as Db from '@/Db';
import { SharedWorkflow } from '@db/entities/SharedWorkflow';
import { WorkflowEntity } from '@db/entities/WorkflowEntity';
import { Role } from '@db/entities/Role';
import { User } from '@db/entities/User';
import { setTagsForImport } from '@/TagHelpers';
import type { ICredentialsDb, IWorkflowToImport } from '@/Interfaces';
import { disableAutoGeneratedIds } from '@db/utils/commandHelpers';

const FIX_INSTRUCTION =
'Please fix the database by running ./packages/cli/bin/n8n user-management:reset';
Expand Down Expand Up @@ -97,6 +97,8 @@ export class ImportWorkflowsCommand extends Command {
}

try {
disableAutoGeneratedIds(WorkflowEntity);

await Db.init();

await this.initOwnerWorkflowRole();
Expand Down Expand Up @@ -207,21 +209,21 @@ export class ImportWorkflowsCommand extends Command {
}

private async storeWorkflow(workflow: object, user: User) {
const newWorkflow = new WorkflowEntity();

Object.assign(newWorkflow, workflow);

const savedWorkflow = await this.transactionManager.save<WorkflowEntity>(newWorkflow);

const newSharedWorkflow = new SharedWorkflow();

Object.assign(newSharedWorkflow, {
workflow: savedWorkflow,
user,
role: this.ownerWorkflowRole,
});

await this.transactionManager.save<SharedWorkflow>(newSharedWorkflow);
const result = await this.transactionManager.upsert(WorkflowEntity, workflow, ['id']);
await this.transactionManager.upsert(
SharedWorkflow,
{
workflowId: result.identifiers[0].id,
userId: user.id,
roleId: this.ownerWorkflowRole.id,
},
['workflowId', 'userId'],
);
if (config.getEnv('database.type') === 'postgresdb') {
await this.transactionManager.query(
"SELECT setval('workflow_entity_id_seq', (SELECT MAX(id) from workflow_entity))",
);
}
}

private async getOwner() {
Expand Down
11 changes: 11 additions & 0 deletions packages/cli/src/databases/utils/commandHelpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import type { WorkflowEntity } from '@db/entities/WorkflowEntity';
import type { CredentialsEntity } from '@db/entities/CredentialsEntity';
import { getMetadataArgsStorage } from 'typeorm';

export const disableAutoGeneratedIds = (
entityClass: typeof WorkflowEntity | typeof CredentialsEntity,
): void => {
const decoratorMetadata = getMetadataArgsStorage().generations;
const index = decoratorMetadata.findIndex((metadata) => metadata.target === entityClass);
decoratorMetadata.splice(index, 1);
};