Skip to content

Commit

Permalink
Fix possible breaking change on json_extract_path for boolean values (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
leandrocp authored May 3, 2022
1 parent c22115b commit 65b1c65
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
5 changes: 4 additions & 1 deletion lib/ecto/adapters/postgres/connection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -710,7 +710,7 @@ if Code.ensure_loaded?(Postgrex) do
defp expr({:count, _, []}, _sources, _query), do: "count(*)"

defp expr({:==, _, [{:json_extract_path, _, [expr, path]} = left, right]}, sources, query)
when is_binary(right) or is_integer(right) do
when is_binary(right) or is_integer(right) or is_boolean(right) do
case Enum.split(path, -1) do
{path, [last]} when is_binary(last) ->
extracted = json_extract_path(expr, path, sources, query)
Expand Down Expand Up @@ -1354,6 +1354,9 @@ if Code.ensure_loaded?(Postgrex) do
Integer.to_string(value)
end

defp escape_json(true), do: ["true"]
defp escape_json(false), do: ["false"]

defp ecto_to_db({:array, t}), do: [ecto_to_db(t), ?[, ?]]
defp ecto_to_db(:id), do: "integer"
defp ecto_to_db(:identity), do: "bigint"
Expand Down
14 changes: 10 additions & 4 deletions test/ecto/adapters/postgres_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -607,16 +607,22 @@ defmodule Ecto.Adapters.PostgresTest do

test "optimized json_extract_path" do
query = Schema |> where([s], s.meta["id"] == 123) |> select(true) |> plan()
assert all(query) == ~s|SELECT TRUE FROM \"schema\" AS s0 WHERE ((s0."meta"@>'{"id": 123}'))|
assert all(query) == ~s|SELECT TRUE FROM "schema" AS s0 WHERE ((s0."meta"@>'{"id": 123}'))|

query = Schema |> where([s], s.meta["id"] == "123") |> select(true) |> plan()
assert all(query) == ~s|SELECT TRUE FROM \"schema\" AS s0 WHERE ((s0."meta"@>'{"id": "123"}'))|
assert all(query) == ~s|SELECT TRUE FROM "schema" AS s0 WHERE ((s0."meta"@>'{"id": "123"}'))|

query = Schema |> where([s], s.meta["tags"][0]["name"] == "123") |> select(true) |> plan()
assert all(query) == ~s|SELECT TRUE FROM \"schema\" AS s0 WHERE (((s0."meta"#>'{"tags",0}')@>'{"name": "123"}'))|
assert all(query) == ~s|SELECT TRUE FROM "schema" AS s0 WHERE (((s0."meta"#>'{"tags",0}')@>'{"name": "123"}'))|

query = Schema |> where([s], s.meta[0] == "123") |> select(true) |> plan()
assert all(query) == ~s|SELECT TRUE FROM \"schema\" AS s0 WHERE ((s0.\"meta\"#>'{0}') = '123')|
assert all(query) == ~s|SELECT TRUE FROM "schema" AS s0 WHERE ((s0.\"meta\"#>'{0}') = '123')|

query = Schema |> where([s], s.meta["enabled"] == true) |> select(true) |> plan()
assert all(query) == ~s|SELECT TRUE FROM "schema" AS s0 WHERE ((s0."meta"@>'{"enabled": true}'))|

query = Schema |> where([s], s.meta["extra"][0]["enabled"] == false) |> select(true) |> plan()
assert all(query) == ~s|SELECT TRUE FROM "schema" AS s0 WHERE (((s0."meta"#>'{"extra",0}')@>'{"enabled": false}'))|
end

test "nested expressions" do
Expand Down

0 comments on commit 65b1c65

Please sign in to comment.