Skip to content

Commit 11760a3

Browse files
committed
Make array encoding configurable
1 parent 206b84a commit 11760a3

File tree

3 files changed

+29
-4
lines changed

3 files changed

+29
-4
lines changed

lib/ecto/adapters/sqlite3.ex

+3
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ defmodule Ecto.Adapters.SQLite3 do
5454
* `:map_type` - Defaults to `:string`. Determines the type of `:map` columns.
5555
Set to `:binary` to use the [JSONB](https://sqlite.org/draft/jsonb.html)
5656
storage format.
57+
* `:array_type` - Defaults to `:string`. Determines the type of `:array` columns.
58+
Arrays are serialized using JSON. Set to `:binary` to use the
59+
[JSONB](https://sqlite.org/draft/jsonb.html) storage format.
5760
* `:datetime_type` - Defaults to `:iso8601`. Determines how datetime fields are
5861
stored in the database. The allowed values are `:iso8601` and `:text_datetime`.
5962
`:iso8601` corresponds to a string of the form `YYYY-MM-DDThh:mm:ss` and

lib/ecto/adapters/sqlite3/data_type.ex

+14-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ defmodule Ecto.Adapters.SQLite3.DataType do
1717
def column_type(:string, _opts), do: "TEXT"
1818
def column_type(:float, _opts), do: "NUMERIC"
1919
def column_type(:binary, _opts), do: "BLOB"
20-
def column_type(:array, _opts), do: "TEXT"
21-
def column_type({:array, _}, _opts), do: "TEXT"
2220
def column_type(:date, _opts), do: "TEXT"
2321
def column_type(:utc_datetime, _opts), do: "TEXT"
2422
def column_type(:utc_datetime_usec, _opts), do: "TEXT"
@@ -41,6 +39,20 @@ defmodule Ecto.Adapters.SQLite3.DataType do
4139
end
4240
end
4341

42+
def column_type(:array, _opts) do
43+
case Application.get_env(:ecto_sqlite3, :array_type, :string) do
44+
:string -> "TEXT"
45+
:binary -> "BLOB"
46+
end
47+
end
48+
49+
def column_type({:array, _}, _opts) do
50+
case Application.get_env(:ecto_sqlite3, :array_type, :string) do
51+
:string -> "TEXT"
52+
:binary -> "BLOB"
53+
end
54+
end
55+
4456
def column_type(:binary_id, _opts) do
4557
case Application.get_env(:ecto_sqlite3, :binary_id_type, :string) do
4658
:string -> "TEXT"

test/ecto/adapters/sqlite3/data_type_test.exs

+12-2
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@ defmodule Ecto.Adapters.SQLite3.DataTypeTest do
44
alias Ecto.Adapters.SQLite3.DataType
55

66
setup do
7+
Application.put_env(:ecto_sqlite3, :array_type, :string)
78
Application.put_env(:ecto_sqlite3, :binary_id_type, :string)
89
Application.put_env(:ecto_sqlite3, :map_type, :string)
910
Application.put_env(:ecto_sqlite3, :uuid_type, :string)
1011

1112
on_exit(fn ->
13+
Application.put_env(:ecto_sqlite3, :array_type, :string)
1214
Application.put_env(:ecto_sqlite3, :binary_id_type, :string)
1315
Application.put_env(:ecto_sqlite3, :map_type, :string)
1416
Application.put_env(:ecto_sqlite3, :uuid_type, :string)
@@ -64,12 +66,20 @@ defmodule Ecto.Adapters.SQLite3.DataTypeTest do
6466
assert DataType.column_type({:map, %{}}, nil) == "BLOB"
6567
end
6668

67-
test ":array is TEXT" do
69+
test ":array is TEXT or BLOB" do
6870
assert DataType.column_type(:array, nil) == "TEXT"
71+
72+
Application.put_env(:ecto_sqlite3, :array_type, :binary)
73+
74+
assert DataType.column_type(:array, nil) == "BLOB"
6975
end
7076

71-
test "{:array, _} is TEXT" do
77+
test "{:array, _} is TEXT or BLOB" do
7278
assert DataType.column_type({:array, []}, nil) == "TEXT"
79+
80+
Application.put_env(:ecto_sqlite3, :array_type, :binary)
81+
82+
assert DataType.column_type({:array, []}, nil) == "BLOB"
7383
end
7484

7585
test ":float is NUMERIC" do

0 commit comments

Comments
 (0)