diff --git a/packages/cli/src/commands/import/credentials.ts b/packages/cli/src/commands/import/credentials.ts index 3d834396c5..2132055dbf 100644 --- a/packages/cli/src/commands/import/credentials.ts +++ b/packages/cli/src/commands/import/credentials.ts @@ -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(newCredential); - - const newSharedCredential = new SharedCredentials(); - - Object.assign(newSharedCredential, { - credentials: savedCredential, - user, - role: this.ownerCredentialRole, - }); - - await this.transactionManager.save(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() { diff --git a/packages/cli/src/commands/import/workflow.ts b/packages/cli/src/commands/import/workflow.ts index 27447ca464..17e1eee2fc 100644 --- a/packages/cli/src/commands/import/workflow.ts +++ b/packages/cli/src/commands/import/workflow.ts @@ -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(newWorkflow); - - const newSharedWorkflow = new SharedWorkflow(); - - Object.assign(newSharedWorkflow, { - workflow: savedWorkflow, - user, - role: this.ownerWorkflowRole, - }); - - await this.transactionManager.save(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() { diff --git a/packages/cli/src/databases/utils/commandHelpers.ts b/packages/cli/src/databases/utils/commandHelpers.ts new file mode 100644 index 0000000000..775d6cb6ed --- /dev/null +++ b/packages/cli/src/databases/utils/commandHelpers.ts @@ -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); +};