Skip to content

Commit 35f3761

Browse files
author
Glen Holcomb
committed
Recurse query and apply filtering directly as appropriate
1 parent 36ebba3 commit 35f3761

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

lib/ecto/soft_delete_repo.ex

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,37 @@ defmodule Ecto.SoftDelete.Repo do
8181
if has_include_deleted_at_clause?(query) || opts[:with_deleted] || !soft_deletable?(query) do
8282
{query, opts}
8383
else
84-
query = from(x in query, where: is_nil(x.deleted_at))
84+
query = filter_soft_deleted(query)
8585
{query, opts}
8686
end
8787
end
8888

89+
# We need to check the entire query and apply filtering
90+
# where appropriate. So, we recurse the query here and
91+
# rebuild it with filtering where appropriate. This
92+
# currently only considers the source and does not handle
93+
# things like joins...
94+
defp filter_soft_deleted(%Ecto.Query{from: %{source: {_schema, _module}}} = query) do
95+
if Ecto.SoftDelete.Query.soft_deletable?(query) do
96+
from(x in query, where: is_nil(x.deleted_at))
97+
else
98+
query
99+
end
100+
end
101+
102+
defp filter_soft_deleted(%Ecto.SubQuery{query: query} = sub) do
103+
if Ecto.SoftDelete.Query.soft_deletable?(query) do
104+
from(x in query, where: is_nil(x.deleted_at)) |> subquery()
105+
else
106+
sub
107+
end
108+
end
109+
110+
defp filter_soft_deleted(%Ecto.Query{from: from} = query) do
111+
updated_from = %{from | source: filter_soft_deleted(from.source)}
112+
%{query | from: updated_from}
113+
end
114+
89115
# Checks the query to see if it contains a where not is_nil(deleted_at)
90116
# if it does, we want to be sure that we don't exclude soft deleted records
91117
defp has_include_deleted_at_clause?(%Ecto.Query{wheres: wheres}) do

0 commit comments

Comments
 (0)