From f7e96b3d72dfff2d9764c97715d7c845f43e6515 Mon Sep 17 00:00:00 2001 From: Billy Chan Date: Mon, 27 Sep 2021 18:35:42 +0800 Subject: [PATCH] Support `chrono::NaiveDate` --- sea-orm-macros/src/derives/entity_model.rs | 2 +- src/entity/prelude.rs | 3 +++ src/executor/query.rs | 3 +++ tests/common/bakery_chain/metadata.rs | 1 + tests/common/setup/schema.rs | 1 + tests/parallel_tests.rs | 3 +++ tests/uuid_tests.rs | 1 + 7 files changed, 13 insertions(+), 1 deletion(-) diff --git a/sea-orm-macros/src/derives/entity_model.rs b/sea-orm-macros/src/derives/entity_model.rs index f0772763..ea9d05f5 100644 --- a/sea-orm-macros/src/derives/entity_model.rs +++ b/sea-orm-macros/src/derives/entity_model.rs @@ -170,7 +170,7 @@ pub fn expand_derive_entity_model(data: Data, attrs: Vec) -> syn::Res "f32" => quote! { Float }, "f64" => quote! { Double }, "bool" => quote! { Boolean }, - "NaiveDate" => quote! { Date }, + "Date" | "NaiveDate" => quote! { Date }, "NaiveTime" => quote! { Time }, "DateTime" | "NaiveDateTime" => { quote! { DateTime } diff --git a/src/entity/prelude.rs b/src/entity/prelude.rs index 8d87a4b2..221736f7 100644 --- a/src/entity/prelude.rs +++ b/src/entity/prelude.rs @@ -9,6 +9,9 @@ pub use crate::{ #[cfg(feature = "with-json")] pub use serde_json::Value as Json; +#[cfg(feature = "with-chrono")] +pub use chrono::NaiveDate as Date; + #[cfg(feature = "with-chrono")] pub use chrono::NaiveDateTime as DateTime; diff --git a/src/executor/query.rs b/src/executor/query.rs index ae09f97c..483d7d54 100644 --- a/src/executor/query.rs +++ b/src/executor/query.rs @@ -241,6 +241,9 @@ try_getable_all!(Vec); #[cfg(feature = "with-json")] try_getable_all!(serde_json::Value); +#[cfg(feature = "with-chrono")] +try_getable_all!(chrono::NaiveDate); + #[cfg(feature = "with-chrono")] try_getable_all!(chrono::NaiveDateTime); diff --git a/tests/common/bakery_chain/metadata.rs b/tests/common/bakery_chain/metadata.rs index 95a7a48b..c7727fb8 100644 --- a/tests/common/bakery_chain/metadata.rs +++ b/tests/common/bakery_chain/metadata.rs @@ -8,6 +8,7 @@ pub struct Model { pub key: String, pub value: String, pub bytes: Vec, + pub date: Date, } #[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] diff --git a/tests/common/setup/schema.rs b/tests/common/setup/schema.rs index 64f31dfe..0787e971 100644 --- a/tests/common/setup/schema.rs +++ b/tests/common/setup/schema.rs @@ -286,6 +286,7 @@ pub async fn create_metadata_table(db: &DbConn) -> Result { .col(ColumnDef::new(metadata::Column::Key).string().not_null()) .col(ColumnDef::new(metadata::Column::Value).string().not_null()) .col(ColumnDef::new(metadata::Column::Bytes).binary().not_null()) + .col(ColumnDef::new(metadata::Column::Date).date().not_null()) .to_owned(); create_table(db, &stmt, Metadata).await diff --git a/tests/parallel_tests.rs b/tests/parallel_tests.rs index 0ac09fd6..d83c7ab5 100644 --- a/tests/parallel_tests.rs +++ b/tests/parallel_tests.rs @@ -25,18 +25,21 @@ pub async fn crud_in_parallel(db: &DatabaseConnection) -> Result<(), DbErr> { key: "markup".to_owned(), value: "1.18".to_owned(), bytes: vec![1, 2, 3], + date: Date::from_ymd(2021, 9, 27), }, metadata::Model { uuid: Uuid::new_v4(), key: "exchange_rate".to_owned(), value: "0.78".to_owned(), bytes: vec![1, 2, 3], + date: Date::from_ymd(2021, 9, 27), }, metadata::Model { uuid: Uuid::new_v4(), key: "service_charge".to_owned(), value: "1.1".to_owned(), bytes: vec![1, 2, 3], + date: Date::from_ymd(2021, 9, 27), }, ]; diff --git a/tests/uuid_tests.rs b/tests/uuid_tests.rs index e58daca4..c6edd2aa 100644 --- a/tests/uuid_tests.rs +++ b/tests/uuid_tests.rs @@ -23,6 +23,7 @@ pub async fn create_metadata(db: &DatabaseConnection) -> Result<(), DbErr> { key: "markup".to_owned(), value: "1.18".to_owned(), bytes: vec![1, 2, 3], + date: Date::from_ymd(2021, 9, 27), }; let res = Metadata::insert(metadata.clone().into_active_model())