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

time support #508

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
7 changes: 5 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,12 @@ futures-util = { version = "^0.3" }
tracing = { version = "0.1", features = ["log"] }
rust_decimal = { version = "^1", optional = true }
sea-orm-macros = { version = "^0.6.0", path = "sea-orm-macros", optional = true }
sea-query = { version = "^0.21.0", features = ["thread-safe"] }
sea-query = { git = "https://github.com/cemoktra/sea-query.git", branch = "with-time", features = ["thread-safe"] }
sea-strum = { version = "^0.23", features = ["derive", "sea-orm"] }
serde = { version = "^1.0", features = ["derive"] }
serde_json = { version = "^1", optional = true }
sqlx = { version = "^0.5", optional = true }
time = { version = "^0.3", features = ["parsing", "macros"], optional = true }
uuid = { version = "0.8", features = ["serde", "v4"], optional = true }
ouroboros = "0.14"
url = "^2.2"
Expand All @@ -58,7 +59,7 @@ default = [
"macros",
"mock",
"with-json",
"with-chrono",
"with-time",
"with-rust_decimal",
"with-uuid",
]
Expand All @@ -67,6 +68,7 @@ mock = []
with-json = ["serde_json", "sea-query/with-json", "chrono/serde"]
with-chrono = ["chrono", "sea-query/with-chrono"]
with-rust_decimal = ["rust_decimal", "sea-query/with-rust_decimal"]
with-time = ["time", "sea-query/with-time"]
with-uuid = ["uuid", "sea-query/with-uuid"]
sqlx-all = ["sqlx-mysql", "sqlx-postgres", "sqlx-sqlite"]
sqlx-dep = ["sqlx-json", "sqlx-chrono", "sqlx-decimal", "sqlx-uuid"]
Expand All @@ -77,6 +79,7 @@ sqlx-uuid = ["sqlx/uuid", "with-uuid"]
sqlx-mysql = ["sqlx-dep", "sea-query/sqlx-mysql", "sqlx/mysql"]
sqlx-postgres = ["sqlx-dep", "sea-query/sqlx-postgres", "sqlx/postgres"]
sqlx-sqlite = ["sqlx-dep", "sea-query/sqlx-sqlite", "sqlx/sqlite"]
sqlx-time = ["sqlx/time", "with-time"]
runtime-async-std = []
runtime-async-std-native-tls = [
"sqlx/runtime-async-std-native-tls",
Expand Down
16 changes: 16 additions & 0 deletions src/entity/active_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -640,6 +640,22 @@ impl_into_active_value!(crate::prelude::DateTimeUtc, Set);
#[cfg_attr(docsrs, doc(cfg(feature = "with-chrono")))]
impl_into_active_value!(crate::prelude::DateTimeLocal, Set);

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

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

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

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

#[cfg(feature = "with-time")]
pub use time::Date;

#[cfg(feature = "with-time")]
pub use time::Time;

#[cfg(feature = "with-time")]
pub use time::PrimitiveDateTime as DateTime;

/// Handles the time and dates
#[cfg(feature = "with-time")]
pub type DateTimeWithTimeZone = time::OffsetDateTime;

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

Expand Down
24 changes: 24 additions & 0 deletions src/executor/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,18 @@ try_getable_all!(chrono::DateTime<chrono::Utc>);
#[cfg(feature = "with-chrono")]
try_getable_all!(chrono::DateTime<chrono::Local>);

#[cfg(feature = "with-time")]
try_getable_all!(time::Date);

#[cfg(feature = "with-time")]
try_getable_all!(time::Time);

#[cfg(feature = "with-time")]
try_getable_all!(time::PrimitiveDateTime);

#[cfg(feature = "with-time")]
try_getable_date_time!(time::OffsetDateTime);

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

Expand Down Expand Up @@ -638,6 +650,18 @@ try_from_u64_err!(chrono::DateTime<chrono::Utc>);
#[cfg(feature = "with-chrono")]
try_from_u64_err!(chrono::DateTime<chrono::Local>);

#[cfg(feature = "with-time")]
try_from_u64_err!(time::Date);

#[cfg(feature = "with-time")]
try_from_u64_err!(time::Time);

#[cfg(feature = "with-time")]
try_from_u64_err!(time::PrimitiveDateTime);

#[cfg(feature = "with-time")]
try_from_u64_err!(time::OffsetDateTime);

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

Expand Down
14 changes: 14 additions & 0 deletions tests/common/features/satellite.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use sea_orm::entity::prelude::*;

#[cfg(feature = "with-chrono")]
#[derive(Clone, Debug, PartialEq, DeriveEntityModel)]
#[sea_orm(table_name = "satellite")]
pub struct Model {
Expand All @@ -12,6 +13,19 @@ pub struct Model {
pub deployment_date: DateTimeLocal,
}

#[cfg(feature = "with-time")]
#[derive(Clone, Debug, PartialEq, DeriveEntityModel)]
#[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 UTC")]
pub launch_date: DateTimeWithTimeZone,
#[sea_orm(default_value = "2022-01-26 16:24:00 +1")]
pub deployment_date: DateTimeWithTimeZone,
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {}

Expand Down
15 changes: 13 additions & 2 deletions tests/crud/create_lineitem.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
pub use super::*;
use chrono::offset::Utc;
use rust_decimal_macros::dec;
use sea_orm::prelude::DateTime;
use uuid::Uuid;

#[cfg(feature = "with-chrono")]
fn utc_now() -> DateTime {
chrono::offset::Utc::now().naive_utc()
}

#[cfg(feature = "with-time")]
fn utc_now() -> DateTime {
let utc = time::OffsetDateTime::now_utc();
utc.date().with_time(utc.time())
}

pub async fn test_create_lineitem(db: &DbConn) {
// Bakery
let seaside_bakery = bakery::ActiveModel {
Expand Down Expand Up @@ -76,7 +87,7 @@ pub async fn test_create_lineitem(db: &DbConn) {
bakery_id: Set(bakery_insert_res.last_insert_id as i32),
customer_id: Set(customer_insert_res.last_insert_id as i32),
total: Set(dec!(7.55)),
placed_at: Set(Utc::now().naive_utc()),
placed_at: Set(utc_now()),
..Default::default()
};
let order_insert_res = Order::insert(order_1)
Expand Down
15 changes: 13 additions & 2 deletions tests/crud/create_order.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
pub use super::*;
use chrono::offset::Utc;
use rust_decimal_macros::dec;
use sea_orm::prelude::DateTime;
use uuid::Uuid;

#[cfg(feature = "with-chrono")]
fn utc_now() -> DateTime {
chrono::offset::Utc::now().naive_utc()
}

#[cfg(feature = "with-time")]
fn utc_now() -> DateTime {
let utc = time::OffsetDateTime::now_utc();
utc.date().with_time(utc.time())
}

pub async fn test_create_order(db: &DbConn) {
// Bakery
let seaside_bakery = bakery::ActiveModel {
Expand Down Expand Up @@ -76,7 +87,7 @@ pub async fn test_create_order(db: &DbConn) {
bakery_id: Set(bakery_insert_res.last_insert_id as i32),
customer_id: Set(customer_insert_res.last_insert_id as i32),
total: Set(dec!(15.10)),
placed_at: Set(Utc::now().naive_utc()),
placed_at: Set(utc_now()),
..Default::default()
};
let order_insert_res = Order::insert(order_1)
Expand Down
100 changes: 100 additions & 0 deletions tests/parallel_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ async fn main() -> Result<(), DbErr> {
Ok(())
}

#[cfg(feature = "with-chrono")]
pub async fn crud_in_parallel(db: &DatabaseConnection) -> Result<(), DbErr> {
let metadata = vec![
metadata::Model {
Expand Down Expand Up @@ -116,3 +117,102 @@ pub async fn crud_in_parallel(db: &DatabaseConnection) -> Result<(), DbErr> {

Ok(())
}

#[cfg(feature = "with-time")]
pub async fn crud_in_parallel(db: &DatabaseConnection) -> Result<(), DbErr> {
let metadata = vec![
metadata::Model {
uuid: Uuid::new_v4(),
ty: "Type".to_owned(),
key: "markup".to_owned(),
value: "1.18".to_owned(),
bytes: vec![1, 2, 3],
date: Some(Date::from_calendar_date(2021, time::Month::September, 27).unwrap()),
time: Some(Time::from_hms(11, 32, 55).unwrap()),
},
metadata::Model {
uuid: Uuid::new_v4(),
ty: "Type".to_owned(),
key: "exchange_rate".to_owned(),
value: "0.78".to_owned(),
bytes: vec![1, 2, 3],
date: Some(Date::from_calendar_date(2021, time::Month::September, 27).unwrap()),
time: Some(Time::from_hms(11, 32, 55).unwrap()),
},
metadata::Model {
uuid: Uuid::new_v4(),
ty: "Type".to_owned(),
key: "service_charge".to_owned(),
value: "1.1".to_owned(),
bytes: vec![1, 2, 3],
date: None,
time: None,
},
];

let _insert_res = futures::try_join!(
metadata[0].clone().into_active_model().insert(db),
metadata[1].clone().into_active_model().insert(db),
metadata[2].clone().into_active_model().insert(db),
)?;

let find_res = futures::try_join!(
Metadata::find_by_id(metadata[0].uuid).one(db),
Metadata::find_by_id(metadata[1].uuid).one(db),
Metadata::find_by_id(metadata[2].uuid).one(db),
)?;

assert_eq!(
metadata,
vec![
find_res.0.clone().unwrap(),
find_res.1.clone().unwrap(),
find_res.2.clone().unwrap(),
]
);

let mut active_models = (
find_res.0.unwrap().into_active_model(),
find_res.1.unwrap().into_active_model(),
find_res.2.unwrap().into_active_model(),
);

active_models.0.bytes = Set(vec![0]);
active_models.1.bytes = Set(vec![1]);
active_models.2.bytes = Set(vec![2]);

let _update_res = futures::try_join!(
active_models.0.clone().update(db),
active_models.1.clone().update(db),
active_models.2.clone().update(db),
)?;

let find_res = futures::try_join!(
Metadata::find_by_id(metadata[0].uuid).one(db),
Metadata::find_by_id(metadata[1].uuid).one(db),
Metadata::find_by_id(metadata[2].uuid).one(db),
)?;

assert_eq!(
vec![
active_models.0.bytes.clone().unwrap(),
active_models.1.bytes.clone().unwrap(),
active_models.2.bytes.clone().unwrap(),
],
vec![
find_res.0.clone().unwrap().bytes,
find_res.1.clone().unwrap().bytes,
find_res.2.clone().unwrap().bytes,
]
);

let _delete_res = futures::try_join!(
active_models.0.delete(db),
active_models.1.delete(db),
active_models.2.delete(db),
)?;

assert_eq!(Metadata::find().all(db).await?, vec![]);

Ok(())
}
71 changes: 71 additions & 0 deletions tests/self_join_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ async fn main() -> Result<(), DbErr> {
Ok(())
}

#[cfg(feature = "with-chrono")]
pub async fn create_metadata(db: &DatabaseConnection) -> Result<(), DbErr> {
let model = self_join::Model {
uuid: Uuid::new_v4(),
Expand Down Expand Up @@ -87,3 +88,73 @@ pub async fn create_metadata(db: &DatabaseConnection) -> Result<(), DbErr> {

Ok(())
}

#[cfg(feature = "with-time")]
pub async fn create_metadata(db: &DatabaseConnection) -> Result<(), DbErr> {
let model = self_join::Model {
uuid: Uuid::new_v4(),
uuid_ref: None,
time: Some(Time::from_hms(1, 00, 00).unwrap()),
};

model.clone().into_active_model().insert(db).await?;

let linked_model = self_join::Model {
uuid: Uuid::new_v4(),
uuid_ref: Some(model.clone().uuid),
time: Some(Time::from_hms(2, 00, 00).unwrap()),
};

linked_model.clone().into_active_model().insert(db).await?;

let not_linked_model = self_join::Model {
uuid: Uuid::new_v4(),
uuid_ref: None,
time: Some(Time::from_hms(3, 00, 00).unwrap()),
};

not_linked_model
.clone()
.into_active_model()
.insert(db)
.await?;

assert_eq!(
model
.find_linked(self_join::SelfReferencingLink)
.all(db)
.await?,
vec![]
);

assert_eq!(
linked_model
.find_linked(self_join::SelfReferencingLink)
.all(db)
.await?,
vec![model.clone()]
);

assert_eq!(
not_linked_model
.find_linked(self_join::SelfReferencingLink)
.all(db)
.await?,
vec![]
);

assert_eq!(
self_join::Entity::find()
.find_also_linked(self_join::SelfReferencingLink)
.order_by_asc(self_join::Column::Time)
.all(db)
.await?,
vec![
(model.clone(), None),
(linked_model, Some(model)),
(not_linked_model, None),
]
);

Ok(())
}
Loading