Repository navigation
Feature Request: Add Support for jiff (like Diesel's chrono support)
#5141
Replies: 5 comments 7 replies
|
There is https://crates.io/crates/jiff-diesel to add a bridge between both crates. The plan is to add support for jiff to diesel as soon as they cut a 1.0 release (which was supposed to happen last year?). I'm also open to add support for it earlier to diesel as long as all the API's used by the diesel integration won't change with a hypothetical 1.0 release. If you can get such a guarantee I'm happy to review a PR. |
|
Thanks @weiznich! I'm indeed already using Some ergonomics I don't like: - pub expires_at: NaiveDateTime,
+ #[diesel(deserialize_as = jiff_diesel::Timestamp)]
+ pub expires_at: jiff::Timestamp,- pub effective_date: Option<NaiveDateTime>,
+ #[diesel(deserialize_as = jiff_diesel::NullableDateTime)]
+ pub effective_date: Option<jiff::civil::DateTime>,- .filter(sessions::ended_at.lt(now - grace))
+ .filter(sessions::ended_at.lt((now - grace).to_diesel())) --- a/src/lib.rs
+++ b/src/lib.rs
@@ -1,21 +1,32 @@
-//! BEFORE: instants stored in naive `TIMESTAMP` columns ("naive means UTC"),
-//! read back as `chrono::NaiveDateTime`.
+//! AFTER: instants stored in `TIMESTAMPTZ` columns, read back as
+//! jiff::Timestamp through `jiff-diesel`'s wrapper.
+use diesel::IntoSql as _;
use diesel::dsl::{IntervalDsl, now};
use diesel::prelude::*;
+use diesel::sql_types::Timestamptz;
diesel::table! {
leases (id) {
id -> Int4,
- leased_until -> Nullable<Timestamp>,
+ leased_until -> Nullable<Timestamptz>,
}
}
/// Extend a lease deadline on the database clock.
-pub fn extend_lease(conn: &mut PgConnection) -> QueryResult<Option<chrono::NaiveDateTime>> {
+pub fn extend_lease(conn: &mut PgConnection) -> QueryResult<Option<jiff_diesel::Timestamp>> {
diesel::update(leases::table)
- .filter(leases::leased_until.gt(now.nullable()))
- .set(leases::leased_until.eq((now + 300.seconds()).nullable()))
+ // Bare `now` still compares fine against a timestamptz column:
+ // AsExpression<Nullable<Timestamptz>> for `now` coerces it.
+ .filter(leases::leased_until.gt(now))
+ // But `now + 300.seconds()` no longer type-checks here: the sum is
+ // ops::Add<now, ...> with SqlType = Timestamp, and the coercion
+ // impls exist only on `now` itself, not on expressions containing
+ // it. The fix is to coerce before the arithmetic:
+ .set(
+ leases::leased_until
+ .eq((now.into_sql::<Timestamptz>() + 300.seconds()).nullable()),
+ )
.returning(leases::leased_until)
.get_result(conn)
}
The error if you keep the old expression against the Timestamptz column:
error[E0277]: the trait bound `Nullable<Add<now, Bound<Interval, PgInterval>>>: AsExpression<...>` is not satisfied
|
| .set(leases_tz::leased_until.eq((now + 300.seconds()).nullable()))
| ^^ unsatisfied trait bound
|
= help: the trait `AsExpression<diesel::sql_types::Nullable<diesel::sql_types::Timestamptz>>`
is not implemented for `Nullable<Add<now, Bound<Interval, PgInterval>>>`
= help: the following other types implement trait `AsExpression<T>`:
...
nowAlso because
That sounds totally reasonable; I'll do some research and see if they've made any such commitment. Chrono also never released a 1.0, so I'm guessing they made such a commitment (or that you're looking back and realizing mistakes were made :) ). I notice |
I will work on this and get back to you :)
That's totally fair, and a dependency version range like we do for
Ah man, I've just discovered the joys of rust-lang/cargo#7130 and hadn't realized we were still lacking something so basic.
I'll start with getting answers to your questions above, but I'd consider it if its the only way forward. Thanks! |
|
Here's the Type mapping
pg/types/date_and_time
mysql/types/date_and_time
sqlite/types/date_and_time
Optional
|
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Backend
PostgreSQL, MySQL, SQLite
What do you want to add to diesel?
As
chronois being soft-deprecated (see chronotope/chrono#1768; see also) and jiff is the recommended migration path, it makes sense to addjiffsupport to Diesel. Lots of teams are migrating away fromchrono, and Diesel support ends up causing friction for some teams.I fully realize that
jifflacks the maturity ofchrono, but with chrono potentially going unmaintained soon and a lot of teams migrating away, it seems to fill an important gap.Implementation notes
No response
Additional details
I'd be happy to raise a PR.
Checklist
All reactions