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

Warn when applying ordering to a query with pre-existing order #524

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 5 additions & 0 deletions lib/flop/adapter/ecto.ex
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,11 @@ defmodule Flop.Adapter.Ecto do

@impl Flop.Adapter
def apply_order_by(query, directions, opts) do
if Ecto.Queryable.to_query(query).order_bys != [] do
Logger.warning("The query you passed to flop includes order_by.\
This may interfere with Flop's ordering and pagination features.")
end

case opts[:for] do
nil ->
Query.order_by(query, ^directions)
Expand Down
10 changes: 10 additions & 0 deletions test/adapters/ecto/cases/flop_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ defmodule Flop.Adapters.Ecto.FlopTest do

doctest Flop, import: true

import ExUnit.CaptureLog
import Ecto.Query
import Flop.Factory
import Flop.Generators
Expand Down Expand Up @@ -167,6 +168,15 @@ defmodule Flop.Adapters.Ecto.FlopTest do
for: Owner
) == Enum.reverse(expected)
end

test "warns if query passed to Flop already included ordering" do
query = from p in Pet, order_by: :species

assert capture_log(fn ->
Flop.all(query, %Flop{order_by: [:species, :name]})
end) =~
"This may interfere with Flop's ordering and pagination features"
end
end

describe "filtering" do
Expand Down