Skip to content

Commit 8f7de49

Browse files
authored
Add support for sqlite strict mode. (#97)
* Handle TEXT_UUID to TEXT. * A uuid is TEXT or BLOB. * Swap :datetime since it is passed to the database directly. * The type ecto uses is :utc_datetime. * A boolean is an INTEGER.
1 parent 2ee3db4 commit 8f7de49

File tree

5 files changed

+35
-34
lines changed

5 files changed

+35
-34
lines changed

lib/ecto/adapters/sqlite3.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,8 @@ defmodule Ecto.Adapters.SQLite3 do
8383
8484
The `:binary_id_type` configuration option allows configuring how `:binary_id` fields are stored in the database as
8585
well as the type of the column in which these IDs will be stored. The possible values are:
86-
* `:string` (default): IDs are stored as strings, and the type of the column is `TEXT_UUID`.
87-
* `:binary`: IDs are stored in their raw binary form, and the type of the column is `UUID`.
86+
* `:string` (default): IDs are stored as strings, and the type of the column is `TEXT`.
87+
* `:binary`: IDs are stored in their raw binary form, and the type of the column is `BLOB`.
8888
8989
The main differences between the two formats are as follows:
9090
* When stored as binary, UUIDs require much less space in the database. IDs stored as strings require 36 bytes each,

lib/ecto/adapters/sqlite3/connection.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1296,7 +1296,7 @@ defmodule Ecto.Adapters.SQLite3.Connection do
12961296
expr(datetime, sources, query),
12971297
",",
12981298
interval(count, interval, sources),
1299-
") AS TEXT_DATETIME)"
1299+
") AS TEXT)"
13001300
]
13011301
end
13021302

@@ -1308,7 +1308,7 @@ defmodule Ecto.Adapters.SQLite3.Connection do
13081308
expr(date, sources, query),
13091309
",",
13101310
interval(count, interval, sources),
1311-
") AS TEXT_DATE)"
1311+
") AS TEXT)"
13121312
]
13131313
end
13141314

lib/ecto/adapters/sqlite3/data_type.ex

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ defmodule Ecto.Adapters.SQLite3.DataType do
1111
def column_type(:id, _opts), do: "INTEGER"
1212
def column_type(:serial, _opts), do: "INTEGER"
1313
def column_type(:bigserial, _opts), do: "INTEGER"
14+
def column_type(:boolean, _opts), do: "INTEGER"
1415
def column_type(:bigint, _opts), do: "INTEGER"
1516
def column_type(:string, _opts), do: "TEXT"
1617
def column_type(:float, _opts), do: "NUMERIC"
@@ -19,10 +20,10 @@ defmodule Ecto.Adapters.SQLite3.DataType do
1920
def column_type(:array, _opts), do: "JSON"
2021
def column_type({:map, _}, _opts), do: "JSON"
2122
def column_type({:array, _}, _opts), do: "JSON"
22-
def column_type(:utc_datetime, _opts), do: "TEXT_DATETIME"
23-
def column_type(:utc_datetime_usec, _opts), do: "TEXT_DATETIME"
24-
def column_type(:naive_datetime, _opts), do: "TEXT_DATETIME"
25-
def column_type(:naive_datetime_usec, _opts), do: "TEXT_DATETIME"
23+
def column_type(:utc_datetime, _opts), do: "TEXT"
24+
def column_type(:utc_datetime_usec, _opts), do: "TEXT"
25+
def column_type(:naive_datetime, _opts), do: "TEXT"
26+
def column_type(:naive_datetime_usec, _opts), do: "TEXT"
2627
def column_type(:decimal, nil), do: "DECIMAL"
2728

2829
def column_type(:decimal, opts) do
@@ -39,15 +40,15 @@ defmodule Ecto.Adapters.SQLite3.DataType do
3940

4041
def column_type(:binary_id, _opts) do
4142
case Application.get_env(:ecto_sqlite3, :binary_id_type, :string) do
42-
:string -> "TEXT_UUID"
43-
:binary -> "UUID"
43+
:string -> "TEXT"
44+
:binary -> "BLOB"
4445
end
4546
end
4647

4748
def column_type(:uuid, _opts) do
4849
case Application.get_env(:ecto_sqlite3, :uuid_type, :string) do
49-
:string -> "TEXT_UUID"
50-
:binary -> "UUID"
50+
:string -> "TEXT"
51+
:binary -> "BLOB"
5152
end
5253
end
5354

test/ecto/adapters/sqlite3/connection_test.exs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -955,7 +955,7 @@ defmodule Ecto.Adapters.SQLite3.ConnectionTest do
955955
|> select([], type(^"601d74e4-a8d3-4b6e-8365-eddb4c893327", Ecto.UUID))
956956
|> plan()
957957

958-
assert all(query) == ~s{SELECT CAST(? AS TEXT_UUID) FROM "schema" AS s0}
958+
assert all(query) == ~s{SELECT CAST(? AS TEXT) FROM "schema" AS s0}
959959
end
960960

961961
test "string type" do
@@ -2152,7 +2152,7 @@ defmodule Ecto.Adapters.SQLite3.ConnectionTest do
21522152
[precision: 8, scale: 2, default: {:fragment, "expr"}]},
21532153
{:add, :on_hand, :integer, [default: 0, null: true]},
21542154
{:add, :likes, :integer, [default: 0, null: false]},
2155-
{:add, :published_at, :datetime, [null: true]},
2155+
{:add, :published_at, :utc_datetime, [null: true]},
21562156
{:add, :is_active, :boolean, [default: true]},
21572157
{:add, :notes, :text, [collate: :nocase]},
21582158
{:add, :meta, :text, [check: %{name: "meta_constraint", expr: "meta != 'a'"}]}
@@ -2166,8 +2166,8 @@ defmodule Ecto.Adapters.SQLite3.ConnectionTest do
21662166
"price" NUMERIC DEFAULT expr, \
21672167
"on_hand" INTEGER DEFAULT 0 NULL, \
21682168
"likes" INTEGER DEFAULT 0 NOT NULL, \
2169-
"published_at" DATETIME NULL, \
2170-
"is_active" BOOLEAN DEFAULT true, \
2169+
"published_at" TEXT NULL, \
2170+
"is_active" INTEGER DEFAULT true, \
21712171
"notes" TEXT COLLATE NOCASE, \
21722172
"meta" TEXT CONSTRAINT meta_constraint CHECK (meta != 'a')\
21732173
)\
@@ -2337,8 +2337,8 @@ defmodule Ecto.Adapters.SQLite3.ConnectionTest do
23372337
assert execute_ddl(create) == [
23382338
"""
23392339
CREATE TABLE "posts" (\
2340-
"published_at" TEXT_DATETIME, \
2341-
"submitted_at" TEXT_DATETIME\
2340+
"published_at" TEXT, \
2341+
"submitted_at" TEXT\
23422342
)\
23432343
"""
23442344
]
@@ -2353,7 +2353,7 @@ defmodule Ecto.Adapters.SQLite3.ConnectionTest do
23532353
]}
23542354

23552355
assert execute_ddl(create) == [
2356-
~s{CREATE TABLE "posts" ("published_at" TEXT_DATETIME, "submitted_at" TEXT_DATETIME)}
2356+
~s{CREATE TABLE "posts" ("published_at" TEXT, "submitted_at" TEXT)}
23572357
]
23582358
end
23592359

@@ -2443,7 +2443,7 @@ defmodule Ecto.Adapters.SQLite3.ConnectionTest do
24432443
""",
24442444
"""
24452445
ALTER TABLE "posts" \
2446-
ADD COLUMN "when" TEXT_DATETIME\
2446+
ADD COLUMN "when" TEXT\
24472447
"""
24482448
]
24492449
end

test/ecto/adapters/sqlite3/data_type_test.exs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,24 +26,24 @@ defmodule Ecto.Adapters.SQLite3.DataTypeTest do
2626
assert DataType.column_type(:bigserial, nil) == "INTEGER"
2727
end
2828

29-
test ":binary_id is TEXT_UUID OR UUID" do
30-
assert DataType.column_type(:binary_id, nil) == "TEXT_UUID"
29+
test ":binary_id is TEXT OR UUID" do
30+
assert DataType.column_type(:binary_id, nil) == "TEXT"
3131

3232
Application.put_env(:ecto_sqlite3, :binary_id_type, :binary)
3333

34-
assert DataType.column_type(:binary_id, nil) == "UUID"
34+
assert DataType.column_type(:binary_id, nil) == "BLOB"
3535
end
3636

3737
test ":string is TEXT" do
3838
assert DataType.column_type(:string, nil) == "TEXT"
3939
end
4040

41-
test ":uuid is TEXT_UUID or UUID" do
42-
assert DataType.column_type(:uuid, nil) == "TEXT_UUID"
41+
test ":uuid is TEXT or UUID" do
42+
assert DataType.column_type(:uuid, nil) == "TEXT"
4343

4444
Application.put_env(:ecto_sqlite3, :uuid_type, :binary)
4545

46-
assert DataType.column_type(:uuid, nil) == "UUID"
46+
assert DataType.column_type(:uuid, nil) == "BLOB"
4747
end
4848

4949
test ":map is JSON" do
@@ -82,20 +82,20 @@ defmodule Ecto.Adapters.SQLite3.DataTypeTest do
8282
assert DataType.column_type(:binary, nil) == "BLOB"
8383
end
8484

85-
test ":utc_datetime is DATETIME" do
86-
assert DataType.column_type(:utc_datetime, nil) == "TEXT_DATETIME"
85+
test ":utc_datetime is TEXT" do
86+
assert DataType.column_type(:utc_datetime, nil) == "TEXT"
8787
end
8888

89-
test ":utc_datetime_usec is DATETIME" do
90-
assert DataType.column_type(:utc_datetime_usec, nil) == "TEXT_DATETIME"
89+
test ":utc_datetime_usec is TEXT" do
90+
assert DataType.column_type(:utc_datetime_usec, nil) == "TEXT"
9191
end
9292

93-
test ":naive_datetime is DATETIME" do
94-
assert DataType.column_type(:naive_datetime, nil) == "TEXT_DATETIME"
93+
test ":naive_datetime is TEXT" do
94+
assert DataType.column_type(:naive_datetime, nil) == "TEXT"
9595
end
9696

97-
test ":naive_datetime_usec is DATETIME" do
98-
assert DataType.column_type(:naive_datetime_usec, nil) == "TEXT_DATETIME"
97+
test ":naive_datetime_usec is TEXT" do
98+
assert DataType.column_type(:naive_datetime_usec, nil) == "TEXT"
9999
end
100100
end
101101
end

0 commit comments

Comments
 (0)