-
Notifications
You must be signed in to change notification settings - Fork 9.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(editor): Prevent creation of input connections for nodes without input slot #5425
Merged
OlegIvaniv
merged 11 commits into
master
from
n8n-6132-bug-phantom-splitinbatches-executions
Feb 9, 2023
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
d5c66b5
fix(editor): Prevent creation of input connections for nodes without …
OlegIvaniv b77511f
WIP: Workflow checks service and controller
OlegIvaniv ebf6bba
fix: Created SQLite migration to remove broken connections
krynble 6c4604c
Cleanup & add mysql/posgres migrations
OlegIvaniv d6350e8
Merge branch 'master' into n8n-6132-bug-phantom-splitinbatches-execut…
OlegIvaniv aa9abd7
Linter fixes
OlegIvaniv 69298e1
Unify the migration scripts
OlegIvaniv 0ef866d
Escape migration workflow_entity
OlegIvaniv a69f122
Wrap the migration in try/catch and do not parse nodes and connection…
OlegIvaniv ac57811
Do migration changes also fro mysql
OlegIvaniv 6a77fcc
refactor: Wrap only the necessary call in try catch block
krynble File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
...ges/cli/src/databases/migrations/mysqldb/1675940580449-PurgeInvalidWorkflowConnections.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import { MigrationInterface, QueryRunner } from 'typeorm'; | ||
import { getTablePrefix, logMigrationEnd, logMigrationStart } from '@db/utils/migrationHelpers'; | ||
import { NodeTypes } from '@/NodeTypes'; | ||
import { IConnections, INode } from 'n8n-workflow'; | ||
import { getLogger } from '@/Logger'; | ||
|
||
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(` | ||
SELECT id, nodes, connections | ||
FROM \`${tablePrefix}workflow_entity\` | ||
`); | ||
|
||
const nodeTypes = NodeTypes(); | ||
|
||
workflows.forEach(async (workflow) => { | ||
let connections: IConnections = workflow.connections; | ||
const nodes: INode[] = workflow.nodes; | ||
|
||
const nodesThatCannotReceiveInput: string[] = nodes.reduce((acc, node) => { | ||
try { | ||
const nodeType = nodeTypes.getByNameAndVersion(node.type, node.typeVersion); | ||
if ((nodeType.description.inputs?.length ?? []) === 0) { | ||
acc.push(node.name); | ||
} | ||
} catch (error) { | ||
getLogger().warn(`Migration ${this.name} failed with error: ${error.message}`); | ||
} | ||
return acc; | ||
}, [] as string[]); | ||
|
||
Object.keys(connections).forEach((sourceNodeName) => { | ||
const connection = connections[sourceNodeName]; | ||
const outputs = Object.keys(connection); | ||
|
||
outputs.forEach((outputConnectionName /* Like `main` */, idx) => { | ||
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), | ||
); | ||
}); | ||
|
||
// Filter out output connection items that are empty | ||
connection[outputConnectionName] = connection[outputConnectionName].filter( | ||
(item) => item.length > 0, | ||
); | ||
|
||
// Delete the output connection container if it is empty | ||
if (connection[outputConnectionName].length === 0) { | ||
delete connection[outputConnectionName]; | ||
} | ||
}); | ||
|
||
// Finally delete the source node if it has no output connections | ||
if (Object.keys(connection).length === 0) { | ||
delete connections[sourceNodeName]; | ||
} | ||
}); | ||
|
||
// Update database with new connections | ||
const [updateQuery, updateParams] = queryRunner.connection.driver.escapeQueryWithParameters( | ||
` | ||
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
.../cli/src/databases/migrations/postgresdb/1675940580449-PurgeInvalidWorkflowConnections.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import { MigrationInterface, QueryRunner } from 'typeorm'; | ||
import { getTablePrefix, logMigrationEnd, logMigrationStart } from '@db/utils/migrationHelpers'; | ||
import { NodeTypes } from '@/NodeTypes'; | ||
import { IConnections, INode } from 'n8n-workflow'; | ||
import { getLogger } from '@/Logger'; | ||
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(` | ||
SELECT id, nodes, connections | ||
FROM "${tablePrefix}workflow_entity" | ||
`); | ||
|
||
const nodeTypes = NodeTypes(); | ||
|
||
workflows.forEach(async (workflow) => { | ||
let connections: IConnections = workflow.connections; | ||
const nodes: INode[] = workflow.nodes; | ||
|
||
const nodesThatCannotReceiveInput: string[] = nodes.reduce((acc, node) => { | ||
try { | ||
const nodeType = nodeTypes.getByNameAndVersion(node.type, node.typeVersion); | ||
if ((nodeType.description.inputs?.length ?? []) === 0) { | ||
acc.push(node.name); | ||
} | ||
} catch (error) { | ||
getLogger().warn(`Migration ${this.name} failed with error: ${error.message}`); | ||
} | ||
return acc; | ||
}, [] as string[]); | ||
|
||
Object.keys(connections).forEach((sourceNodeName) => { | ||
const connection = connections[sourceNodeName]; | ||
const outputs = Object.keys(connection); | ||
|
||
outputs.forEach((outputConnectionName /* Like `main` */, idx) => { | ||
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), | ||
); | ||
}); | ||
|
||
// Filter out output connection items that are empty | ||
connection[outputConnectionName] = connection[outputConnectionName].filter( | ||
(item) => item.length > 0, | ||
); | ||
|
||
// Delete the output connection container if it is empty | ||
if (connection[outputConnectionName].length === 0) { | ||
delete connection[outputConnectionName]; | ||
} | ||
}); | ||
|
||
// Finally delete the source node if it has no output connections | ||
if (Object.keys(connection).length === 0) { | ||
delete connections[sourceNodeName]; | ||
} | ||
}); | ||
|
||
// Update database with new connections | ||
const [updateQuery, updateParams] = queryRunner.connection.driver.escapeQueryWithParameters( | ||
` | ||
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
...ages/cli/src/databases/migrations/sqlite/1675940580449-PurgeInvalidWorkflowConnections.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import { MigrationInterface, QueryRunner } from 'typeorm'; | ||
import { getTablePrefix, logMigrationEnd, logMigrationStart } from '@db/utils/migrationHelpers'; | ||
import { NodeTypes } from '@/NodeTypes'; | ||
import { IConnections, INode } from 'n8n-workflow'; | ||
import { getLogger } from '@/Logger'; | ||
|
||
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: string; connections: string }> = | ||
await queryRunner.query(` | ||
SELECT id, nodes, connections | ||
FROM "${tablePrefix}workflow_entity" | ||
`); | ||
|
||
const nodeTypes = NodeTypes(); | ||
|
||
workflows.forEach(async (workflow) => { | ||
let connections: IConnections = JSON.parse(workflow.connections); | ||
const nodes: INode[] = JSON.parse(workflow.nodes); | ||
|
||
const nodesThatCannotReceiveInput: string[] = nodes.reduce((acc, node) => { | ||
try { | ||
const nodeType = nodeTypes.getByNameAndVersion(node.type, node.typeVersion); | ||
if ((nodeType.description.inputs?.length ?? []) === 0) { | ||
acc.push(node.name); | ||
} | ||
} catch (error) { | ||
getLogger().warn(`Migration ${this.name} failed with error: ${error.message}`); | ||
} | ||
return acc; | ||
}, [] as string[]); | ||
|
||
Object.keys(connections).forEach((sourceNodeName) => { | ||
const connection = connections[sourceNodeName]; | ||
const outputs = Object.keys(connection); | ||
|
||
outputs.forEach((outputConnectionName /* Like `main` */, idx) => { | ||
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), | ||
); | ||
}); | ||
|
||
// Filter out output connection items that are empty | ||
connection[outputConnectionName] = connection[outputConnectionName].filter( | ||
(item) => item.length > 0, | ||
); | ||
|
||
// Delete the output connection container if it is empty | ||
if (connection[outputConnectionName].length === 0) { | ||
delete connection[outputConnectionName]; | ||
} | ||
}); | ||
|
||
// Finally delete the source node if it has no output connections | ||
if (Object.keys(connection).length === 0) { | ||
delete connections[sourceNodeName]; | ||
} | ||
}); | ||
|
||
// Update database with new connections | ||
const [updateQuery, updateParams] = queryRunner.connection.driver.escapeQueryWithParameters( | ||
` | ||
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We had to move the loading of nodes and credentials up to make sure they are available in the migrations. @netroy Do you see any issues with this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be fine. I don't foresee any issues.