Skip to content

Encodes nil blobs for updates #140

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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