Skip to content

Commit

Permalink
fix: pass the options to the repo calls
Browse files Browse the repository at this point in the history
  • Loading branch information
yordis authored and cpjolicoeur committed Oct 17, 2024
1 parent c7edcc1 commit b0b5dbc
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 38 deletions.
3 changes: 2 additions & 1 deletion config/test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ config :scrivener_ecto, Scrivener.Ecto.Repo,
adapter: Ecto.Adapters.Postgres,
pool: Ecto.Adapters.SQL.Sandbox,
database: "scrivener_test",
username: System.get_env("SCRIVENER_ECTO_DB_USER") || "postgres"
username: System.get_env("SCRIVENER_ECTO_DB_USER") || "postgres",
password: System.get_env("SCRIVENER_ECTO_DB_PASSWORD") || "postgres"

config :logger, :console, level: :error
51 changes: 14 additions & 37 deletions lib/scrivener/paginater/ecto/query.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ defimpl Scrivener.Paginater, for: Ecto.Query do
options: options
}) do
total_entries =
Keyword.get_lazy(options, :total_entries, fn ->
total_entries(query, repo, caller, options)
options
|> Keyword.put_new(:caller, caller)
|> Keyword.get_lazy(:total_entries, fn ->
aggregate(query, repo, options)
end)

total_pages = total_pages(total_entries, page_size)
Expand All @@ -27,27 +29,23 @@ defimpl Scrivener.Paginater, for: Ecto.Query do
%Page{
page_size: page_size,
page_number: page_number,
entries: entries(query, repo, page_number, total_pages, page_size, caller, options),
entries: entries(query, repo, page_number, total_pages, page_size, options),
total_entries: total_entries,
total_pages: total_pages
}
end

defp entries(_, _, page_number, total_pages, _, _, _) when page_number > total_pages, do: []
defp entries(_query, _repo, page_number, total_pages, _page_size, _options)
when page_number > total_pages,
do: []

defp entries(query, repo, page_number, _, page_size, caller, options) do
defp entries(query, repo, page_number, _total_pages, page_size, options) do
offset = Keyword.get_lazy(options, :offset, fn -> page_size * (page_number - 1) end)
prefix = options[:prefix]

query
|> offset(^offset)
|> limit(^page_size)
|> all(repo, caller, prefix)
end

defp total_entries(query, repo, caller, options) do
prefix = options[:prefix]
aggregate(query, repo, caller, prefix)
|> repo.all(options)
end

defp aggregate(
Expand All @@ -62,8 +60,7 @@ defimpl Scrivener.Paginater, for: Ecto.Query do
]
} = query,
repo,
caller,
prefix
options
) do
query
|> exclude(:preload)
Expand All @@ -72,36 +69,16 @@ defimpl Scrivener.Paginater, for: Ecto.Query do
|> select([{x, source_index}], struct(x, ^[field]))
|> subquery()
|> select(count("*"))
|> one(repo, caller, prefix)
end

defp aggregate(query, repo, caller, nil) do
repo.aggregate(query, :count, caller: caller)
|> repo.one(options)
end

defp aggregate(query, repo, caller, prefix) do
repo.aggregate(query, :count, caller: caller, prefix: prefix)
defp aggregate(query, repo, options) do
repo.aggregate(query, :count, options)
end

defp total_pages(0, _), do: 1

defp total_pages(total_entries, page_size) do
(total_entries / page_size) |> Float.ceil() |> round
end

defp all(query, repo, caller, nil) do
repo.all(query, caller: caller)
end

defp all(query, repo, caller, prefix) do
repo.all(query, caller: caller, prefix: prefix)
end

defp one(query, repo, caller, nil) do
repo.one(query, caller: caller)
end

defp one(query, repo, caller, prefix) do
repo.one(query, caller: caller, prefix: prefix)
end
end
12 changes: 12 additions & 0 deletions test/scrivener/paginator/ecto/query_test.exs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
defmodule Scrivener.Paginator.Ecto.QueryTest do
use Scrivener.Ecto.TestCase

import ExUnit.CaptureLog

alias Scrivener.Ecto.{Comment, KeyValue, Post, User}

defp create_posts do
Expand Down Expand Up @@ -458,4 +460,14 @@ defmodule Scrivener.Paginator.Ecto.QueryTest do
assert page_tenant_2.total_pages == 1
end
end

test "accepts repo options" do
log = capture_log(fn -> Scrivener.Ecto.Repo.paginate(Post, options: [log: true]) end)

assert log =~
"SELECT p0.\"id\", p0.\"title\", p0.\"body\", p0.\"published\", p0.\"inserted_at\", p0.\"updated_at\" FROM \"posts\" AS p0 LIMIT $1 OFFSET $2"

log = capture_log(fn -> Scrivener.Ecto.Repo.paginate(Post, options: [log: false]) end)
assert log == ""
end
end

0 comments on commit b0b5dbc

Please sign in to comment.