Repository navigation
Support datetime_add and date_add so ago/2 keeps its interval #123
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
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,162 @@ | ||
| defmodule EctoLibSql.EctoDatetimeIntervalTest do | ||
| @moduledoc """ | ||
| Coverage for `ago/2` and `from_now/2`, which Ecto lowers to `datetime_add` and | ||
| `date_add`. | ||
|
|
||
| Neither had an `expr/3` clause, so both fell through to the catch-all that emits a | ||
| bare "?" and the interval was dropped: `inserted_at > ago(14, "day")` became | ||
| `inserted_at > ?` bound to the current time. Nothing raised - Ecto plans the | ||
| parameters regardless of how many placeholders the adapter emits, and SQLite | ||
| ignores an unreferenced one - so the comparison silently degraded to | ||
| `column > now()` and rejected anything older than the current second while never | ||
| enforcing the interval at all. | ||
| """ | ||
| use ExUnit.Case, async: false | ||
|
|
||
| import Ecto.Query | ||
|
|
||
| defmodule TestRepo do | ||
| use Ecto.Repo, otp_app: :ecto_libsql, adapter: Ecto.Adapters.LibSql | ||
| end | ||
|
|
||
| defmodule Event do | ||
| use Ecto.Schema | ||
|
|
||
| schema "interval_events" do | ||
| field(:name, :string) | ||
| field(:at, :utc_datetime) | ||
| field(:on_date, :date) | ||
| end | ||
| end | ||
|
|
||
| setup do | ||
| unique_id = :erlang.unique_integer([:positive]) | ||
| test_db = "z_ecto_libsql_test-datetime_interval-#{unique_id}.db" | ||
|
|
||
| {:ok, pid} = TestRepo.start_link(database: test_db, pool_size: 1, name: TestRepo) | ||
|
|
||
| Ecto.Adapters.SQL.query!(TestRepo, """ | ||
| CREATE TABLE IF NOT EXISTS interval_events ( | ||
| id INTEGER PRIMARY KEY AUTOINCREMENT, | ||
| name TEXT, | ||
| at TEXT, | ||
| on_date TEXT | ||
| ) | ||
| """) | ||
|
|
||
| on_exit(fn -> | ||
| if Process.alive?(pid) do | ||
| try do | ||
| :ok = Supervisor.stop(pid) | ||
| catch | ||
| :exit, _ -> :ok | ||
| end | ||
| end | ||
|
|
||
| # Give SQLite time to release the file handles before removing them. | ||
| Process.sleep(50) | ||
| for f <- Path.wildcard(test_db <> "*"), do: File.rm(f) | ||
| end) | ||
|
|
||
| :ok | ||
| end | ||
|
|
||
| defp at!(name, amount, unit) do | ||
| at = DateTime.add(DateTime.utc_now(:second), amount, unit) | ||
| TestRepo.insert!(%Event{name: name, at: at}) | ||
| end | ||
|
|
||
| defp names(query), do: query |> TestRepo.all() |> Enum.sort() | ||
|
|
||
| describe "ago/2" do | ||
| test "keeps rows inside the window and drops rows outside it" do | ||
| at!("now", 0, :second) | ||
| at!("day-13", -13, :day) | ||
| at!("day-15", -15, :day) | ||
| at!("year-ago", -365, :day) | ||
|
|
||
| assert names(from(e in Event, where: e.at > ago(14, "day"), select: e.name)) == | ||
| ["day-13", "now"] | ||
| end | ||
|
|
||
| test "a row hours old is still inside a day-scale window" do | ||
| at!("hours-ago", -6, :hour) | ||
|
|
||
| # The bug made every window behave as `> now()`, so this vanished entirely. | ||
| assert names(from(e in Event, where: e.at > ago(14, "day"), select: e.name)) == | ||
| ["hours-ago"] | ||
| end | ||
|
|
||
| test "minute and hour units resolve to their own scale" do | ||
| at!("min-10", -10, :minute) | ||
| at!("min-20", -20, :minute) | ||
|
|
||
| assert names(from(e in Event, where: e.at > ago(15, "minute"), select: e.name)) == | ||
| ["min-10"] | ||
|
|
||
| assert names(from(e in Event, where: e.at > ago(1, "hour"), select: e.name)) == | ||
| ["min-10", "min-20"] | ||
| end | ||
|
|
||
| test "week, millisecond and microsecond are converted to units SQLite has" do | ||
| at!("day-3", -3, :day) | ||
| at!("day-10", -10, :day) | ||
|
|
||
| assert names(from(e in Event, where: e.at > ago(1, "week"), select: e.name)) == ["day-3"] | ||
|
|
||
| at!("recent", -2, :second) | ||
|
|
||
| assert names(from(e in Event, where: e.at > ago(5000, "millisecond"), select: e.name)) == | ||
| ["recent"] | ||
|
|
||
| assert names(from(e in Event, where: e.at > ago(5_000_000, "microsecond"), select: e.name)) == | ||
| ["recent"] | ||
| end | ||
|
|
||
| test "an interpolated count still binds as a parameter" do | ||
| at!("day-3", -3, :day) | ||
| at!("day-10", -10, :day) | ||
|
|
||
| days = 7 | ||
| query = from(e in Event, where: e.at > ago(^days, "day"), select: e.name) | ||
|
|
||
| assert names(query) == ["day-3"] | ||
|
|
||
| # The count is rendered through expr/3 rather than interpolated into the SQL, | ||
| # so an interpolated value stays a bound parameter. | ||
| {sql, _params} = Ecto.Adapters.SQL.to_sql(:all, TestRepo, query) | ||
| assert sql =~ "strftime" | ||
| assert sql =~ "days" | ||
| end | ||
|
|
||
| test "the interval reaches the SQL rather than being dropped" do | ||
| {sql, _params} = | ||
| Ecto.Adapters.SQL.to_sql(:all, TestRepo, from(e in Event, where: e.at > ago(14, "day"))) | ||
|
|
||
| assert sql =~ "strftime" | ||
| assert sql =~ "days" | ||
| refute sql =~ ~r/"at" > \?\s*\)/ | ||
| end | ||
| end | ||
|
|
||
| describe "from_now/2" do | ||
| test "selects rows ahead of now" do | ||
| at!("past", -1, :day) | ||
| at!("soon", 60, :second) | ||
| at!("later", 3, :day) | ||
|
|
||
| assert names(from(e in Event, where: e.at < from_now(1, "day"), select: e.name)) == | ||
| ["past", "soon"] | ||
| end | ||
| end | ||
|
|
||
| describe "date_add" do | ||
| test "applies the interval to a date column" do | ||
| today = Date.utc_today() | ||
| TestRepo.insert!(%Event{name: "d-3", on_date: Date.add(today, -3)}) | ||
| TestRepo.insert!(%Event{name: "d-30", on_date: Date.add(today, -30)}) | ||
|
|
||
| assert names(from(e in Event, where: e.on_date > ago(7, "day"), select: e.name)) == ["d-3"] | ||
| end | ||
| end | ||
| end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🟠 Major | 🏗️ Heavy lift
🔎 Supported by static analysis
🏁 Script executed:
Repository: ocean/ecto_libsql
Length of output: 34738
🏁 Script executed:
Repository: ocean/ecto_libsql
Length of output: 36620
🏁 Script executed:
Repository: ocean/ecto_libsql
Length of output: 33925
🏁 Script executed:
Repository: ocean/ecto_libsql
Length of output: 33925
Preserve datetime precision and shape in
datetime_add.ago/2andfrom_now/2reach this renderer, which emits a whole-secondstrftimeresult. The adapter stores:utc_datetime_usecvalues with.ffffffZand:naive_datetime_usecvalues with.ffffffand noZ. Text comparison can therefore exclude or misorder values within the cutoff second.Use a comparison representation that preserves microseconds and normalises both operands to the stored UTC or naive datetime shape. Add boundary tests for both microsecond field types.
🤖 Prompt for AI Agents