diff --git a/lib/ecto/adapters/sqlite3/codec.ex b/lib/ecto/adapters/sqlite3/codec.ex index 64fb68b..bc95e46 100644 --- a/lib/ecto/adapters/sqlite3/codec.ex +++ b/lib/ecto/adapters/sqlite3/codec.ex @@ -92,6 +92,7 @@ defmodule Ecto.Adapters.SQLite3.Codec do Application.get_env(:ecto_sqlite3, :json_library, Jason).encode(value) end + def blob_encode(nil), do: {:ok, nil} def blob_encode(value), do: {:ok, {:blob, value}} def bool_encode(nil), do: {:ok, nil} diff --git a/test/ecto/adapters/sqlite3/codec_test.exs b/test/ecto/adapters/sqlite3/codec_test.exs index ab8532d..0bed3d6 100644 --- a/test/ecto/adapters/sqlite3/codec_test.exs +++ b/test/ecto/adapters/sqlite3/codec_test.exs @@ -210,4 +210,17 @@ defmodule Ecto.Adapters.SQLite3.CodecTest do {:ok, "2011-01-09"} = Codec.date_encode(date, :iso8601) end end + + describe ".blob_encode/1" do + test "nil" do + {:ok, nil} = Codec.blob_encode(nil) + end + + test "valid blob" do + {:ok, {:blob, <<>>}} = Codec.blob_encode(<<>>) + + {:ok, {:blob, <<0xDE, 0xAD, 0xBE, 0xEF>>}} = + Codec.blob_encode(<<0xDE, 0xAD, 0xBE, 0xEF>>) + end + end end diff --git a/test/ecto/integration/blob_test.exs b/test/ecto/integration/blob_test.exs new file mode 100644 index 0000000..54096ce --- /dev/null +++ b/test/ecto/integration/blob_test.exs @@ -0,0 +1,24 @@ +defmodule Ecto.Integration.BlobTest do + use Ecto.Integration.Case + + alias Ecto.Integration.TestRepo + alias EctoSQLite3.Schemas.Setting + + @moduletag :integration + + test "updates blob to nil" do + setting = + %Setting{} + |> Setting.changeset(%{checksum: <<0x00, 0x01>>}) + |> TestRepo.insert!() + + # Read the record back using ecto and confirm it + assert %Setting{checksum: <<0x00, 0x01>>} = + TestRepo.get(Setting, setting.id) + + assert %Setting{checksum: nil} = + setting + |> Setting.changeset(%{checksum: nil}) + |> TestRepo.update!() + end +end diff --git a/test/support/migration.ex b/test/support/migration.ex index 0341bdc..d1cb67d 100644 --- a/test/support/migration.ex +++ b/test/support/migration.ex @@ -44,6 +44,7 @@ defmodule EctoSQLite3.Integration.Migration do create table(:settings) do add(:properties, :map) + add(:checksum, :binary) end end end diff --git a/test/support/schemas/setting.ex b/test/support/schemas/setting.ex index 8a89ccc..11a4fdd 100644 --- a/test/support/schemas/setting.ex +++ b/test/support/schemas/setting.ex @@ -7,9 +7,10 @@ defmodule EctoSQLite3.Schemas.Setting do schema "settings" do field(:properties, :map) + field(:checksum, :binary) end def changeset(struct, attrs) do - cast(struct, attrs, [:properties]) + cast(struct, attrs, [:properties, :checksum]) end end