Skip to content

Commit

Permalink
Hotfix/absolute distinct fix (mojotech#80)
Browse files Browse the repository at this point in the history
* Add failing test for distinct: true with join

* Subquery for counts of distinct: true queries. Fixes mojotech#79

* Be more explicit about checking for truthy distinct value

* Fix formatting
  • Loading branch information
dawner authored May 20, 2020
1 parent ab25a7e commit 2835a67
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/scrivener/paginater/ecto/query.ex
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ defimpl Scrivener.Paginater, for: Ecto.Query do
total_entries || 0
end

defp aggregate(%{distinct: %{expr: [_ | _]}} = query) do
defp aggregate(%{distinct: %{expr: expr}} = query) when expr == true or is_list(expr) do
query
|> exclude(:select)
|> count()
Expand Down
16 changes: 16 additions & 0 deletions test/scrivener/paginator/ecto/query_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -403,5 +403,21 @@ defmodule Scrivener.Paginator.Ecto.QueryTest do
assert page.total_entries == 7
assert page.total_pages == 2
end

test "pagination plays nice with absolute distinct on a join query" do
create_posts()

page =
Post
|> distinct(true)
|> join(:inner, [p], c in assoc(p, :comments))
|> Scrivener.Ecto.Repo.paginate()

assert length(page.entries) == 1
assert page.page_size == 5
assert page.page_number == 1
assert page.total_entries == 1
assert page.total_pages == 1
end
end
end

0 comments on commit 2835a67

Please sign in to comment.