fix(core): Upsert credentials and workflows in the import:* commands (#5231)

This commit is contained in:
कारतोफ्फेलस्क्रिप्ट™ 2023-01-24 19:05:23 +01:00 committed by GitHub
parent 237b1d8614
commit 259296c5c9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 48 additions and 35 deletions

View file

@ -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';
@ -76,6 +75,7 @@ export class ImportCredentialsCommand extends Command {
let totalImported = 0;
try {
disableAutoGeneratedIds(CredentialsEntity);
await Db.init();
await this.initOwnerCredentialRole();
@ -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() {

View file

@ -10,15 +10,14 @@
/* 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';
@ -26,6 +25,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';
@ -97,6 +97,8 @@ export class ImportWorkflowsCommand extends Command {
}
try {
disableAutoGeneratedIds(WorkflowEntity);
await Db.init();
await this.initOwnerWorkflowRole();
@ -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() {

View file

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