An Ecto SQLite3 adapter.
0

Configure Feed

Select the types of activity you want to include in your feed.

Fix datetime serialization format via `:datetime_type` config

Thank you @krwenholz for pointing this out.

closes: #116

Matthew Johnston (Sep 4, 2024, 9:38 PM -0500) 83d4a32b 826b0529

+44 -5
+1
CHANGELOG.md
··· 6 6 project adheres to [Semantic Versioning][semver]. 7 7 8 8 ## Unreleased 9 + - fixed: Handle datetime serialization format via `:datetime_type` config. 9 10 10 11 ## v0.17.1 11 12 - changed: Bump minimum ecto to `3.12`.
+30
test/ecto/integration/timestamps_test.exs
··· 181 181 |> TestRepo.all() 182 182 end 183 183 184 + test "using built in ecto functions" do 185 + account = insert_account(%{name: "Test"}) 186 + 187 + insert_product(%{ 188 + account_id: account.id, 189 + name: "Foo", 190 + inserted_at: seconds_ago(1) 191 + }) 192 + 193 + insert_product(%{ 194 + account_id: account.id, 195 + name: "Bar", 196 + inserted_at: seconds_ago(3) 197 + }) 198 + 199 + assert [ 200 + %{name: "Foo"}, 201 + ] = 202 + Product 203 + |> select([p], p) 204 + |> where([p], p.inserted_at >= ago(2, "second")) 205 + |> order_by([p], desc: p.inserted_at) 206 + |> TestRepo.all() 207 + end 208 + 184 209 defp insert_account(attrs) do 185 210 %Account{} 186 211 |> Account.changeset(attrs) ··· 191 216 %Product{} 192 217 |> Product.changeset(attrs) 193 218 |> TestRepo.insert!() 219 + end 220 + 221 + defp seconds_ago(seconds) do 222 + now = DateTime.utc_now() 223 + DateTime.add(now, -seconds, :second) 194 224 end 195 225 end
+6 -2
lib/ecto/adapters/sqlite3/codec.ex
··· 114 114 end 115 115 116 116 @text_datetime_format "%Y-%m-%d %H:%M:%S" 117 + @iso8601_format "%Y-%m-%dT%H:%M:%S" 118 + 119 + def datetime_format(:text_datetime), do: @text_datetime_format 120 + def datetime_format(_), do: @iso8601_format 117 121 118 122 def utc_datetime_encode(nil, :iso8601), do: {:ok, nil} 119 123 def utc_datetime_encode(nil, :text_datetime), do: {:ok, nil} 120 124 121 125 def utc_datetime_encode(%{time_zone: "Etc/UTC"} = value, :iso8601) do 122 - {:ok, NaiveDateTime.to_iso8601(value)} 126 + {:ok, Calendar.strftime(value, @iso8601_format)} 123 127 end 124 128 125 129 def utc_datetime_encode(%{time_zone: "Etc/UTC"} = value, :text_datetime) do ··· 135 139 def naive_datetime_encode(nil, :text_datetime), do: {:ok, nil} 136 140 137 141 def naive_datetime_encode(value, :iso8601) do 138 - {:ok, NaiveDateTime.to_iso8601(value)} 142 + {:ok, Calendar.strftime(value, @iso8601_format)} 139 143 end 140 144 141 145 def naive_datetime_encode(value, :text_datetime) do
+5 -1
lib/ecto/adapters/sqlite3/connection.ex
··· 1369 1369 [quote_name(name)] 1370 1370 end 1371 1371 1372 + @datetime_type Application.compile_env(:ecto_sqlite3, :datetime_type, :iso8601) 1373 + 1372 1374 defp expr({:datetime_add, _, [datetime, count, interval]}, sources, query) do 1375 + format = Ecto.Adapters.SQLite3.Codec.datetime_format(@datetime_type) 1376 + 1373 1377 [ 1374 1378 "CAST (", 1375 - "strftime('%Y-%m-%d %H:%M:%f000Z'", 1379 + "strftime('#{format}'", 1376 1380 ",", 1377 1381 expr(datetime, sources, query), 1378 1382 ",",
+2 -2
test/ecto/adapters/sqlite3/connection/datetime_add_test.exs
··· 11 11 |> select([], true) 12 12 |> plan() 13 13 14 - assert ~s{SELECT 1 FROM "schema" AS s0 WHERE (CAST (strftime('%Y-%m-%d %H:%M:%f000Z',s0.\"foo\",1 || ' month') AS TEXT) > s0."bar")} == 14 + assert ~s{SELECT 1 FROM "schema" AS s0 WHERE (CAST (strftime('%Y-%m-%dT%H:%M:%S',s0.\"foo\",1 || ' month') AS TEXT) > s0."bar")} == 15 15 all(query) 16 16 end 17 17 ··· 22 22 |> select([], true) 23 23 |> plan() 24 24 25 - assert ~s{SELECT 1 FROM "schema" AS s0 WHERE (CAST (strftime('%Y-%m-%d %H:%M:%f000Z',CAST(s0.\"foo\" AS TEXT),1 || ' month') AS TEXT) > s0."bar")} == 25 + assert ~s{SELECT 1 FROM "schema" AS s0 WHERE (CAST (strftime('%Y-%m-%dT%H:%M:%S',CAST(s0.\"foo\" AS TEXT),1 || ' month') AS TEXT) > s0."bar")} == 26 26 all(query) 27 27 end 28 28 end