diff --git a/config/test.exs b/config/test.exs index 7e558e0..057cc36 100644 --- a/config/test.exs +++ b/config/test.exs @@ -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 diff --git a/lib/scrivener/paginater/ecto/query.ex b/lib/scrivener/paginater/ecto/query.ex index c3f9aac..abbb2fb 100644 --- a/lib/scrivener/paginater/ecto/query.ex +++ b/lib/scrivener/paginater/ecto/query.ex @@ -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) @@ -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( @@ -62,8 +60,7 @@ defimpl Scrivener.Paginater, for: Ecto.Query do ] } = query, repo, - caller, - prefix + options ) do query |> exclude(:preload) @@ -72,15 +69,11 @@ 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 @@ -88,20 +81,4 @@ defimpl Scrivener.Paginater, for: Ecto.Query do 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 diff --git a/test/scrivener/paginator/ecto/query_test.exs b/test/scrivener/paginator/ecto/query_test.exs index b1f9ddc..671eecf 100644 --- a/test/scrivener/paginator/ecto/query_test.exs +++ b/test/scrivener/paginator/ecto/query_test.exs @@ -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 @@ -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