feat: add migration for transaction, category, and tag tables with foreign key constraints

This commit is contained in:
GW_MC
2026-05-28 04:00:01 +00:00
parent 1d5e24bdfb
commit ea16cc2d55
9 changed files with 517 additions and 2 deletions

View File

@@ -0,0 +1,39 @@
//! `SeaORM` Entity, @generated by sea-orm-codegen 2.0
use sea_orm::entity::prelude::*;
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel, Serialize, Deserialize)]
#[sea_orm(table_name = "category")]
#[allow(dead_code)]
pub struct Model {
#[sea_orm(primary_key)]
pub id: i64,
pub parent_id: Option<i64>,
pub name: String,
pub color_code: String,
pub created_at: String,
pub updated_at: String,
}
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {
#[sea_orm(
belongs_to = "Entity",
from = "Column::ParentId",
to = "Column::Id",
on_update = "Restrict",
on_delete = "NoAction"
)]
SelfRef,
#[sea_orm(has_many = "super::transaction::Entity")]
Transaction,
}
impl Related<super::transaction::Entity> for Entity {
fn to() -> RelationDef {
Relation::Transaction.def()
}
}
impl ActiveModelBehavior for ActiveModel {}

View File

@@ -4,3 +4,7 @@ pub mod prelude;
pub mod account;
pub mod account_category;
pub mod category;
pub mod tag;
pub mod transaction;
pub mod transaction_tag;

View File

@@ -4,3 +4,7 @@
pub use super::account::Entity as Account;
pub use super::account_category::Entity as AccountCategory;
pub use super::category::Entity as Category;
pub use super::tag::Entity as Tag;
pub use super::transaction::Entity as Transaction;
pub use super::transaction_tag::Entity as TransactionTag;

View File

@@ -0,0 +1,40 @@
//! `SeaORM` Entity, @generated by sea-orm-codegen 2.0
use sea_orm::entity::prelude::*;
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel, Serialize, Deserialize)]
#[sea_orm(table_name = "tag")]
#[allow(dead_code)]
pub struct Model {
#[sea_orm(primary_key)]
pub id: i64,
#[sea_orm(unique)]
pub name: String,
pub color_code: String,
pub created_at: String,
pub updated_at: String,
}
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {
#[sea_orm(has_many = "super::transaction_tag::Entity")]
TransactionTag,
}
impl Related<super::transaction_tag::Entity> for Entity {
fn to() -> RelationDef {
Relation::TransactionTag.def()
}
}
impl Related<super::transaction::Entity> for Entity {
fn to() -> RelationDef {
super::transaction_tag::Relation::Transaction.def()
}
fn via() -> Option<RelationDef> {
Some(super::transaction_tag::Relation::Tag.def().rev())
}
}
impl ActiveModelBehavior for ActiveModel {}

View File

@@ -0,0 +1,76 @@
//! `SeaORM` Entity, @generated by sea-orm-codegen 2.0
use sea_orm::entity::prelude::*;
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel, Serialize, Deserialize)]
#[sea_orm(table_name = "transaction")]
#[allow(dead_code)]
pub struct Model {
#[sea_orm(primary_key)]
pub id: i64,
pub amount: String,
pub transaction_type: String,
pub currency_code: String,
pub exchange_rate: Option<String>,
pub transaction_date: String,
pub metadata: Option<String>,
pub created_at: String,
pub updated_at: String,
pub account_id: i64,
pub from_account_id: Option<i64>,
pub category_id: Option<i64>,
}
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {
#[sea_orm(
belongs_to = "super::account::Entity",
from = "Column::FromAccountId",
to = "super::account::Column::Id",
on_update = "Restrict",
on_delete = "NoAction"
)]
Account2,
#[sea_orm(
belongs_to = "super::account::Entity",
from = "Column::AccountId",
to = "super::account::Column::Id",
on_update = "Restrict",
on_delete = "NoAction"
)]
Account1,
#[sea_orm(
belongs_to = "super::category::Entity",
from = "Column::CategoryId",
to = "super::category::Column::Id",
on_update = "Restrict",
on_delete = "NoAction"
)]
Category,
#[sea_orm(has_many = "super::transaction_tag::Entity")]
TransactionTag,
}
impl Related<super::category::Entity> for Entity {
fn to() -> RelationDef {
Relation::Category.def()
}
}
impl Related<super::transaction_tag::Entity> for Entity {
fn to() -> RelationDef {
Relation::TransactionTag.def()
}
}
impl Related<super::tag::Entity> for Entity {
fn to() -> RelationDef {
super::transaction_tag::Relation::Tag.def()
}
fn via() -> Option<RelationDef> {
Some(super::transaction_tag::Relation::Transaction.def().rev())
}
}
impl ActiveModelBehavior for ActiveModel {}

View File

@@ -0,0 +1,48 @@
//! `SeaORM` Entity, @generated by sea-orm-codegen 2.0
use sea_orm::entity::prelude::*;
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel, Serialize, Deserialize)]
#[sea_orm(table_name = "transaction_tag")]
#[allow(dead_code)]
pub struct Model {
#[sea_orm(primary_key, auto_increment = false)]
pub transaction_id: i64,
#[sea_orm(primary_key, auto_increment = false)]
pub tag_id: i64,
}
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {
#[sea_orm(
belongs_to = "super::tag::Entity",
from = "Column::TagId",
to = "super::tag::Column::Id",
on_update = "Restrict",
on_delete = "NoAction"
)]
Tag,
#[sea_orm(
belongs_to = "super::transaction::Entity",
from = "Column::TransactionId",
to = "super::transaction::Column::Id",
on_update = "Restrict",
on_delete = "NoAction"
)]
Transaction,
}
impl Related<super::tag::Entity> for Entity {
fn to() -> RelationDef {
Relation::Tag.def()
}
}
impl Related<super::transaction::Entity> for Entity {
fn to() -> RelationDef {
Relation::Transaction.def()
}
}
impl ActiveModelBehavior for ActiveModel {}

View File

@@ -3,9 +3,59 @@ pub mod entities;
use async_trait::async_trait;
use sea_orm::{
ConnectionTrait, DatabaseConnection, DatabaseTransaction, DbBackend, DbErr, ExecResult,
QueryResult, Statement, StatementBuilder,
QueryResult, SqlxError, Statement, StatementBuilder,
};
#[derive(thiserror::Error, Debug)]
pub enum DbServiceError {
#[error("unique constraint violation: {0}")]
UniqueConstraintViolation(String),
#[error("check constraint violation: {0}")]
CheckConstraintViolation(String),
#[error("foreign key constraint violation: {0}")]
ForeignKeyConstraintViolation(String),
// catchall
#[error("unknown database error: {0}")]
Unknown(String),
}
impl From<&SqlxError> for DbServiceError {
fn from(err: &SqlxError) -> Self {
match err {
SqlxError::Database(db_err) => {
if db_err.is_check_violation() {
DbServiceError::CheckConstraintViolation(db_err.to_string())
} else if db_err.is_unique_violation() {
DbServiceError::UniqueConstraintViolation(db_err.to_string())
} else if db_err.is_foreign_key_violation() {
DbServiceError::ForeignKeyConstraintViolation(db_err.to_string())
} else {
DbServiceError::Unknown(db_err.to_string())
}
}
other => DbServiceError::Unknown(other.to_string()),
}
}
}
impl From<sea_orm::RuntimeErr> for DbServiceError {
fn from(err: sea_orm::RuntimeErr) -> Self {
match err {
sea_orm::RuntimeErr::SqlxError(sqlx_err) => DbServiceError::from(&*sqlx_err),
other => DbServiceError::Unknown(other.to_string()),
}
}
}
impl From<DbErr> for DbServiceError {
fn from(err: DbErr) -> Self {
match err {
DbErr::Exec(exec_err) => DbServiceError::from(exec_err),
other => DbServiceError::Unknown(other.to_string()),
}
}
}
// 1. Create a uniform enum wrapper
pub enum Db<'a> {
Connection(&'a DatabaseConnection),