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

Fix binary uuid casting #121

Merged
merged 3 commits into from
Aug 29, 2023
Merged
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
20 changes: 20 additions & 0 deletions lib/ecto/adapters/sqlite3/connection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1431,6 +1431,26 @@ defmodule Ecto.Adapters.SQLite3.Connection do
[?x, ?', hex, ?']
end

def expr(%Ecto.Query.Tagged{value: expr, type: :binary_id}, sources, query) do
case Application.get_env(:ecto_sqlite3, :binary_id_type, :string) do
:string ->
["CAST(", expr(expr, sources, query), " AS ", column_type(:string, query), ?)]

:binary ->
[expr(expr, sources, query)]
end
end

def expr(%Ecto.Query.Tagged{value: expr, type: :uuid}, sources, query) do
case Application.get_env(:ecto_sqlite3, :uuid_type, :string) do
:string ->
["CAST(", expr(expr, sources, query), " AS ", column_type(:string, query), ?)]

:binary ->
[expr(expr, sources, query)]
end
end

def expr(%Ecto.Query.Tagged{value: other, type: type}, sources, query)
when type in [:decimal, :float] do
["CAST(", expr(other, sources, query), " AS REAL)"]
Expand Down
38 changes: 38 additions & 0 deletions test/ecto/integration/uuid_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ defmodule Ecto.Integration.UUIDTest do
alias Ecto.Integration.TestRepo
alias EctoSQLite3.Schemas.Product

import Ecto.Query, only: [from: 2]

setup do
Application.put_env(:ecto_sqlite3, :uuid_type, :string)
on_exit(fn -> Application.put_env(:ecto_sqlite3, :uuid_type, :string) end)
Expand Down Expand Up @@ -34,4 +36,40 @@ defmodule Ecto.Integration.UUIDTest do
assert found
assert found.external_id == external_id
end

test "handles uuid casting with binary format" do
Application.put_env(:ecto_sqlite3, :uuid_type, :binary)
Application.put_env(:ecto_sqlite3, :binary_id_type, :binary)

external_id = Ecto.UUID.generate()
TestRepo.insert!(%Product{external_id: external_id, bid: external_id})

product =
TestRepo.one(from(p in Product, where: p.external_id == type(p.bid, Ecto.UUID)))

assert %{external_id: ^external_id} = product

product =
TestRepo.one(
from(p in Product, where: p.external_id == type(^external_id, Ecto.UUID))
)

assert %{external_id: ^external_id} = product
end

test "handles binary_id casting with binary format" do
Application.put_env(:ecto_sqlite3, :uuid_type, :binary)
Application.put_env(:ecto_sqlite3, :binary_id_type, :binary)

bid = Ecto.UUID.generate()
TestRepo.insert!(%Product{bid: bid, external_id: bid})

product =
TestRepo.one(from(p in Product, where: p.bid == type(p.external_id, :binary_id)))

assert %{bid: ^bid} = product

product = TestRepo.one(from(p in Product, where: p.bid == type(^bid, :binary_id)))
assert %{bid: ^bid} = product
end
end
1 change: 1 addition & 0 deletions test/support/migration.ex
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ defmodule EctoSQLite3.Integration.Migration do
add(:name, :string)
add(:description, :text)
add(:external_id, :uuid)
add(:bid, :binary_id)
add(:tags, {:array, :string})
add(:approved_at, :naive_datetime)
add(:price, :decimal)
Expand Down
1 change: 1 addition & 0 deletions test/support/schemas/product.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ defmodule EctoSQLite3.Schemas.Product do
field(:name, :string)
field(:description, :string)
field(:external_id, Ecto.UUID)
field(:bid, :binary_id)
field(:tags, {:array, :string}, default: [])
field(:approved_at, :naive_datetime)
field(:price, :decimal)
Expand Down
Loading