Skip to content

Make map/array encoding configurable #163

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 3 commits into from
Mar 19, 2025
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ All notable changes will be documented in this file.
The format is loosely based on [Keep a Changelog][keepachangelog], and this
project adheres to [Semantic Versioning][semver].

## Unreleased
- changed: Configurable encoding for `:map` and `:array`, allowing usage of SQLite's JSONB storage format.

## v0.18.1
- fixed: Support both `Jason` and `JSON`.

Expand Down
6 changes: 6 additions & 0 deletions lib/ecto/adapters/sqlite3.ex
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ defmodule Ecto.Adapters.SQLite3 do
* `:uuid_type` - Defaults to `:string`. Determines the type of `:uuid` columns.
Possible values and column types are the same as for
[binary IDs](#module-binary-id-types).
* `:map_type` - Defaults to `:string`. Determines the type of `:map` columns.
Set to `:binary` to use the [JSONB](https://sqlite.org/jsonb.html)
storage format.
* `:array_type` - Defaults to `:string`. Determines the type of `:array` columns.
Arrays are serialized using JSON. Set to `:binary` to use the
[JSONB](https://sqlite.org/jsonb.html) storage format.
* `:datetime_type` - Defaults to `:iso8601`. Determines how datetime fields are
stored in the database. The allowed values are `:iso8601` and `:text_datetime`.
`:iso8601` corresponds to a string of the form `YYYY-MM-DDThh:mm:ss` and
Expand Down
32 changes: 28 additions & 4 deletions lib/ecto/adapters/sqlite3/data_type.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@ defmodule Ecto.Adapters.SQLite3.DataType do
def column_type(:string, _opts), do: "TEXT"
def column_type(:float, _opts), do: "NUMERIC"
def column_type(:binary, _opts), do: "BLOB"
def column_type(:map, _opts), do: "TEXT"
def column_type(:array, _opts), do: "TEXT"
def column_type({:map, _}, _opts), do: "TEXT"
def column_type({:array, _}, _opts), do: "TEXT"
def column_type(:date, _opts), do: "TEXT"
def column_type(:utc_datetime, _opts), do: "TEXT"
def column_type(:utc_datetime_usec, _opts), do: "TEXT"
Expand All @@ -43,13 +39,41 @@ defmodule Ecto.Adapters.SQLite3.DataType do
end
end

def column_type(:array, _opts) do
case Application.get_env(:ecto_sqlite3, :array_type, :string) do
:string -> "TEXT"
:binary -> "BLOB"
end
end

def column_type({:array, _}, _opts) do
case Application.get_env(:ecto_sqlite3, :array_type, :string) do
:string -> "TEXT"
:binary -> "BLOB"
end
end

def column_type(:binary_id, _opts) do
case Application.get_env(:ecto_sqlite3, :binary_id_type, :string) do
:string -> "TEXT"
:binary -> "BLOB"
end
end

def column_type(:map, _opts) do
case Application.get_env(:ecto_sqlite3, :map_type, :string) do
:string -> "TEXT"
:binary -> "BLOB"
end
end

def column_type({:map, _}, _opts) do
case Application.get_env(:ecto_sqlite3, :map_type, :string) do
:string -> "TEXT"
:binary -> "BLOB"
end
end

def column_type(:uuid, _opts) do
case Application.get_env(:ecto_sqlite3, :uuid_type, :string) do
:string -> "TEXT"
Expand Down
28 changes: 24 additions & 4 deletions test/ecto/adapters/sqlite3/data_type_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@ defmodule Ecto.Adapters.SQLite3.DataTypeTest do
alias Ecto.Adapters.SQLite3.DataType

setup do
Application.put_env(:ecto_sqlite3, :array_type, :string)
Application.put_env(:ecto_sqlite3, :binary_id_type, :string)
Application.put_env(:ecto_sqlite3, :map_type, :string)
Application.put_env(:ecto_sqlite3, :uuid_type, :string)

on_exit(fn ->
Application.put_env(:ecto_sqlite3, :array_type, :string)
Application.put_env(:ecto_sqlite3, :binary_id_type, :string)
Application.put_env(:ecto_sqlite3, :map_type, :string)
Application.put_env(:ecto_sqlite3, :uuid_type, :string)
end)
end
Expand Down Expand Up @@ -46,20 +50,36 @@ defmodule Ecto.Adapters.SQLite3.DataTypeTest do
assert DataType.column_type(:uuid, nil) == "BLOB"
end

test ":map is TEXT" do
test ":map is TEXT or BLOB" do
assert DataType.column_type(:map, nil) == "TEXT"

Application.put_env(:ecto_sqlite3, :map_type, :binary)

assert DataType.column_type(:map, nil) == "BLOB"
end

test "{:map, _} is TEXT" do
test "{:map, _} is TEXT or BLOB" do
assert DataType.column_type({:map, %{}}, nil) == "TEXT"

Application.put_env(:ecto_sqlite3, :map_type, :binary)

assert DataType.column_type({:map, %{}}, nil) == "BLOB"
end

test ":array is TEXT" do
test ":array is TEXT or BLOB" do
assert DataType.column_type(:array, nil) == "TEXT"

Application.put_env(:ecto_sqlite3, :array_type, :binary)

assert DataType.column_type(:array, nil) == "BLOB"
end

test "{:array, _} is TEXT" do
test "{:array, _} is TEXT or BLOB" do
assert DataType.column_type({:array, []}, nil) == "TEXT"

Application.put_env(:ecto_sqlite3, :array_type, :binary)

assert DataType.column_type({:array, []}, nil) == "BLOB"
end

test ":float is NUMERIC" do
Expand Down
31 changes: 29 additions & 2 deletions test/ecto/integration/json_test.exs
Original file line number Diff line number Diff line change
@@ -1,13 +1,40 @@
defmodule Ecto.Integration.JsonTest do
use Ecto.Integration.Case
use Ecto.Integration.Case, async: false

alias Ecto.Adapters.SQL
alias Ecto.Integration.TestRepo
alias EctoSQLite3.Schemas.Setting

@moduletag :integration

test "serializes json correctly" do
setup do
Application.put_env(:ecto_sqlite3, :map_type, :string)
on_exit(fn -> Application.put_env(:ecto_sqlite3, :map_type, :string) end)
end

test "serializes json correctly with string format" do
# Insert a record purposefully with atoms as the map key. We are going to
# verify later they were coerced into strings.
setting =
%Setting{}
|> Setting.changeset(%{properties: %{foo: "bar", qux: "baz"}})
|> TestRepo.insert!()

# Read the record back using ecto and confirm it
assert %Setting{properties: %{"foo" => "bar", "qux" => "baz"}} =
TestRepo.get(Setting, setting.id)

assert %{num_rows: 1, rows: [["bar"]]} =
SQL.query!(
TestRepo,
"select json_extract(properties, '$.foo') from settings where id = ?1",
[setting.id]
)
end

test "serializes json correctly with binary format" do
Application.put_env(:ecto_sqlite3, :map_type, :binary)

# Insert a record purposefully with atoms as the map key. We are going to
# verify later they were coerced into strings.
setting =
Expand Down