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: Erroneous where clause on subquery w/ un-selected 'deleted_at' #185 #186

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ erl_crash.dump
*.ez

/.elixir_ls
/.lexical
28 changes: 27 additions & 1 deletion lib/ecto/soft_delete_repo.ex
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,37 @@ defmodule Ecto.SoftDelete.Repo do
if has_include_deleted_at_clause?(query) || opts[:with_deleted] || !soft_deletable?(query) do
{query, opts}
else
query = from(x in query, where: is_nil(x.deleted_at))
query = filter_soft_deleted(query)
{query, opts}
end
end

# We need to check the entire query and apply filtering
# where appropriate. So, we recurse the query here and
# rebuild it with filtering where appropriate. This
# currently only considers the source and does not handle
# things like joins...
defp filter_soft_deleted(%Ecto.Query{from: %{source: {_schema, _module}}} = query) do
if Ecto.SoftDelete.Query.soft_deletable?(query) do
from(x in query, where: is_nil(x.deleted_at))
else
query
end
end

defp filter_soft_deleted(%Ecto.SubQuery{query: query} = sub) do
if Ecto.SoftDelete.Query.soft_deletable?(query) do
%{sub | query: from(x in query, where: is_nil(x.deleted_at))}
else
sub
end
end

defp filter_soft_deleted(%Ecto.Query{from: from} = query) do
updated_from = %{from | source: filter_soft_deleted(from.source)}
%{query | from: updated_from}
end

# Checks the query to see if it contains a where not is_nil(deleted_at)
# if it does, we want to be sure that we don't exclude soft deleted records
defp has_include_deleted_at_clause?(%Ecto.Query{wheres: wheres}) do
Expand Down
10 changes: 10 additions & 0 deletions test/soft_delete_repo_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,16 @@ defmodule Ecto.SoftDelete.Repo.Test do
assert Enum.member?(results, soft_deleted_user)
end

test "handles subquery that does not expose deleted_at as 'main' schema" do
Repo.insert!(%User{email: "[email protected]"})
Repo.insert!(%Nondeletable{value: "stuff"})

subq = User |> where([u], u.email == "[email protected]") |> select([u], %{id: u.id, email: u.email})
query = subquery(subq) |> join(:left, [u], nondel in Nondeletable, on: u.id == nondel.id)

Repo.all(query)
end

test "includes soft deleted records if where not is_nil(deleted_at) clause is present" do
user = Repo.insert!(%User{email: "[email protected]"})

Expand Down
Loading