Merge pull request #173 from SeaQL/pg-timestamp-tz

`TimestampWithTimeZone` Fixups
This commit is contained in:
Chris Tsang 2021-09-17 23:38:47 +08:00 committed by GitHub
commit 89661ae3d0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 89 additions and 4 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.3", 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

@ -22,7 +22,7 @@ clap = { version = "^2.33.3" }
dotenv = { version = "^0.15" }
async-std = { version = "^1.9", features = [ "attributes" ] }
sea-orm-codegen = { version = "^0.2.0", path = "../sea-orm-codegen" }
sea-schema = { version = "^0.2.8", git = "https://github.com/SeaQL/sea-schema.git", default-features = false, features = [
sea-schema = { version = "^0.2.8", default-features = false, features = [
"debug-print",
"sqlx-mysql",
"sqlx-postgres",

View File

@ -38,6 +38,7 @@ impl Column {
ColumnType::Double(_) => "f64",
ColumnType::Json | ColumnType::JsonBinary => "Json",
ColumnType::DateTime(_) | ColumnType::Timestamp(_) => "DateTime",
ColumnType::TimestampWithTimeZone(_) => "DateTimeWithTimeZone",
ColumnType::Decimal(_) | ColumnType::Money(_) => "Decimal",
ColumnType::Uuid => "Uuid",
ColumnType::Binary(_) => "Vec<u8>",

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
}

33
tests/timestamp_tests.rs Normal file
View File

@ -0,0 +1,33 @@
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()));
Ok(())
}