Skip to content

Commit

Permalink
Support DateTimeLocal
Browse files Browse the repository at this point in the history
  • Loading branch information
billy1624 committed Jan 26, 2022
1 parent 3adeccf commit 666b05c
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 21 deletions.
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 = { git = "https://github.com/SeaQL/sea-query.git", features = ["thread-safe"] }
sea-query = { git = "https://github.com/SeaQL/sea-query.git", branch = "date-time-local", 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
2 changes: 1 addition & 1 deletion sea-orm-macros/src/derives/entity_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ pub fn expand_derive_entity_model(data: Data, attrs: Vec<Attribute>) -> syn::Res
"DateTime" | "NaiveDateTime" => {
quote! { DateTime }
}
"DateTimeUtc" | "DateTimeWithTimeZone" => {
"DateTimeUtc" | "DateTimeLocal" | "DateTimeWithTimeZone" => {
quote! { TimestampWithTimeZone }
}
"Uuid" => quote! { Uuid },
Expand Down
4 changes: 4 additions & 0 deletions src/entity/active_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -612,6 +612,10 @@ impl_into_active_value!(crate::prelude::DateTimeWithTimeZone, Set);
#[cfg_attr(docsrs, doc(cfg(feature = "with-chrono")))]
impl_into_active_value!(crate::prelude::DateTimeUtc, Set);

#[cfg(feature = "with-chrono")]
#[cfg_attr(docsrs, doc(cfg(feature = "with-chrono")))]
impl_into_active_value!(crate::prelude::DateTimeLocal, 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
4 changes: 4 additions & 0 deletions src/entity/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ pub type DateTimeWithTimeZone = chrono::DateTime<chrono::FixedOffset>;
#[cfg(feature = "with-chrono")]
pub type DateTimeUtc = chrono::DateTime<chrono::Utc>;

/// Date time with local time zone
#[cfg(feature = "with-chrono")]
pub type DateTimeLocal = chrono::DateTime<chrono::Local>;

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

Expand Down
8 changes: 7 additions & 1 deletion src/executor/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,10 @@ try_getable_all!(chrono::NaiveDateTime);
try_getable_date_time!(chrono::DateTime<chrono::FixedOffset>);

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

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

#[cfg(feature = "with-rust_decimal")]
use rust_decimal::Decimal;
Expand Down Expand Up @@ -620,6 +623,9 @@ try_from_u64_err!(chrono::DateTime<chrono::FixedOffset>);
#[cfg(feature = "with-chrono")]
try_from_u64_err!(chrono::DateTime<chrono::Utc>);

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

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

Expand Down
4 changes: 2 additions & 2 deletions tests/common/features/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ pub mod active_enum;
pub mod active_enum_child;
pub mod applog;
pub mod byte_primary_key;
pub mod datetimeutc;
pub mod metadata;
pub mod repository;
pub mod satellite;
pub mod schema;
pub mod sea_orm_active_enums;
pub mod self_join;
Expand All @@ -13,9 +13,9 @@ pub use active_enum::Entity as ActiveEnum;
pub use active_enum_child::Entity as ActiveEnumChild;
pub use applog::Entity as Applog;
pub use byte_primary_key::Entity as BytePrimaryKey;
pub use datetimeutc::Entity as DateTimeUtcTest;
pub use metadata::Entity as Metadata;
pub use repository::Entity as Repository;
pub use satellite::Entity as Satellite;
pub use schema::*;
pub use sea_orm_active_enums::*;
pub use self_join::Entity as SelfJoin;
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
use sea_orm::entity::prelude::*;

#[derive(Clone, Debug, PartialEq, DeriveEntityModel)]
#[sea_orm(table_name = "satellites")]
#[sea_orm(table_name = "satellite")]
pub struct Model {
#[sea_orm(primary_key)]
pub id: i32,
pub satellite_name: String,
#[sea_orm(default_value = "2022-01-26 16:24:00")]
pub launch_date: DateTimeUtc,
#[sea_orm(default_value = "2022-01-26 16:24:00")]
pub deployment_date: DateTimeUtc,
pub deployment_date: DateTimeLocal,
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
Expand Down
12 changes: 6 additions & 6 deletions tests/common/features/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,32 +205,32 @@ pub async fn create_active_enum_child_table(db: &DbConn) -> Result<ExecResult, D

pub async fn create_satellites_table(db: &DbConn) -> Result<ExecResult, DbErr> {
let stmt = sea_query::Table::create()
.table(datetimeutc::Entity)
.table(satellite::Entity)
.col(
ColumnDef::new(datetimeutc::Column::Id)
ColumnDef::new(satellite::Column::Id)
.integer()
.not_null()
.auto_increment()
.primary_key(),
)
.col(
ColumnDef::new(datetimeutc::Column::SatelliteName)
ColumnDef::new(satellite::Column::SatelliteName)
.string()
.not_null(),
)
.col(
ColumnDef::new(datetimeutc::Column::LaunchDate)
ColumnDef::new(satellite::Column::LaunchDate)
.timestamp_with_time_zone()
.not_null()
.default("2022-01-26 16:24:00"),
)
.col(
ColumnDef::new(datetimeutc::Column::DeploymentDate)
ColumnDef::new(satellite::Column::DeploymentDate)
.timestamp_with_time_zone()
.not_null()
.default("2022-01-26 16:24:00"),
)
.to_owned();

create_table(db, &stmt, DateTimeUtcTest).await
create_table(db, &stmt, Satellite).await
}
13 changes: 5 additions & 8 deletions tests/timestamp_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,29 +31,26 @@ pub async fn create_applog(db: &DatabaseConnection) -> Result<(), DbErr> {
.exec(db)
.await?;

assert_eq!(log.id.clone(), res.last_insert_id);
assert_eq!(log.id, res.last_insert_id);
assert_eq!(Applog::find().one(db).await?, Some(log.clone()));

Ok(())
}

pub async fn create_satellites_log(db: &DatabaseConnection) -> Result<(), DbErr> {
let archive = datetimeutc::Model {
let archive = satellite::Model {
id: 1,
satellite_name: "Sea-00001-2022".to_owned(),
launch_date: "2022-01-07T12:11:23Z".parse().unwrap(),
deployment_date: "2022-01-07T12:11:23Z".parse().unwrap(),
};

let res = DateTimeUtcTest::insert(archive.clone().into_active_model())
let res = Satellite::insert(archive.clone().into_active_model())
.exec(db)
.await?;

assert_eq!(archive.id.clone(), res.last_insert_id);
assert_eq!(
DateTimeUtcTest::find().one(db).await?,
Some(archive.clone())
);
assert_eq!(archive.id, res.last_insert_id);
assert_eq!(Satellite::find().one(db).await?, Some(archive.clone()));

Ok(())
}

0 comments on commit 666b05c

Please sign in to comment.