Skip to content

Commit 14f1f76

Browse files
Encodes nil blobs for updates (#140)
1 parent d8b07f6 commit 14f1f76

File tree

5 files changed

+41
-1
lines changed

5 files changed

+41
-1
lines changed

lib/ecto/adapters/sqlite3/codec.ex

+1
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ defmodule Ecto.Adapters.SQLite3.Codec do
9292
Application.get_env(:ecto_sqlite3, :json_library, Jason).encode(value)
9393
end
9494

95+
def blob_encode(nil), do: {:ok, nil}
9596
def blob_encode(value), do: {:ok, {:blob, value}}
9697

9798
def bool_encode(nil), do: {:ok, nil}

test/ecto/adapters/sqlite3/codec_test.exs

+13
Original file line numberDiff line numberDiff line change
@@ -210,4 +210,17 @@ defmodule Ecto.Adapters.SQLite3.CodecTest do
210210
{:ok, "2011-01-09"} = Codec.date_encode(date, :iso8601)
211211
end
212212
end
213+
214+
describe ".blob_encode/1" do
215+
test "nil" do
216+
{:ok, nil} = Codec.blob_encode(nil)
217+
end
218+
219+
test "valid blob" do
220+
{:ok, {:blob, <<>>}} = Codec.blob_encode(<<>>)
221+
222+
{:ok, {:blob, <<0xDE, 0xAD, 0xBE, 0xEF>>}} =
223+
Codec.blob_encode(<<0xDE, 0xAD, 0xBE, 0xEF>>)
224+
end
225+
end
213226
end

test/ecto/integration/blob_test.exs

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
defmodule Ecto.Integration.BlobTest do
2+
use Ecto.Integration.Case
3+
4+
alias Ecto.Integration.TestRepo
5+
alias EctoSQLite3.Schemas.Setting
6+
7+
@moduletag :integration
8+
9+
test "updates blob to nil" do
10+
setting =
11+
%Setting{}
12+
|> Setting.changeset(%{checksum: <<0x00, 0x01>>})
13+
|> TestRepo.insert!()
14+
15+
# Read the record back using ecto and confirm it
16+
assert %Setting{checksum: <<0x00, 0x01>>} =
17+
TestRepo.get(Setting, setting.id)
18+
19+
assert %Setting{checksum: nil} =
20+
setting
21+
|> Setting.changeset(%{checksum: nil})
22+
|> TestRepo.update!()
23+
end
24+
end

test/support/migration.ex

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ defmodule EctoSQLite3.Integration.Migration do
4444

4545
create table(:settings) do
4646
add(:properties, :map)
47+
add(:checksum, :binary)
4748
end
4849
end
4950
end

test/support/schemas/setting.ex

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ defmodule EctoSQLite3.Schemas.Setting do
77

88
schema "settings" do
99
field(:properties, :map)
10+
field(:checksum, :binary)
1011
end
1112

1213
def changeset(struct, attrs) do
13-
cast(struct, attrs, [:properties])
14+
cast(struct, attrs, [:properties, :checksum])
1415
end
1516
end

0 commit comments

Comments
 (0)