monzo-ingestion/migration/src/m20230904_141851_create_monzo_tables.rs
2024-05-28 15:48:55 +01:00

102 lines
3.0 KiB
Rust

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
.create_table(
Table::create()
.table(Transaction::Table)
.if_not_exists()
.col(
ColumnDef::new(Transaction::Id)
.string()
.not_null()
.primary_key(),
)
.col(
ColumnDef::new(Transaction::TransactionType)
.string()
.not_null(),
)
.col(
ColumnDef::new(Transaction::TotalAmount)
.decimal()
.not_null(),
)
.col(
ColumnDef::new(Transaction::Timestamp)
.timestamp()
.not_null(),
)
.col(ColumnDef::new(Transaction::Title).string().not_null())
.col(ColumnDef::new(Transaction::Emoji).string())
.col(ColumnDef::new(Transaction::Notes).string())
.col(ColumnDef::new(Transaction::Receipt).string())
.col(ColumnDef::new(Transaction::Description).string())
.to_owned(),
)
.await?;
manager
.create_table(
Table::create()
.table(Expenditure::Table)
.if_not_exists()
.col(
ColumnDef::new(Expenditure::TransactionId)
.string()
.not_null(),
)
.col(ColumnDef::new(Expenditure::Category).string().not_null())
.primary_key(
Index::create()
.col(Expenditure::TransactionId)
.col(Expenditure::Category),
)
.col(ColumnDef::new(Expenditure::Amount).decimal().not_null())
.to_owned(),
)
.await?;
Ok(())
}
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.drop_table(Table::drop().table(Expenditure::Table).to_owned())
.await?;
manager
.drop_table(Table::drop().table(Transaction::Table).to_owned())
.await?;
Ok(())
}
}
#[derive(DeriveIden)]
enum Transaction {
Table,
Id,
TransactionType,
Timestamp,
Title,
Emoji,
Notes,
Receipt,
TotalAmount,
Description,
}
#[derive(DeriveIden)]
enum Expenditure {
Table,
Category,
Amount,
TransactionId,
}