fix(workflows): Postgres-safe id capture on workflow inserts

On Postgres, knex .insert() without .returning() resolves to [], so ins[0]
was undefined → the child workflow_nodes inserts hit a NOT NULL violation and
the whole transaction rolled back. Result on PG: migration + tables present
but zero rows — the seeded dunning flow never persisted, and the 'New
workflow' button would 500. SQLite returns the row id, so the test harness
masked it.

Add .returning('id') and normalise the {id} (pg) vs bare-id (sqlite) shapes
(same pattern as the crmDb harness) in both the built-in seed and the admin
create route. Tests stay green on SQLite (17).
This commit is contained in:
Luca
2026-06-23 11:33:14 +02:00
parent 5259ee9705
commit cede885b04
2 changed files with 11 additions and 4 deletions
+5 -2
View File
@@ -131,8 +131,11 @@ router.post('/', requirePermission('workflows.manage'), async (req, res, next) =
name: b.name, description: b.description || null, enabled: !!b.enabled, version: 1, name: b.name, description: b.description || null, enabled: !!b.enabled, version: 1,
trigger_type: b.trigger_type, trigger_config: b.trigger_config ? JSON.stringify(b.trigger_config) : null, trigger_type: b.trigger_type, trigger_config: b.trigger_config ? JSON.stringify(b.trigger_config) : null,
created_by: req.admin?.id || null, created_by: req.admin?.id || null,
}); }).returning('id');
const newId = ins[0]; // Postgres returns [] without an explicit returning clause, so ins[0]
// would be undefined → the child node inserts would violate NOT NULL.
// Normalise the {id} (pg) vs bare id (sqlite) shapes.
const newId = ins[0]?.id ?? ins[0];
await writeGraph(trx, newId, 1, b.nodes, b.edges); await writeGraph(trx, newId, 1, b.nodes, b.edges);
return newId; return newId;
}); });
+6 -2
View File
@@ -127,8 +127,12 @@ async function seedBuiltinWorkflowsAtBoot(db, logger) {
trigger_config: JSON.stringify({ seedVersion: SEED_VERSION }), trigger_config: JSON.stringify({ seedVersion: SEED_VERSION }),
is_builtin: true, is_builtin: true,
builtin_key: DUNNING_KEY, builtin_key: DUNNING_KEY,
}); }).returning('id');
await writeGraph(trx, ins[0], 1, nodes, edges); // Postgres returns [] without `.returning`, so ins[0] would be undefined
// and the child node inserts would roll back on NOT NULL. Normalise the
// {id} (pg) vs bare-id (sqlite) shapes.
const workflowId = ins[0]?.id ?? ins[0];
await writeGraph(trx, workflowId, 1, nodes, edges);
}); });
booted = true; booted = true;