Skip to content
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

Support the use of chrono::DateTime<Utc> in sea-orm #429

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ futures-util = { version = "^0.3" }
tracing = { version = "0.1", features = ["log"] }
rust_decimal = { version = "^1", optional = true }
sea-orm-macros = { version = "^0.5.0", path = "sea-orm-macros", optional = true }
sea-query = { version = "^0.20.0", features = ["thread-safe"] }
sea-query = { git = "https://github.com/charleschege/sea-query.git", features = ["thread-safe"] }
sea-strum = { version = "^0.23", features = ["derive", "sea-orm"] }
serde = { version = "^1.0", features = ["derive"] }
serde_json = { version = "^1", optional = true }
Expand Down
5 changes: 5 additions & 0 deletions src/entity/active_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -608,6 +608,11 @@ impl_into_active_value!(crate::prelude::DateTime, Set);
#[cfg_attr(docsrs, doc(cfg(feature = "with-chrono")))]
impl_into_active_value!(crate::prelude::DateTimeWithTimeZone, Set);

#[cfg(feature = "with-chrono")]
#[cfg_attr(docsrs, doc(cfg(feature = "with-chrono")))]
impl_into_active_value!(crate::prelude::DateTimeUtc, Set);


#[cfg(feature = "with-rust_decimal")]
#[cfg_attr(docsrs, doc(cfg(feature = "with-rust_decimal")))]
impl_into_active_value!(crate::prelude::Decimal, Set);
Expand Down
44 changes: 44 additions & 0 deletions src/entity/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,50 @@ pub use chrono::NaiveDateTime as DateTime;
#[cfg(feature = "with-chrono")]
pub type DateTimeWithTimeZone = chrono::DateTime<chrono::FixedOffset>;

/// Handles the time and dates in UTC
///
/// ### Example Usage
/// ```ignore
/// use chrono::{DateTime, NaiveDateTime, Utc};
/// use sea_orm::prelude::*;
///
/// let my_model = fruit::Model {
/// id: 3_i32,
/// name: "Fruit".to_owned(),
/// cake_id: Some(4),
/// timer: DateTime::<Utc>::from_utc(NaiveDateTime::from_timestamp(61, 0), Utc),
/// };
///
/// assert_eq!(
/// fruit::Model {
/// id: 3,
/// name: "Fruit".to_owned(),
/// cake_id: Some(4,),
/// timer: DateTime::<Utc>::from_utc(NaiveDateTime::from_timestamp(61, 0), Utc),
/// },
/// my_model
/// );
///
/// // Define a `Model` containing a type of `DateTimeUtc` field
/// #[derive(Clone, Debug, PartialEq, DeriveModel, DeriveActiveModel)]
/// pub struct Model {
/// pub id: i32,
/// pub name: String,
/// pub cake_id: Option<i32>,
/// pub timer: DateTimeUtc,
/// }
///
/// #[derive(Copy, Clone, Debug, EnumIter, DeriveColumn)]
/// pub enum Column {
/// Id,
/// Name,
/// CakeId,
/// Timer,
/// }
/// ```
#[cfg(feature = "with-chrono")]
pub type DateTimeUtc = chrono::DateTime<chrono::Utc>;
Comment on lines +72 to +73
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The example would be inserting and updating ActiveModel with DateTimeUtc attribute.

e.g.

#[derive(Clone, Debug, PartialEq, DeriveEntityModel)]
#[sea_orm(table_name = "applog")]
pub struct Model {
    #[sea_orm(primary_key)]
    pub id: i32,
    pub action: String,
    pub json: Json,
    pub created_at: DateTimeWithTimeZone,
    pub deleted_at: DateTimeUtc,
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@billy1624 Some help on understanding the results of github action failures. They seem to build fine and store the values in the databases, but retrieval is an issue.

For example, asserting on mysql

2022-01-07T14:12:10.675164Z  INFO sqlx::query: /* SQLx ping */; rows: 0, elapsed: 5.909ms  
Error: Query("error returned from database: column \"launch_date\" is of type timestamp with time zone but expression is of type text")
thread 'main' panicked at 'assertion failed: `(left == right)`
  left: `1`,
 right: `0`: the test returned a termination value with a non-zero status code (1) which indicates a failure', /rustc/f1edd0429582dd29cccacaf50fd134b05593bd9c/library/test/src/lib.rs:195:5
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem you encountered is rooted in SeaQL/sea-query#222 (comment)


#[cfg(feature = "with-rust_decimal")]
pub use rust_decimal::Decimal;

Expand Down
7 changes: 7 additions & 0 deletions src/executor/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,10 @@ try_getable_all!(chrono::NaiveDateTime);
#[cfg(feature = "with-chrono")]
try_getable_date_time!(chrono::DateTime<chrono::FixedOffset>);

#[cfg(feature = "with-chrono")]
try_getable_date_time!(chrono::DateTime<chrono::Utc>);


#[cfg(feature = "with-rust_decimal")]
use rust_decimal::Decimal;

Expand Down Expand Up @@ -614,6 +618,9 @@ try_from_u64_err!(chrono::NaiveDateTime);
#[cfg(feature = "with-chrono")]
try_from_u64_err!(chrono::DateTime<chrono::FixedOffset>);

#[cfg(feature = "with-chrono")]
try_from_u64_err!(chrono::DateTime<chrono::Utc>);

#[cfg(feature = "with-rust_decimal")]
try_from_u64_err!(rust_decimal::Decimal);

Expand Down
14 changes: 12 additions & 2 deletions src/query/combine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,21 @@ where
None => {
let col = match &sel.expr {
SimpleExpr::Column(col_ref) => match &col_ref {
ColumnRef::Column(col) | ColumnRef::TableColumn(_, col) => col,
ColumnRef::Column(col)
| ColumnRef::TableColumn(_, col)
| ColumnRef::SchemaTableColumn(_, _, col) => col,
ColumnRef::Asterisk | ColumnRef::TableAsterisk(_) => {
panic!("cannot apply alias for Column with asterisk")
}
},
SimpleExpr::AsEnum(_, simple_expr) => match simple_expr.as_ref() {
SimpleExpr::Column(col_ref) => match &col_ref {
ColumnRef::Column(col) | ColumnRef::TableColumn(_, col) => col,
ColumnRef::Column(col)
| ColumnRef::TableColumn(_, col)
| ColumnRef::SchemaTableColumn(_, _, col) => col,
ColumnRef::Asterisk | ColumnRef::TableAsterisk(_) => {
panic!("cannot apply alias for AsEnum with asterisk")
}
},
_ => {
panic!("cannot apply alias for AsEnum with expr other than Column")
Expand Down