mirror of
https://github.com/Abdulazizzn/n8n-enterprise-unlocked.git
synced 2025-12-18 10:31:15 +00:00
refactor(core): Decouple NodeTypes from migrations (#14941)
This commit is contained in:
@@ -1,60 +1,16 @@
|
|||||||
import type { WorkflowEntity } from '@/databases/entities/workflow-entity';
|
import { UserError } from 'n8n-workflow';
|
||||||
import type { MigrationContext, IrreversibleMigration } from '@/databases/types';
|
|
||||||
|
|
||||||
interface Workflow {
|
import { WorkflowEntity } from '@/databases/entities/workflow-entity';
|
||||||
id: number;
|
import type { IrreversibleMigration, MigrationContext } from '@/databases/types';
|
||||||
nodes: WorkflowEntity['nodes'] | string;
|
|
||||||
connections: WorkflowEntity['connections'] | string;
|
|
||||||
}
|
|
||||||
|
|
||||||
export class PurgeInvalidWorkflowConnections1675940580449 implements IrreversibleMigration {
|
export class PurgeInvalidWorkflowConnections1675940580449 implements IrreversibleMigration {
|
||||||
async up({ escape, parseJson, runQuery, nodeTypes }: MigrationContext) {
|
async up({ queryRunner }: MigrationContext) {
|
||||||
const workflowsTable = escape.tableName('workflow_entity');
|
const workflowCount = await queryRunner.manager.count(WorkflowEntity);
|
||||||
const workflows: Workflow[] = await runQuery(
|
|
||||||
`SELECT id, nodes, connections FROM ${workflowsTable}`,
|
|
||||||
);
|
|
||||||
|
|
||||||
await Promise.all(
|
if (workflowCount > 0) {
|
||||||
workflows.map(async (workflow) => {
|
throw new UserError(
|
||||||
const connections = parseJson(workflow.connections);
|
'Migration "PurgeInvalidWorkflowConnections1675940580449" is no longer supported. Please upgrade to n8n@1.0.0 first.',
|
||||||
const nodes = parseJson(workflow.nodes);
|
);
|
||||||
|
}
|
||||||
const nodesThatCannotReceiveInput = nodes.reduce<string[]>((acc, node) => {
|
|
||||||
try {
|
|
||||||
const nodeType = nodeTypes.getByNameAndVersion(node.type, node.typeVersion);
|
|
||||||
if ((nodeType.description.inputs?.length ?? []) === 0) {
|
|
||||||
acc.push(node.name);
|
|
||||||
}
|
|
||||||
} catch (error) {}
|
|
||||||
return acc;
|
|
||||||
}, []);
|
|
||||||
|
|
||||||
Object.keys(connections).forEach((sourceNodeName) => {
|
|
||||||
const connection = connections[sourceNodeName];
|
|
||||||
const outputs = Object.keys(connection);
|
|
||||||
|
|
||||||
outputs.forEach((outputConnectionName /* Like `main` */) => {
|
|
||||||
const outputConnection = connection[outputConnectionName];
|
|
||||||
|
|
||||||
// It filters out all connections that are connected to a node that cannot receive input
|
|
||||||
outputConnection.forEach((outputConnectionItem, outputConnectionItemIdx) => {
|
|
||||||
outputConnection[outputConnectionItemIdx] = (outputConnectionItem ?? []).filter(
|
|
||||||
(outgoingConnections) =>
|
|
||||||
!nodesThatCannotReceiveInput.includes(outgoingConnections.node),
|
|
||||||
);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
// Update database with new connections
|
|
||||||
return await runQuery(
|
|
||||||
`UPDATE ${workflowsTable} SET connections = :connections WHERE id = :id`,
|
|
||||||
{
|
|
||||||
connections: JSON.stringify(connections),
|
|
||||||
id: workflow.id,
|
|
||||||
},
|
|
||||||
);
|
|
||||||
}),
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
import type { QueryRunner, ObjectLiteral } from '@n8n/typeorm';
|
import type { QueryRunner, ObjectLiteral } from '@n8n/typeorm';
|
||||||
import type { Logger } from 'n8n-core';
|
import type { Logger } from 'n8n-core';
|
||||||
import type { INodeTypes } from 'n8n-workflow';
|
|
||||||
|
|
||||||
import type { createSchemaBuilder } from './dsl';
|
import type { createSchemaBuilder } from './dsl';
|
||||||
|
|
||||||
@@ -16,7 +15,6 @@ export interface MigrationContext {
|
|||||||
isPostgres: boolean;
|
isPostgres: boolean;
|
||||||
dbName: string;
|
dbName: string;
|
||||||
migrationName: string;
|
migrationName: string;
|
||||||
nodeTypes: INodeTypes;
|
|
||||||
schemaBuilder: ReturnType<typeof createSchemaBuilder>;
|
schemaBuilder: ReturnType<typeof createSchemaBuilder>;
|
||||||
loadSurveyFromDisk(): string | null;
|
loadSurveyFromDisk(): string | null;
|
||||||
parseJson<T>(data: string | T): T;
|
parseJson<T>(data: string | T): T;
|
||||||
|
|||||||
@@ -9,7 +9,6 @@ import { jsonParse, UnexpectedError } from 'n8n-workflow';
|
|||||||
import { inTest } from '@/constants';
|
import { inTest } from '@/constants';
|
||||||
import { createSchemaBuilder } from '@/databases/dsl';
|
import { createSchemaBuilder } from '@/databases/dsl';
|
||||||
import type { BaseMigration, Migration, MigrationContext, MigrationFn } from '@/databases/types';
|
import type { BaseMigration, Migration, MigrationContext, MigrationFn } from '@/databases/types';
|
||||||
import { NodeTypes } from '@/node-types';
|
|
||||||
|
|
||||||
const PERSONALIZATION_SURVEY_FILENAME = 'personalizationSurvey.json';
|
const PERSONALIZATION_SURVEY_FILENAME = 'personalizationSurvey.json';
|
||||||
|
|
||||||
@@ -108,7 +107,6 @@ const createContext = (queryRunner: QueryRunner, migration: Migration): Migratio
|
|||||||
migrationName: migration.name,
|
migrationName: migration.name,
|
||||||
queryRunner,
|
queryRunner,
|
||||||
schemaBuilder: createSchemaBuilder(tablePrefix, queryRunner),
|
schemaBuilder: createSchemaBuilder(tablePrefix, queryRunner),
|
||||||
nodeTypes: Container.get(NodeTypes),
|
|
||||||
loadSurveyFromDisk,
|
loadSurveyFromDisk,
|
||||||
parseJson,
|
parseJson,
|
||||||
escape: {
|
escape: {
|
||||||
|
|||||||
Reference in New Issue
Block a user