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

diffs cardinality many through, inc tests #153

Merged
merged 9 commits into from
Oct 4, 2023
Merged
Show file tree
Hide file tree
Changes from 5 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
46 changes: 44 additions & 2 deletions lib/ecto_diff.ex
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,47 @@ defmodule EctoDiff do
end
end

defp diff_association(previous, current, %{cardinality: :many, field: field, related: struct}, acc, opts) do
defp diff_association(
previous,
current,
%{cardinality: :many, through: [through, assoc_field], owner: owner},
acc,
opts
) do
assoc_diff =
acc
|> Keyword.get_values(through)
|> List.flatten()
|> Enum.map(& &1[:changes][assoc_field])
leftstanding marked this conversation as resolved.
Show resolved Hide resolved

diff =
case assoc_diff do
[_ | _] = diff ->
List.flatten(diff)

_ ->
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ℹ️ This should just be an empty list:

Suggested change
_ ->
[] ->

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At the time, the return could also be [nil]. Changed the way we handle this.

Copy link

@luizmiranda7 luizmiranda7 Oct 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ℹ️ I don't quite understand what is expected value here... perhaps an empty list?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the through key is present, but the assoc_field key is not, the return could be [nil], so _ -> was to handle both [] || [nil]. I have changed how we handle this so it can expect only empty lists.

through_association = owner.__schema__(:association, through).related
association = through_association.__schema__(:association, assoc_field)

diff = diff_association(previous, current, association, acc, opts)

diff
|> Keyword.get_values(assoc_field)
|> List.flatten()
end

if no_changes?(diff), do: acc, else: [{assoc_field, diff} | acc]
end

defp diff_association(
previous,
current,
%{cardinality: :many, field: field} = association,
acc,
opts
) do
%{related: struct} = association

primary_key_fields = get_primary_key_fields(struct, opts)

if primary_key_fields == [],
Expand Down Expand Up @@ -428,7 +468,9 @@ defmodule EctoDiff do
keys
end

defp no_changes?(%{effect: :changed, changes: map}) when map == %{}, do: true
defp no_changes?([_ | _] = changes), do: Enum.any?(changes, &no_changes?/1)
defp no_changes?([]), do: true
defp no_changes?(%{effect: effect, changes: map}) when map == %{} and effect in [:added, :changed], do: true
leftstanding marked this conversation as resolved.
Show resolved Hide resolved
defp no_changes?(_), do: false

defp primary_key_nil?(key), do: Enum.all?(key, fn {_key, value} -> is_nil(value) end)
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ defmodule EctoDiff.MixProject do
{:credo, "~> 1.0", only: [:dev, :test], runtime: false},
{:dialyxir, "~> 1.1", only: [:dev, :test], runtime: false},
{:ecto_sql, "~> 3.8", only: [:dev, :test]},
{:ecto, "~> 3.8"},
{:ecto, "~> 3.10.3"},
leftstanding marked this conversation as resolved.
Show resolved Hide resolved
{:ex_doc, "~> 0.28", only: :dev, runtime: false},
{:excoveralls, "~> 0.14", only: :test},
{:jason, ">= 1.0.0", only: [:dev, :test]},
Expand Down
18 changes: 18 additions & 0 deletions priv/repo/migrations/20230915120413_create_resources_toys.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
defmodule EctoDiff.Repo.Migrations.CreateResources do
use Ecto.Migration

def change do
create table(:resources) do
add :pet_id, references(:pets)
add :refid, :uuid
end

create table(:toys) do
add :name, :string
add :type, :string
add :quantity, :integer, default: 1
add :resource_id, references(:resources)
add :refid, :uuid
end
end
end
Loading
Loading