Skip to content

Commit

Permalink
fix: Upsert data in the import:* commands
Browse files Browse the repository at this point in the history
  • Loading branch information
netroy committed Jan 23, 2023
1 parent bd14ec1 commit 43f5b84
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 32 deletions.
26 changes: 11 additions & 15 deletions packages/cli/src/commands/import/credentials.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ 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 +77,7 @@ export class ImportCredentialsCommand extends Command {
let totalImported = 0;

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

await this.initOwnerCredentialRole();
Expand Down Expand Up @@ -165,21 +167,15 @@ 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,
},
['credentialsId', 'userId'],
);
}

private async getOwner() {
Expand Down
30 changes: 13 additions & 17 deletions packages/cli/src/commands/import/workflow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@
/* 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';
Expand All @@ -26,6 +24,7 @@ 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 +96,8 @@ export class ImportWorkflowsCommand extends Command {
}

try {
disableAutoGeneratedIds(WorkflowEntity);

await Db.init();

await this.initOwnerWorkflowRole();
Expand Down Expand Up @@ -207,21 +208,16 @@ 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'],
);
}

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);
};

0 comments on commit 43f5b84

Please sign in to comment.