fix: accept a Duration in from_now/1 - #2844
Conversation
`from_now`'s moduledoc documents `from_now(Duration.new!(day: 7))` and
`evaluate/1` implements it, but `args/0` declared only the interval form, so
arg validation rejected the call before `evaluate/1` was reached:
No such function from_now/1. Did you mean one of: from_now/2
`ago`, `date_add` and `datetime_add` all declare their Duration form. This
brings `from_now` into line.
The existing test called `evaluate/1` directly, which is reached only once
`args/0` declares the form, so it passed while the function was uncallable in
an expression. Added a test that goes through the expression path instead.
Also documents the Duration forms in the expressions guide, which listed only
the interval forms for all four functions.
| use Ash.Query.Function, name: :from_now, eager_evaluate?: false | ||
|
|
||
| def args, do: [[:integer, :duration_name]] | ||
| def args, do: [[:integer, :duration_name], [:duration]] |
There was a problem hiding this comment.
We either need to:
- update ash_sql code to handle this
- modify
def newto destructure the duration into its parts
I'm partial to #2
|
Thanks Zach, Ecto 3.14 has a native :duration type, and Postgres accepts a %Duration{} as a parameter directly — I checked against PG19: Multi-unit, negative and sub-second all arrive correctly. So option 1 is one extra clause per function rendering That multi-unit row is also what put me off (2): a Worth noting this isn't really about Given how small it is I went ahead this way with ash_sql. I've contributed ash-project/ash_sql#245 and will do an ash_postgres test PR for merge on ash_sql release. This will deliver part of the ash_postgres Duration support ash-project/ash_postgres#553 which I'll continue with, it needs the operators and read/write path, which writes OK but needs to handle So if you are happy with the ash_sql PR, this one can be merged unchanged? |
|
🚀 Thank you for your contribution! 🚀 |
Contributor checklist
Fixes #2843
from_now's Function'args/0gains theDurationform, matchingagoago,date_addanddatetime_addall declare their Duration form. This bringsfrom_nowinto line.Added a test that goes through the expression path.
Documented missing Duration forms in the expressions guide.