Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUGFIX] [MER-2676] Section creation from product fails when product exceeds certain size #4328

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion lib/oli/delivery/sections.ex
Original file line number Diff line number Diff line change
Expand Up @@ -884,7 +884,26 @@ defmodule Oli.Delivery.Sections do
def bulk_create_section_resource([], _opts), do: {0, []}

def bulk_create_section_resource(section_resource_rows, opts) do
Repo.insert_all(SectionResource, section_resource_rows, returning: opts[:returning] || true)
section_resource_rows
|> Enum.chunk_every(calculate_chunk_size(section_resource_rows))
|> Enum.reduce({0, []}, fn chunk, {total, acc} ->
{new_total, new_acc} = Repo.insert_all(SectionResource, chunk, returning: opts[:returning] || true)
{total + new_total, acc ++ new_acc}
end)
end

defp calculate_chunk_size(section_resource_rows) do
# We want to split the list of section resources into chunks
# to avoid hitting the max number of bind variables in a query.
max_bind_variables = 65535

fields_count =
section_resource_rows
|> List.first()
|> Map.keys()
|> length()

div(max_bind_variables, fields_count)
end

@doc """
Expand Down