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
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Recurse query and apply filtering directly as appropriate
Glen Holcomb committed Oct 30, 2024
commit 35f376197f961a14e6154b27d35456193700b03d
28 changes: 27 additions & 1 deletion lib/ecto/soft_delete_repo.ex
Original file line number Diff line number Diff line change
@@ -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
from(x in query, where: is_nil(x.deleted_at)) |> subquery()
LeakyBucket marked this conversation as resolved.
Show resolved Hide resolved
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