This commit is contained in:
Billy Chan 2021-09-17 19:04:18 +08:00
parent 5509048550
commit 37d390e500
No known key found for this signature in database
GPG Key ID: A2D690CAC7DF3CC7
9 changed files with 89 additions and 3 deletions

View File

@ -30,7 +30,7 @@ futures-util = { version = "^0.3" }
log = { version = "^0.4", optional = true }
rust_decimal = { version = "^1", optional = true }
sea-orm-macros = { version = "^0.2", path = "sea-orm-macros", optional = true }
sea-query = { version = "^0.16.1", features = ["thread-safe"] }
sea-query = { version = "^0.16.2", git = "https://github.com/SeaQL/sea-query.git", branch = "pg-driver-timestamp-tz", features = ["thread-safe"] }
sea-strum = { version = "^0.21", features = ["derive", "sea-orm"] }
serde = { version = "^1.0", features = ["derive"] }
serde_json = { version = "^1", optional = true }

View File

@ -172,9 +172,12 @@ pub fn expand_derive_entity_model(data: Data, attrs: Vec<Attribute>) -> syn::Res
"bool" => quote! { Boolean },
"NaiveDate" => quote! { Date },
"NaiveTime" => quote! { Time },
"DateTime" | "NaiveDateTime" | "DateTimeWithTimeZone" => {
"DateTime" | "NaiveDateTime" => {
quote! { DateTime }
}
"DateTimeWithTimeZone" => {
quote! { TimestampWithTimeZone }
}
"Uuid" => quote! { Uuid },
"Json" => quote! { Json },
"Decimal" => quote! { Decimal },

View File

@ -24,6 +24,7 @@ pub enum ColumnType {
Decimal(Option<(u32, u32)>),
DateTime,
Timestamp,
TimestampWithTimeZone,
Time,
Date,
Binary,
@ -278,6 +279,7 @@ impl From<ColumnType> for sea_query::ColumnType {
ColumnType::Decimal(s) => sea_query::ColumnType::Decimal(s),
ColumnType::DateTime => sea_query::ColumnType::DateTime(None),
ColumnType::Timestamp => sea_query::ColumnType::Timestamp(None),
ColumnType::TimestampWithTimeZone => sea_query::ColumnType::TimestampWithTimeZone(None),
ColumnType::Time => sea_query::ColumnType::Time(None),
ColumnType::Date => sea_query::ColumnType::Date,
ColumnType::Binary => sea_query::ColumnType::Binary(None),
@ -309,6 +311,7 @@ impl From<sea_query::ColumnType> for ColumnType {
sea_query::ColumnType::Decimal(s) => Self::Decimal(s),
sea_query::ColumnType::DateTime(_) => Self::DateTime,
sea_query::ColumnType::Timestamp(_) => Self::Timestamp,
sea_query::ColumnType::TimestampWithTimeZone(_) => Self::TimestampWithTimeZone,
sea_query::ColumnType::Time(_) => Self::Time,
sea_query::ColumnType::Date => Self::Date,
sea_query::ColumnType::Binary(_) => Self::Binary,

View File

@ -0,0 +1,21 @@
use sea_orm::entity::prelude::*;
#[derive(Clone, Debug, PartialEq, DeriveEntityModel)]
#[sea_orm(table_name = "log")]
pub struct Model {
#[sea_orm(primary_key)]
pub id: i32,
pub json: Json,
pub created_at: DateTimeWithTimeZone,
}
#[derive(Copy, Clone, Debug, EnumIter)]
pub enum Relation {}
impl RelationTrait for Relation {
fn def(&self) -> RelationDef {
unreachable!()
}
}
impl ActiveModelBehavior for ActiveModel {}

View File

@ -1,5 +1,4 @@
use sea_orm::entity::prelude::*;
use uuid::Uuid;
#[derive(Clone, Debug, PartialEq, DeriveEntityModel)]
#[sea_orm(table_name = "metadata")]

View File

@ -4,6 +4,7 @@ pub mod cake;
pub mod cakes_bakers;
pub mod customer;
pub mod lineitem;
pub mod log;
pub mod metadata;
pub mod order;
@ -13,5 +14,6 @@ pub use super::cake::Entity as Cake;
pub use super::cakes_bakers::Entity as CakesBakers;
pub use super::customer::Entity as Customer;
pub use super::lineitem::Entity as Lineitem;
pub use super::log::Entity as Log;
pub use super::metadata::Entity as Metadata;
pub use super::order::Entity as Order;

View File

@ -53,6 +53,7 @@ pub async fn setup(base_url: &str, db_name: &str) -> DatabaseConnection {
schema::create_cakes_bakers_table(&db).await.unwrap();
schema::create_lineitem_table(&db).await.unwrap();
schema::create_metadata_table(&db).await.unwrap();
schema::create_log_table(&db).await.unwrap();
db
}

View File

@ -286,3 +286,25 @@ pub async fn create_metadata_table(db: &DbConn) -> Result<ExecResult, DbErr> {
create_table(db, &stmt, Metadata).await
}
pub async fn create_log_table(db: &DbConn) -> Result<ExecResult, DbErr> {
let stmt = sea_query::Table::create()
.table(log::Entity)
.if_not_exists()
.col(
ColumnDef::new(log::Column::Id)
.integer()
.not_null()
.auto_increment()
.primary_key(),
)
.col(ColumnDef::new(log::Column::Json).json().not_null())
.col(
ColumnDef::new(log::Column::CreatedAt)
.timestamp_with_time_zone()
.not_null(),
)
.to_owned();
create_table(db, &stmt, Log).await
}

35
tests/timestamp_tests.rs Normal file
View File

@ -0,0 +1,35 @@
pub mod common;
pub use common::{bakery_chain::*, setup::*, TestContext};
use sea_orm::{entity::prelude::*, DatabaseConnection, IntoActiveModel};
#[sea_orm_macros::test]
#[cfg(feature = "sqlx-postgres")]
async fn main() -> Result<(), DbErr> {
let ctx = TestContext::new("bakery_chain_schema_timestamp_tests").await;
create_log(&ctx.db).await?;
ctx.delete().await;
Ok(())
}
pub async fn create_log(db: &DatabaseConnection) -> Result<(), DbErr> {
let log = log::Model {
id: 1,
json: Json::String("HI".to_owned()),
created_at: "2021-09-17T17:50:20+08:00".parse().unwrap(),
};
let res = Log::insert(log.clone().into_active_model())
.exec(db)
.await?;
assert_eq!(log.id.clone(), res.last_insert_id);
assert_eq!(Log::find().one(db).await?, Some(log.clone()));
assert!(false);
Ok(())
}