Skip to content

Commit

Permalink
fix: myxql type cast of :integer should be signed integer (instead of…
Browse files Browse the repository at this point in the history
… unsigned)
  • Loading branch information
lejoko committed May 20, 2024
1 parent 1ced585 commit 90da6e2
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 5 deletions.
5 changes: 5 additions & 0 deletions integration_test/sql/sql.exs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ defmodule Ecto.Integration.SQLTest do
assert [123] = TestRepo.all(from p in "posts", select: type(fragment("visits"), :integer))
end

test "type casting negative integers" do
TestRepo.insert!(%Post{visits: -42})
assert [-42] = TestRepo.all(from(p in Post, select: type(p.visits, :integer)))
end

@tag :array_type
test "fragment array types" do
text1 = "foo"
Expand Down
2 changes: 1 addition & 1 deletion lib/ecto/adapters/myxql/connection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1445,7 +1445,7 @@ if Code.ensure_loaded?(MyXQL) do
end

defp ecto_cast_to_db(:id, _query), do: "unsigned"
defp ecto_cast_to_db(:integer, _query), do: "unsigned"
defp ecto_cast_to_db(:integer, _query), do: "signed"
defp ecto_cast_to_db(:string, _query), do: "char"
defp ecto_cast_to_db(:utc_datetime_usec, _query), do: "datetime(6)"
defp ecto_cast_to_db(:naive_datetime_usec, _query), do: "datetime(6)"
Expand Down
8 changes: 4 additions & 4 deletions test/ecto/adapters/myxql_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ defmodule Ecto.Adapters.MyXQLTest do
~s{UNION ALL } <>
~s{(SELECT sc0.`id`, st1.`depth` + 1 FROM `categories` AS sc0 } <>
~s{INNER JOIN `tree` AS st1 ON st1.`id` = sc0.`parent_id`)) } <>
~s{SELECT s0.`x`, t1.`id`, CAST(t1.`depth` AS unsigned) } <>
~s{SELECT s0.`x`, t1.`id`, CAST(t1.`depth` AS signed) } <>
~s{FROM `schema` AS s0 } <>
~s{INNER JOIN `tree` AS t1 ON t1.`id` = s0.`category_id`}
end
Expand Down Expand Up @@ -1475,7 +1475,7 @@ defmodule Ecto.Adapters.MyXQLTest do
|> plan()
|> all()

cast_types = %{bid: "binary(16)", num: "unsigned"}
cast_types = %{bid: "binary(16)", num: "signed"}
from_values_text = values_text(values, cast_types)
join_values_text = values_text(values, cast_types)
select_fields = Enum.map_join(types, ", ", fn {field, _} -> "v1.`#{field}`" end)
Expand All @@ -1498,7 +1498,7 @@ defmodule Ecto.Adapters.MyXQLTest do
|> plan(:delete_all)
|> delete_all()

cast_types = %{bid: "binary(16)", num: "unsigned"}
cast_types = %{bid: "binary(16)", num: "signed"}
values_text = values_text(values, cast_types)
fields = Enum.map_join(types, ",", fn {field, _} -> "`#{field}`" end)

Expand All @@ -1523,7 +1523,7 @@ defmodule Ecto.Adapters.MyXQLTest do
|> plan(:update_all)
|> update_all()

cast_types = %{bid: "binary(16)", num: "unsigned"}
cast_types = %{bid: "binary(16)", num: "signed"}
values_text = values_text(values, cast_types)
fields = Enum.map_join(types, ",", fn {field, _} -> "`#{field}`" end)

Expand Down

0 comments on commit 90da6e2

Please sign in to comment.