feat: init book handler

This commit is contained in:
brian
2025-06-08 20:42:24 +08:00
parent 50913eecbb
commit 27c94f4276
26 changed files with 4307 additions and 3 deletions

22
migration/Cargo.toml Normal file
View File

@@ -0,0 +1,22 @@
[package]
name = "migration"
version = "0.1.0"
edition = "2021"
publish = false
[lib]
name = "migration"
path = "src/lib.rs"
[dependencies]
async-std = { version = "1", features = ["attributes", "tokio1"] }
[dependencies.sea-orm-migration]
version = "1.1.12"
features = [
# Enable at least one `ASYNC_RUNTIME` and `DATABASE_DRIVER` feature if you want to run migration via CLI.
# View the list of supported features at https://www.sea-ql.org/SeaORM/docs/install-and-config/database-and-async-runtime.
# e.g.
"runtime-tokio-rustls", # `ASYNC_RUNTIME` feature
"sqlx-postgres", # `DATABASE_DRIVER` feature
]

41
migration/README.md Normal file
View File

@@ -0,0 +1,41 @@
# Running Migrator CLI
- Generate a new migration file
```sh
cargo run -- generate MIGRATION_NAME
```
- Apply all pending migrations
```sh
cargo run
```
```sh
cargo run -- up
```
- Apply first 10 pending migrations
```sh
cargo run -- up -n 10
```
- Rollback last applied migrations
```sh
cargo run -- down
```
- Rollback last 10 applied migrations
```sh
cargo run -- down -n 10
```
- Drop all tables from the database, then reapply all migrations
```sh
cargo run -- fresh
```
- Rollback all applied migrations, then reapply all migrations
```sh
cargo run -- refresh
```
- Rollback all applied migrations
```sh
cargo run -- reset
```
- Check the status of all migrations
```sh
cargo run -- status
```

22
migration/src/lib.rs Normal file
View File

@@ -0,0 +1,22 @@
pub use sea_orm_migration::prelude::*;
mod m20250525_000001_create_ledger_table_category;
mod m20250525_000002_create_ledger_table_book;
mod m20250525_000003_create_ledger_table_tag;
mod m20250525_000004_create_ledger_table_account;
mod m20250525_000005_create_ledger_table_transaction;
pub struct Migrator;
#[async_trait::async_trait]
impl MigratorTrait for Migrator {
fn migrations() -> Vec<Box<dyn MigrationTrait>> {
vec![
Box::new(m20250525_000001_create_ledger_table_category::Migration),
Box::new(m20250525_000002_create_ledger_table_book::Migration),
Box::new(m20250525_000003_create_ledger_table_tag::Migration),
Box::new(m20250525_000004_create_ledger_table_account::Migration),
Box::new(m20250525_000005_create_ledger_table_transaction::Migration),
]
}
}

View File

@@ -0,0 +1,64 @@
use sea_orm_migration::prelude::*;
pub struct Migration;
impl MigrationName for Migration {
fn name(&self) -> &str {
"m20250525_000001_create_ledger_table_category" // Make sure this matches with the file name
}
}
#[async_trait::async_trait]
impl MigrationTrait for Migration {
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.create_table(
Table::create()
.table(Category::Table)
.col(
ColumnDef::new(Category::Id)
.big_integer()
.not_null()
.auto_increment()
.primary_key(),
)
.col(ColumnDef::new(Category::Name).string().not_null())
.col(ColumnDef::new(Category::Uid).big_integer().not_null())
.col(ColumnDef::new(Category::ParentId).big_integer().default(0i64).not_null())
.col(ColumnDef::new(Category::IsDeleted).boolean().default(false).not_null())
.col(
ColumnDef::new(Category::CreatedAt)
.date_time()
.default(Expr::current_timestamp())
.not_null(),
)
.col(
ColumnDef::new(Category::UpdatedAt)
.date_time()
.default(Expr::current_timestamp())
.not_null(),
)
.to_owned(),
)
.await
}
// Define how to rollback this migration: Drop the Bakery table.
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.drop_table(Table::drop().table(Category::Table).to_owned())
.await
}
}
#[derive(Iden)]
pub enum Category {
Table,
Id,
Name,
Uid,
ParentId,
IsDeleted,
CreatedAt,
UpdatedAt,
}

View File

@@ -0,0 +1,63 @@
use sea_orm_migration::prelude::*;
pub struct Migration;
impl MigrationName for Migration {
fn name(&self) -> &str {
"m20250525_000002_create_ledger_table_book" // Make sure this matches with the file name
}
}
#[async_trait::async_trait]
impl MigrationTrait for Migration {
// Define how to apply this migration
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.create_table(
Table::create()
.table(Book::Table)
.col(
ColumnDef::new(Book::Id)
.big_integer()
.not_null()
.auto_increment()
.primary_key(),
)
.col(ColumnDef::new(Book::Name).string().not_null())
.col(ColumnDef::new(Book::Uid).big_integer().not_null())
.col(ColumnDef::new(Book::IsDeleted).boolean().default(false).not_null())
.col(
ColumnDef::new(Book::CreatedAt)
.date_time()
.default(Expr::current_timestamp())
.not_null(),
)
.col(
ColumnDef::new(Book::UpdatedAt)
.date_time()
.default(Expr::current_timestamp())
.not_null(),
)
.to_owned(),
)
.await
}
// Define how to rollback this migration: Drop the Bakery table.
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.drop_table(Table::drop().table(Book::Table).to_owned())
.await
}
}
#[derive(Iden)]
pub enum Book {
Table,
Id,
Name,
Uid,
IsDeleted,
CreatedAt,
UpdatedAt,
}

View File

@@ -0,0 +1,62 @@
use sea_orm_migration::prelude::*;
pub struct Migration;
impl MigrationName for Migration {
fn name(&self) -> &str {
"m20250525_000001_create_ledger_table_tag" // Make sure this matches with the file name
}
}
#[async_trait::async_trait]
impl MigrationTrait for Migration {
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.create_table(
Table::create()
.table(Tag::Table)
.col(
ColumnDef::new(Tag::Id)
.big_integer()
.not_null()
.auto_increment()
.primary_key(),
)
.col(ColumnDef::new(Tag::Name).string().not_null())
.col(ColumnDef::new(Tag::Uid).big_integer().not_null())
.col(ColumnDef::new(Tag::IsDeleted).boolean().default(false).not_null())
.col(
ColumnDef::new(Tag::CreatedAt)
.date_time()
.default(Expr::current_timestamp())
.not_null(),
)
.col(
ColumnDef::new(Tag::UpdatedAt)
.date_time()
.default(Expr::current_timestamp())
.not_null(),
)
.to_owned(),
)
.await
}
// Define how to rollback this migration: Drop the Bakery table.
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.drop_table(Table::drop().table(Tag::Table).to_owned())
.await
}
}
#[derive(Iden)]
pub enum Tag {
Table,
Id,
Name,
Uid,
IsDeleted,
CreatedAt,
UpdatedAt,
}

View File

@@ -0,0 +1,64 @@
use sea_orm_migration::prelude::*;
pub struct Migration;
impl MigrationName for Migration {
fn name(&self) -> &str {
"m20250525_000004_create_ledger_table_account" // Make sure this matches with the file name
}
}
#[async_trait::async_trait]
impl MigrationTrait for Migration {
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.create_table(
Table::create()
.table(Account::Table)
.col(
ColumnDef::new(Account::Id)
.big_integer()
.not_null()
.auto_increment()
.primary_key(),
)
.col(ColumnDef::new(Account::Name).string().not_null())
.col(ColumnDef::new(Account::Type).integer().not_null())
.col(ColumnDef::new(Account::Uid).big_integer().not_null())
.col(ColumnDef::new(Account::IsDeleted).boolean().default(false).not_null())
.col(
ColumnDef::new(Account::CreatedAt)
.date_time()
.default(Expr::current_timestamp())
.not_null(),
)
.col(
ColumnDef::new(Account::UpdatedAt)
.date_time()
.default(Expr::current_timestamp())
.not_null(),
)
.to_owned(),
)
.await
}
// Define how to rollback this migration: Drop the Bakery table.
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.drop_table(Table::drop().table(Account::Table).to_owned())
.await
}
}
#[derive(Iden)]
pub enum Account {
Table,
Id,
Name,
Uid,
Type,
IsDeleted,
CreatedAt,
UpdatedAt,
}

View File

@@ -0,0 +1,84 @@
use sea_orm_migration::prelude::*;
pub struct Migration;
impl MigrationName for Migration {
fn name(&self) -> &str {
"m20250525_000005_create_ledger_table_transaction" // Make sure this matches with the file name
}
}
#[async_trait::async_trait]
impl MigrationTrait for Migration {
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.create_table(
Table::create()
.table(Transaction::Table)
.col(
ColumnDef::new(Transaction::Id)
.big_integer()
.not_null()
.auto_increment()
.primary_key(),
)
.col(ColumnDef::new(Transaction::Uid).big_integer().not_null())
.col(ColumnDef::new(Transaction::Type).integer().not_null())
.col(ColumnDef::new(Transaction::BookId).big_integer().not_null())
.col(
ColumnDef::new(Transaction::CategoryId)
.big_integer()
.not_null(),
)
.col(ColumnDef::new(Transaction::Description).string().not_null())
.col(
ColumnDef::new(Transaction::TransactionTime)
.timestamp_with_time_zone()
.default(Expr::current_timestamp())
.not_null(),
)
.col(
ColumnDef::new(Transaction::IsDeleted)
.boolean()
.default(false)
.not_null(),
)
.col(
ColumnDef::new(Transaction::CreatedAt)
.date_time()
.default(Expr::current_timestamp())
.not_null(),
)
.col(
ColumnDef::new(Transaction::UpdatedAt)
.date_time()
.default(Expr::current_timestamp())
.not_null(),
)
.to_owned(),
)
.await
}
// Define how to rollback this migration: Drop the Bakery table.
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.drop_table(Table::drop().table(Transaction::Table).to_owned())
.await
}
}
#[derive(Iden)]
pub enum Transaction {
Table,
Id,
Uid,
Type,
BookId,
CategoryId,
Description,
TransactionTime,
IsDeleted,
CreatedAt,
UpdatedAt,
}

6
migration/src/main.rs Normal file
View File

@@ -0,0 +1,6 @@
use sea_orm_migration::prelude::*;
#[async_std::main]
async fn main() {
cli::run_cli(migration::Migrator).await;
}