DEV Community

Cover image for How to Build Maintenance Sub-Workflow to Use on n8n for MySQL
Federico Moretti
Federico Moretti

Posted on

How to Build Maintenance Sub-Workflow to Use on n8n for MySQL

We all love n8n, don’t you? But sometimes I have complex requirements and a simple workflow isn’t enough: sub-workflows can help to maintain projects that need to be updated often. Here’s, for example, how I use them to handle MySQL tables with standard nodes.


I’m working with n8n for several projects and I find it very useful to automate processes. However, it has some limitations when talking about operations that need to be executed once in a while. I mean, n8n itself is ideal for repetitive actions, but doesn’t handle sporadic actions at best.

Let’s pretend that you have a MySQL database, but it can be any kind of, and you suddenly need to alter its existing schema. n8n provides different kinds of nodes to achieve this, but workflows are intended do be executed repetitively in cycle: you should have planned in advance a schema update on certain circumstances.

What if you didn’t? Not all SQL operations can be scheduled. Recently, I had to add a new table that wasn’t planned, and I deleted one that became useless. We didn’t expect this to happen, when we created the n8n workflow, and we can’t know today if we will need something similar in the future.

Maintenance Sub-Workflows

This is where the importance of maintenance sub-workflows comes in. I’m going to show you what I did for MySQL, but I created others for HTTP requests as well: they have in common the CRUD principle, because I basically need to modify on demand an already defined structure.

It all starts with a Chat Trigger node. You don’t have to open this to the public, since you’ll keep it private to be used internally: I rely on it to exclude unexpected triggering. It must be triggered manually when required by whom is in charge of maintaining the workflow it belongs to.

Then, depending on your needs, I suggest to add one or more Switch to handle different cases: I use {{ $json.chatInput }} as the source, so you can type your commands directly in the chat and provide different paths. I have both a create and a drop outputs, for example.

MySQL Sub-Workflow in n8n

If I send a message containing the word “create” to the Chat Trigger, n8n will reach a MySQL node that executes an SQL query to actually create a table if it doesn’t exist yet. Same if I send “drop”, but deleting it. You may want to set table names in the chat or in the query itself like I do.

This way, in the main workflow that handle insertions I just need to change the table name from the automatically generated list, without modifying anything else: valid credentials are enough to refresh available tables in the select dropdown. Finally, you can select the involved nodes and choose Convert to sub-workflow.

You can only have one Chat Trigger per workflow, so maybe you need more than just a Switch. I have one to reach the proper sub-workflow, for example, and another one to choose the operation to perform right after. It’s up to you! These simple maintenance tools increased my productivity.


I know I didn’t include screenshots here: I promise I’ll do with the next tutorial. Maybe I will update this article as well. Anyway, n8n is really useful for DevOps—and not only for no-code automations. I hope you can benefit from this short guide, because I did.

EDIT: I added a screenshot of my own sub-workflow.

Top comments (0)