Skip to content

Commit a476817

Browse files
authored
Add missing :date column type (#112)
* Add missing column type map `:date` to `TEXT` * Add tests for Ecto.Adapters.SQLite3.Codec.date_encode/2 * Makes date_encode/2 require Date structs
1 parent f478b38 commit a476817

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

lib/ecto/adapters/sqlite3/codec.ex

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,15 @@ defmodule Ecto.Adapters.SQLite3.Codec do
6868
end
6969
end
7070

71+
def date_encode(val), do: date_encode(val, :iso8601)
72+
73+
def date_encode(%Date{} = val, :iso8601) do
74+
case Date.to_iso8601(val) do
75+
date when is_bitstring(date) -> {:ok, date}
76+
_ -> :error
77+
end
78+
end
79+
7180
def time_decode(nil), do: {:ok, nil}
7281

7382
def time_decode(value) do

lib/ecto/adapters/sqlite3/data_type.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ defmodule Ecto.Adapters.SQLite3.DataType do
2020
def column_type(:array, _opts), do: "TEXT"
2121
def column_type({:map, _}, _opts), do: "TEXT"
2222
def column_type({:array, _}, _opts), do: "TEXT"
23+
def column_type(:date, _opts), do: "TEXT"
2324
def column_type(:utc_datetime, _opts), do: "TEXT"
2425
def column_type(:utc_datetime_usec, _opts), do: "TEXT"
2526
def column_type(:naive_datetime, _opts), do: "TEXT"

test/ecto/adapters/sqlite3/codec_test.exs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,4 +176,17 @@ defmodule Ecto.Adapters.SQLite3.CodecTest do
176176
end
177177
end
178178
end
179+
180+
describe ".date_encode/2" do
181+
setup do
182+
[
183+
date: ~D[2011-01-09],
184+
datetime: ~U[2011-01-09 08:46:08.00Z]
185+
]
186+
end
187+
188+
test "on %Date{} structs", %{date: date} do
189+
{:ok, "2011-01-09"} = Codec.date_encode(date, :iso8601)
190+
end
191+
end
179192
end

0 commit comments

Comments
 (0)