Skip to content

Commit b8a5a5c

Browse files
committed
Encodes nil blobs for updates
1 parent d8b07f6 commit b8a5a5c

File tree

5 files changed

+38
-1
lines changed

5 files changed

+38
-1
lines changed

lib/ecto/adapters/sqlite3/codec.ex

Lines changed: 1 addition & 0 deletions
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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,4 +210,15 @@ 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+
{:ok, {:blob, <<0xde, 0xad, 0xbe, 0xef>>}} = Codec.blob_encode(<<0xde, 0xad, 0xbe, 0xef>>)
222+
end
223+
end
213224
end

test/ecto/integration/blob_test.exs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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} = setting
20+
|> Setting.changeset(%{checksum: nil})
21+
|> TestRepo.update!()
22+
end
23+
end

test/support/migration.ex

Lines changed: 1 addition & 0 deletions
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

Lines changed: 2 additions & 1 deletion
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)