refactor(core): Delete boilerplate code across migrations (no-changelog) (#5254)

This commit is contained in:
कारतोफ्फेलस्क्रिप्ट™
2023-05-05 09:28:59 +00:00
committed by GitHub
parent d5c44987f4
commit 82fe6383ef
122 changed files with 879 additions and 1844 deletions

View File

@@ -1,28 +1,19 @@
import { MigrationInterface, QueryRunner } from 'typeorm';
import { getTablePrefix, logMigrationEnd, logMigrationStart } from '@db/utils/migrationHelpers';
import type { IConnections, INode } from 'n8n-workflow';
import type { MigrationContext, IrreversibleMigration } from '@db/types';
import { NodeTypes } from '@/NodeTypes';
import { IConnections, INode } from 'n8n-workflow';
import { getLogger } from '@/Logger';
import { Container } from 'typedi';
export class PurgeInvalidWorkflowConnections1675940580449 implements MigrationInterface {
name = 'PurgeInvalidWorkflowConnections1675940580449';
async up(queryRunner: QueryRunner): Promise<void> {
logMigrationStart(this.name);
const tablePrefix = getTablePrefix();
const workflows: Array<{ id: number; nodes: INode[]; connections: IConnections }> =
await queryRunner.query(`
export class PurgeInvalidWorkflowConnections1675940580449 implements IrreversibleMigration {
async up({ queryRunner, tablePrefix, migrationName, logger }: MigrationContext) {
const workflows = (await queryRunner.query(`
SELECT id, nodes, connections
FROM "${tablePrefix}workflow_entity"
`);
`)) as Array<{ id: number; nodes: INode[]; connections: IConnections }>;
const nodeTypes = Container.get(NodeTypes);
workflows.forEach(async (workflow) => {
let connections: IConnections = workflow.connections;
const nodes: INode[] = workflow.nodes;
const { connections, nodes } = workflow;
const nodesThatCannotReceiveInput: string[] = nodes.reduce((acc, node) => {
try {
@@ -31,7 +22,7 @@ export class PurgeInvalidWorkflowConnections1675940580449 implements MigrationIn
acc.push(node.name);
}
} catch (error) {
getLogger().warn(`Migration ${this.name} failed with error: ${error.message}`);
logger.warn(`Migration ${migrationName} failed with error: ${(error as Error).message}`);
}
return acc;
}, [] as string[]);
@@ -40,7 +31,7 @@ export class PurgeInvalidWorkflowConnections1675940580449 implements MigrationIn
const connection = connections[sourceNodeName];
const outputs = Object.keys(connection);
outputs.forEach((outputConnectionName /* Like `main` */, idx) => {
outputs.forEach((outputConnectionName /* Like `main` */) => {
const outputConnection = connection[outputConnectionName];
// It filters out all connections that are connected to a node that cannot receive input
@@ -55,22 +46,14 @@ export class PurgeInvalidWorkflowConnections1675940580449 implements MigrationIn
// Update database with new connections
const [updateQuery, updateParams] = queryRunner.connection.driver.escapeQueryWithParameters(
`
UPDATE "${tablePrefix}workflow_entity"
SET connections = :connections
WHERE id = '${workflow.id}'
`,
`UPDATE "${tablePrefix}workflow_entity"
SET connections = :connections
WHERE id = '${workflow.id}'`,
{ connections: JSON.stringify(connections) },
{},
);
await queryRunner.query(updateQuery, updateParams);
});
logMigrationEnd(this.name);
}
async down(queryRunner: QueryRunner): Promise<void> {
// No need to revert this migration
}
}