Skip to content

Commit

Permalink
Revert "Add support for clauses with nil values in Repo.get_by(!)/2 (
Browse files Browse the repository at this point in the history
…#2125)"

This reverts commit 8e1b378.
  • Loading branch information
José Valim committed Aug 27, 2017
1 parent f8f3c1f commit db55b0c
Show file tree
Hide file tree
Showing 3 changed files with 1 addition and 10 deletions.
3 changes: 0 additions & 3 deletions integration_test/cases/repo.exs
Original file line number Diff line number Diff line change
Expand Up @@ -601,21 +601,18 @@ defmodule Ecto.Integration.RepoTest do
test "get_by(!)" do
post1 = TestRepo.insert!(%Post{title: "1", text: "hai"})
post2 = TestRepo.insert!(%Post{title: "2", text: "hello"})
post3 = TestRepo.insert!(%Post{title: "3", text: nil})

assert post1 == TestRepo.get_by(Post, id: post1.id)
assert post1 == TestRepo.get_by(Post, text: post1.text)
assert post1 == TestRepo.get_by(Post, id: post1.id, text: post1.text)
assert post2 == TestRepo.get_by(Post, id: to_string(post2.id)) # With casting
assert nil == TestRepo.get_by(Post, text: "hey")
assert nil == TestRepo.get_by(Post, id: post2.id, text: "hey")
assert post3 == TestRepo.get_by(Post, text: nil)

assert post1 == TestRepo.get_by!(Post, id: post1.id)
assert post1 == TestRepo.get_by!(Post, text: post1.text)
assert post1 == TestRepo.get_by!(Post, id: post1.id, text: post1.text)
assert post2 == TestRepo.get_by!(Post, id: to_string(post2.id)) # With casting
assert post3 == TestRepo.get_by!(Post, text: nil)

assert post1 == TestRepo.get_by!(Post, %{id: post1.id})

Expand Down
7 changes: 1 addition & 6 deletions lib/ecto/repo/queryable.ex
Original file line number Diff line number Diff line change
Expand Up @@ -347,12 +347,7 @@ defmodule Ecto.Repo.Queryable do
end

defp query_for_get_by(_repo, queryable, clauses) do
Enum.reduce(clauses, queryable, fn
({key, nil}, query) ->
Query.where(query, [x], is_nil(field(x, ^key)))
({key, value}, query) ->
Query.where(query, [x], field(x, ^key) == ^value)
end)
Query.where(queryable, [], ^Enum.to_list(clauses))
end

defp query_for_aggregate(queryable, aggregate, field) do
Expand Down
1 change: 0 additions & 1 deletion test/ecto/repo_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@ defmodule Ecto.RepoTest do
test "validates get_by" do
TestRepo.get_by(MySchema, id: 123)
TestRepo.get_by(MySchema, %{id: 123})
TestRepo.get_by(MySchema, id: nil)

message = ~r"value `:atom` in `where` cannot be cast to type :id in query"
assert_raise Ecto.Query.CastError, message, fn ->
Expand Down

0 comments on commit db55b0c

Please # to comment.