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:
@@ -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,
|
||||
trigger_type: b.trigger_type, trigger_config: b.trigger_config ? JSON.stringify(b.trigger_config) : null,
|
||||
created_by: req.admin?.id || null,
|
||||
});
|
||||
const newId = ins[0];
|
||||
}).returning('id');
|
||||
// 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);
|
||||
return newId;
|
||||
});
|
||||
|
||||
@@ -127,8 +127,12 @@ async function seedBuiltinWorkflowsAtBoot(db, logger) {
|
||||
trigger_config: JSON.stringify({ seedVersion: SEED_VERSION }),
|
||||
is_builtin: true,
|
||||
builtin_key: DUNNING_KEY,
|
||||
});
|
||||
await writeGraph(trx, ins[0], 1, nodes, edges);
|
||||
}).returning('id');
|
||||
// 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;
|
||||
|
||||
Reference in New Issue
Block a user