Skip to content

Commit 735320d

Browse files
authored
Started batch inserting automation action edges (TryGhost#28521)
towards https://linear.app/ghost/issue/NY-1340 `replaceAutomationGraph` makes a number of trips to the database when it doesn't need to. We should fix this for performance. This fixes one of those spots: inserting a batch of action edges, instead of doing them one-by-one. We'll do more [soon][0]. [0]: https://linear.app/ghost/issue/NY-1340
1 parent 22812c1 commit 735320d

1 file changed

Lines changed: 7 additions & 6 deletions

File tree

ghost/core/core/server/services/automations/fake-database-automations-repository.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -658,9 +658,7 @@ async function replaceAutomationGraph(trx: Knex.Transaction, automationId: strin
658658

659659
await deleteAutomationEdges(trx, automationId);
660660

661-
for (const edge of edges) {
662-
await insertActionEdge(trx, edge);
663-
}
661+
await insertActionEdges(trx, edges);
664662
}
665663

666664
async function loadAutomationActionRows(trx: Knex.Transaction, automationId: string): Promise<Array<Pick<ActionRow, 'id' | 'type'>>> {
@@ -812,11 +810,14 @@ async function deleteAutomationEdges(trx: Knex.Transaction, automationId: string
812810
.where('automation_id', automationId));
813811
}
814812

815-
async function insertActionEdge(trx: Knex.Transaction, edge: Readonly<AutomationEdge>): Promise<void> {
816-
await trx('automation_action_edges').insert({
813+
async function insertActionEdges(trx: Knex.Transaction, edges: ReadonlyArray<AutomationEdge>): Promise<void> {
814+
if (edges.length === 0) {
815+
return;
816+
}
817+
await trx('automation_action_edges').insert(edges.map(edge => ({
817818
source_action_id: edge.source_action_id,
818819
target_action_id: edge.target_action_id
819-
});
820+
})));
820821
}
821822

822823
function requireAutomation(automation: AutomationRow | null, id: string): AutomationRow {

0 commit comments

Comments
 (0)