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

Fix duplicate ID check for many assocs/embeds #4237

Merged
merged 2 commits into from
Jul 20, 2023
Merged
Show file tree
Hide file tree
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
27 changes: 20 additions & 7 deletions lib/ecto/changeset/relation.ex
Original file line number Diff line number Diff line change
Expand Up @@ -308,9 +308,10 @@ defmodule Ecto.Changeset.Relation do
defp cast_or_change(%{cardinality: :many} = relation, value, current, current_pks_fun, new_pks_fun, fun)
when is_list(value) do
{current_pks, current_map} = process_current(current, current_pks_fun, relation)
%{unique: unique, ordered: ordered} = relation
%{unique: unique, ordered: ordered, related: mod} = relation
change_pks_fun = change_pk(mod.__schema__(:primary_key))
ordered = if ordered, do: current_pks, else: []
map_changes(value, new_pks_fun, fun, current_map, [], true, true, unique && %{}, 0, ordered)
map_changes(value, new_pks_fun, change_pks_fun, fun, current_map, [], true, true, unique && %{}, 0, ordered)
end

defp cast_or_change(_, _, _, _, _, _), do: :error
Expand Down Expand Up @@ -356,35 +357,36 @@ defmodule Ecto.Changeset.Relation do

# map changes

defp map_changes([changes | rest], new_pks, fun, current, acc, valid?, skip?, unique, idx, ordered)
defp map_changes([changes | rest], new_pks, change_pks, fun, current, acc, valid?, skip?, unique, idx, ordered)
when is_map(changes) or is_list(changes) do
pk_values = new_pks.(changes)
{struct, current, allowed_actions} = pop_current(current, pk_values)

case fun.(changes, struct, allowed_actions, idx) do
{:ok, %{action: :ignore}} ->
ordered = pop_ordered(pk_values, ordered)
map_changes(rest, new_pks, fun, current, acc, valid?, skip?, unique, idx + 1, ordered)
map_changes(rest, new_pks, change_pks, fun, current, acc, valid?, skip?, unique, idx + 1, ordered)
{:ok, changeset} ->
pk_values = change_pks.(changeset)
changeset = maybe_add_error_on_pk(changeset, pk_values, unique)
acc = [changeset | acc]
valid? = valid? and changeset.valid?
skip? = (struct != nil) and skip? and skip?(changeset)
unique = unique && Map.put(unique, pk_values, true)
ordered = pop_ordered(pk_values, ordered)
map_changes(rest, new_pks, fun, current, acc, valid?, skip?, unique, idx + 1, ordered)
map_changes(rest, new_pks, change_pks, fun, current, acc, valid?, skip?, unique, idx + 1, ordered)
:error ->
:error
end
end

defp map_changes([], _new_pks, fun, current, acc, valid?, skip?, _unique, _idx, ordered) do
defp map_changes([], _new_pks, _change_pks, fun, current, acc, valid?, skip?, _unique, _idx, ordered) do
current_structs = Enum.map(current, &elem(&1, 1))
skip? = skip? and ordered == []
reduce_delete_changesets(current_structs, fun, Enum.reverse(acc), valid?, skip?)
end

defp map_changes(_params, _new_pks, _fun, _current, _acc, _valid?, _skip?, _unique, _idx, _ordered) do
defp map_changes(_params, _new_pks, _change_pks, _fun, _current, _acc, _valid?, _skip?, _unique, _idx, _ordered) do
:error
end

Expand Down Expand Up @@ -510,6 +512,17 @@ defmodule Ecto.Changeset.Relation do
end
end

defp change_pk(pks) do
fn %Changeset{} = cs ->
Enum.map(pks, fn pk ->
case cs.changes do
%{^pk => pk_value} -> pk_value
_ -> Map.get(cs.data, pk)
end
end)
end
end
greg-rychlewski marked this conversation as resolved.
Show resolved Hide resolved

defp put_new_action(%{action: action} = changeset, new_action) when is_nil(action),
do: Map.put(changeset, :action, new_action)
defp put_new_action(changeset, _new_action),
Expand Down
2 changes: 1 addition & 1 deletion test/ecto/repo/embedded_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ defmodule Ecto.Repo.EmbeddedTest do

test "duplicate pk on insert" do
embeds = [%MyEmbed{x: "xyz", id: @uuid} |> Ecto.Changeset.change,
%MyEmbed{x: "abc", id: @uuid} |> Ecto.Changeset.change]
%MyEmbed{} |> Ecto.Changeset.change(%{x: "abc", id: @uuid})]
changeset =
%MySchema{}
|> Ecto.Changeset.change
Expand Down
2 changes: 1 addition & 1 deletion test/ecto/repo/has_assoc_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ defmodule Ecto.Repo.HasAssocTest do

test "duplicate pk on insert" do
assocs = [%MyAssoc{x: "xyz", id: 1} |> Ecto.Changeset.change,
%MyAssoc{x: "abc", id: 1} |> Ecto.Changeset.change]
%MyAssoc{} |> Ecto.Changeset.change(%{x: "abc", id: 1})]
changeset =
%MySchema{}
|> Ecto.Changeset.change
Expand Down