So, I’m trying to update a project to use Ecto 3.1. One of the issues I’ve encountered after wrapping my head around the new “usec” family of types was from_now
not working the way I expect.
in order for this to work with milliseconds, I had to change
from e in MyModel,
join: se in assoc(e, :segment_export),
where: e.last_updated < from_now(^minus_n, ^tu),
order_by: e.last_updated,
preload: [segment_export: se]
into ugly
from e in MyModel,
join: se in assoc(e, :segment_export),
where: e.last_updated < datetime_add(type(^DateTime.utc_now, :naive_datetime_usec), ^minus_n, ^tu),
order_by: e.last_updated,
preload: [segment_export: se]
The last_updated
field is defined with a proper type:
field :last_updated, :naive_datetime_usec, default: @default_timestamp
Is this just a sad new world, or a bug or maybe I hold it wrong?
1 Like
It looks like it should work, so please file a bug report. Also, when filing the report, please describe exactly how it was working before and exactly which error are you seeing now (is it a type error?).
2 Likes
Sorry, I was tired yesterday, I should have specified what exactly was my problem.
Without an explicit type cast type(^DateTime.utc_now, :naive_datetime_usec)
query generator drops the precision of DateTime.utc_now
:
UERY OK source="foo_exports" db=1.7ms
SELECT <redacted> FROM "foo_exports" AS f0 INNER JOIN "bar_exports" AS s1 ON s1."id" = f0."export_id" WHERE (f0."last_updated" < $1::timestamp + ($2::numeric * interval '1 millisecond')) ORDER BY f0."last_updated" LIMIT 1 FOR UPDATE SKIP LOCKED [~N[2019-07-19 20:10:03], #Decimal<-1>]
In tests, I have very tight time boundaries, and the query above returns an empty result set. Meanwhile, with the typecast in place, the timestamp obtained from utc_now
is represented as ~N[2019-07-20 09:29:59.044696]
and therefore have enough precision for the query to always see a row inserted earlier in the test.