Skip to content

Commit

Permalink
Encodes nil blobs for updates (#140)
Browse files Browse the repository at this point in the history
  • Loading branch information
jessestimpson authored Feb 4, 2024
1 parent d8b07f6 commit 14f1f76
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 1 deletion.
1 change: 1 addition & 0 deletions lib/ecto/adapters/sqlite3/codec.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
13 changes: 13 additions & 0 deletions test/ecto/adapters/sqlite3/codec_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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
24 changes: 24 additions & 0 deletions test/ecto/integration/blob_test.exs
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions test/support/migration.ex
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ defmodule EctoSQLite3.Integration.Migration do

create table(:settings) do
add(:properties, :map)
add(:checksum, :binary)
end
end
end
3 changes: 2 additions & 1 deletion test/support/schemas/setting.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 14f1f76

Please sign in to comment.