From f80288dacf6408ef0bd1a2dddf192d9677845e4d Mon Sep 17 00:00:00 2001 From: Darren Siegel Date: Fri, 17 Nov 2023 12:44:38 -0500 Subject: [PATCH] handle returning values correctly --- lib/oli/utils/database.ex | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/lib/oli/utils/database.ex b/lib/oli/utils/database.ex index bbfa8971ccc..91033202b8e 100644 --- a/lib/oli/utils/database.ex +++ b/lib/oli/utils/database.ex @@ -228,10 +228,21 @@ defmodule Oli.Utils.Database do def batch_insert_all(schema, rows, opts) do rows |> Enum.chunk_every(calculate_chunk_size(rows)) - |> Enum.reduce({0, []}, fn chunk, {total, acc} -> + |> Enum.reduce({0, nil}, fn chunk, {total, acc} -> {new_total, new_acc} = Repo.insert_all(schema, chunk, opts) - {total + new_total, acc ++ new_acc} + # safely combine the results of the "returning" portion + # of insert_all. For queries that return something, these will + # be lists, but for queries that do not return anything, these + # are nil + acc = case {acc, new_acc} do + {nil, nil} -> nil + {nil, list} when is_list(list) -> list + {list, nil} when is_list(list) -> list + {list1, list2} when is_list(list1) and is_list(list2) -> list1 ++ list2 + end + + {total + new_total, acc} end) end end