From 6a52f3c2d1ff5af1cae1cd1f16b23a7f9df7f19e Mon Sep 17 00:00:00 2001 From: Miroito Date: Fri, 3 Mar 2023 15:08:34 +0100 Subject: [PATCH] add created_at column to transaction table --- Cargo.lock | 1 + client/src/api/types/transaction.rs | 4 +- server/migration/Cargo.toml | 6 +-- server/migration/src/lib.rs | 2 + ...20230303_132528_transactions_created_at.rs | 52 +++++++++++++++++++ server/src/model/transaction.rs | 1 + server/src/route/transaction.rs | 5 +- 7 files changed, 65 insertions(+), 6 deletions(-) create mode 100644 server/migration/src/m20230303_132528_transactions_created_at.rs diff --git a/Cargo.lock b/Cargo.lock index 09a39e0..d0fbe3c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2011,6 +2011,7 @@ name = "migration" version = "0.1.0" dependencies = [ "async-std", + "chrono", "sea-orm-migration", ] diff --git a/client/src/api/types/transaction.rs b/client/src/api/types/transaction.rs index a681b45..9a65061 100644 --- a/client/src/api/types/transaction.rs +++ b/client/src/api/types/transaction.rs @@ -1,4 +1,4 @@ -use chrono::NaiveDate; +use chrono::{NaiveDate, NaiveDateTime}; use serde::{Deserialize, Serialize}; use super::company::Company; @@ -17,6 +17,7 @@ pub struct Transaction { pub instrument: String, pub volume: i32, pub unit_price: f32, + pub created_at: NaiveDateTime, } #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] @@ -32,5 +33,6 @@ pub struct TransactionCompany { pub instrument: String, pub volume: i32, pub unit_price: f32, + pub created_at: NaiveDateTime, pub company: Company, } diff --git a/server/migration/Cargo.toml b/server/migration/Cargo.toml index 6d3180b..d3387e8 100644 --- a/server/migration/Cargo.toml +++ b/server/migration/Cargo.toml @@ -10,10 +10,8 @@ path = "src/lib.rs" [dependencies] async-std = { version = "^1", features = ["attributes", "tokio1"] } +chrono.workspace = true [dependencies.sea-orm-migration] version = "^0.10.0" -features = [ -"sqlx-mysql", -"runtime-tokio-rustls", -] +features = ["sqlx-mysql", "runtime-tokio-rustls"] diff --git a/server/migration/src/lib.rs b/server/migration/src/lib.rs index 86bb294..a98dfce 100644 --- a/server/migration/src/lib.rs +++ b/server/migration/src/lib.rs @@ -3,6 +3,7 @@ pub use sea_orm_migration::prelude::*; mod m20230112_115856_create_company_table; mod m20230112_160440_create_transaction_table; mod m20230119_112539_create_transactions_in_process_table; +mod m20230303_132528_transactions_created_at; pub struct Migrator; @@ -13,6 +14,7 @@ impl MigratorTrait for Migrator { Box::new(m20230112_115856_create_company_table::Migration), Box::new(m20230112_160440_create_transaction_table::Migration), Box::new(m20230119_112539_create_transactions_in_process_table::Migration), + Box::new(m20230303_132528_transactions_created_at::Migration), ] } } diff --git a/server/migration/src/m20230303_132528_transactions_created_at.rs b/server/migration/src/m20230303_132528_transactions_created_at.rs new file mode 100644 index 0000000..ec3c20c --- /dev/null +++ b/server/migration/src/m20230303_132528_transactions_created_at.rs @@ -0,0 +1,52 @@ +use sea_orm_migration::prelude::*; + +#[derive(DeriveMigrationName)] +pub struct Migration; + +#[async_trait::async_trait] +impl MigrationTrait for Migration { + async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> { + manager + .alter_table( + Table::alter() + .table(Transaction::Table) + .add_column( + ColumnDef::new(Transaction::CreatedAt) + .date_time() + .not_null() + .default(Expr::current_timestamp()), + ) + .to_owned(), + ) + .await?; + + let query = Query::update() + .table(Transaction::Table) + .value( + Transaction::CreatedAt, + Expr::col(Transaction::DatePublished), + ) + .to_owned(); + + manager.exec_stmt(query).await + } + + async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> { + manager + .alter_table( + Table::alter() + .table(Transaction::Table) + .drop_column(Transaction::CreatedAt) + .to_owned(), + ) + .await + } +} + +/// Learn more at https://docs.rs/sea-query#iden +#[derive(Iden)] +enum Transaction { + Table, + DatePublished, + CreatedAt, +} diff --git a/server/src/model/transaction.rs b/server/src/model/transaction.rs index 55ea84c..1759ab8 100644 --- a/server/src/model/transaction.rs +++ b/server/src/model/transaction.rs @@ -21,6 +21,7 @@ pub struct Model { pub instrument: String, pub volume: i32, pub unit_price: f32, + pub created_at: DateTime, } #[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] diff --git a/server/src/route/transaction.rs b/server/src/route/transaction.rs index 6863229..67206fe 100644 --- a/server/src/route/transaction.rs +++ b/server/src/route/transaction.rs @@ -1,4 +1,4 @@ -use chrono::NaiveDate; +use chrono::{NaiveDate, NaiveDateTime}; use rocket::http::Status; use rocket::response::status::Custom; use sea_orm::{ColumnTrait, Order}; @@ -52,6 +52,7 @@ pub async fn get_all( instrument: t.0.instrument.to_owned(), volume: t.0.volume, unit_price: t.0.unit_price, + created_at: t.0.created_at, company: t.1.to_owned(), }) .collect(); @@ -110,6 +111,7 @@ pub async fn get_by_company_id( instrument: t.0.instrument.to_owned(), volume: t.0.volume, unit_price: t.0.unit_price, + created_at: t.0.created_at, company: t.1.to_owned(), }) .collect(); @@ -136,5 +138,6 @@ pub struct TransactionCompany { pub instrument: String, pub volume: i32, pub unit_price: f32, + pub created_at: NaiveDateTime, pub company: Option, }