add created_at column to transaction table

pull/26/head
Miroito 3 years ago
parent 188e9efefb
commit 6a52f3c2d1

1
Cargo.lock generated

@ -2011,6 +2011,7 @@ name = "migration"
version = "0.1.0"
dependencies = [
"async-std",
"chrono",
"sea-orm-migration",
]

@ -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,
}

@ -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"]

@ -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),
]
}
}

@ -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,
}

@ -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)]

@ -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<model::company::Model>,
}

Loading…
Cancel
Save