Skip to content

Commit 732a44c

Browse files
ImTotemclaude
andcommitted
feat(workflow): create missing sheets + skip empty tables
- Add Ensure Sheet Exists node (onError: continue if already exists) - Move Dump before Clear — skip Clear+Write for empty tables - Add Has Data? conditional to avoid unnecessary API calls Flow: Loop → Create Sheet → Dump → Has Data? → Clear → Write → Loop ↘ (no data) → Loop Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 40ba880 commit 732a44c

1 file changed

Lines changed: 44 additions & 11 deletions

File tree

workflows/pg_sheets_sync.json

Lines changed: 44 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@
2525
"name": "List PG Tables",
2626
"type": "n8n-nodes-base.postgres",
2727
"typeVersion": 2.6,
28-
"position": [220, 0],
29-
"onError": "continueRegularOutput"
28+
"position": [220, 0]
3029
},
3130
{
3231
"parameters": {"options": {}},
@@ -36,18 +35,52 @@
3635
"typeVersion": 3,
3736
"position": [440, 0]
3837
},
38+
{
39+
"parameters": {
40+
"operation": "create",
41+
"documentId": {"__rl": true, "mode": "id", "value": "={{ $env.GOOGLE_SHEETS_ID }}"},
42+
"title": "={{ $json.table_name }}"
43+
},
44+
"id": "sheets-create",
45+
"name": "Ensure Sheet Exists",
46+
"type": "n8n-nodes-base.googleSheets",
47+
"typeVersion": 4.7,
48+
"position": [660, 0],
49+
"onError": "continueRegularOutput"
50+
},
3951
{
4052
"parameters": {
4153
"operation": "executeQuery",
42-
"query": "=SELECT * FROM {{ $json.table_name }}",
54+
"query": "=SELECT * FROM {{ $('Loop Over Tables').item.json.table_name }}",
4355
"options": {}
4456
},
4557
"id": "pg-dump-table",
4658
"name": "Dump Table",
4759
"type": "n8n-nodes-base.postgres",
4860
"typeVersion": 2.6,
49-
"position": [660, 0],
50-
"onError": "continueRegularOutput"
61+
"position": [880, 0]
62+
},
63+
{
64+
"parameters": {
65+
"conditions": {
66+
"options": {"caseSensitive": true, "leftValue": "", "typeValidation": "strict"},
67+
"conditions": [
68+
{
69+
"id": "has-rows",
70+
"leftValue": "={{ $input.all().length }}",
71+
"rightValue": 0,
72+
"operator": {"type": "number", "operation": "gt"}
73+
}
74+
],
75+
"combinator": "and"
76+
},
77+
"options": {}
78+
},
79+
"id": "if-has-data",
80+
"name": "Has Data?",
81+
"type": "n8n-nodes-base.if",
82+
"typeVersion": 2.3,
83+
"position": [1100, 0]
5184
},
5285
{
5386
"parameters": {
@@ -59,8 +92,7 @@
5992
"name": "Clear Sheet",
6093
"type": "n8n-nodes-base.googleSheets",
6194
"typeVersion": 4.7,
62-
"position": [880, 0],
63-
"onError": "continueRegularOutput"
95+
"position": [1320, -100]
6496
},
6597
{
6698
"parameters": {
@@ -77,15 +109,16 @@
77109
"name": "Write to Sheet",
78110
"type": "n8n-nodes-base.googleSheets",
79111
"typeVersion": 4.7,
80-
"position": [1100, 0],
81-
"onError": "continueRegularOutput"
112+
"position": [1540, -100]
82113
}
83114
],
84115
"connections": {
85116
"Every 5 Minutes": {"main": [[{"node": "List PG Tables", "type": "main", "index": 0}]]},
86117
"List PG Tables": {"main": [[{"node": "Loop Over Tables", "type": "main", "index": 0}]]},
87-
"Loop Over Tables": {"main": [[], [{"node": "Dump Table", "type": "main", "index": 0}]]},
88-
"Dump Table": {"main": [[{"node": "Clear Sheet", "type": "main", "index": 0}]]},
118+
"Loop Over Tables": {"main": [[], [{"node": "Ensure Sheet Exists", "type": "main", "index": 0}]]},
119+
"Ensure Sheet Exists": {"main": [[{"node": "Dump Table", "type": "main", "index": 0}]]},
120+
"Dump Table": {"main": [[{"node": "Has Data?", "type": "main", "index": 0}]]},
121+
"Has Data?": {"main": [[{"node": "Clear Sheet", "type": "main", "index": 0}], [{"node": "Loop Over Tables", "type": "main", "index": 0}]]},
89122
"Clear Sheet": {"main": [[{"node": "Write to Sheet", "type": "main", "index": 0}]]},
90123
"Write to Sheet": {"main": [[{"node": "Loop Over Tables", "type": "main", "index": 0}]]}
91124
},

0 commit comments

Comments
 (0)