Files
expenses/server/src/database.rs
T

149 lines
4.5 KiB
Rust

pub mod access_tokens;
pub mod account_members;
pub mod accounts;
pub mod dtos;
mod error_codes;
pub mod recurring_transactions;
pub mod refresh_tokens;
pub mod transactions;
pub mod user_app_secrets;
pub mod user_apps;
pub mod users;
use error_codes::{PG_CHECK_VIOLATION, PG_FOREIGN_KEY_VIOLATION, PG_UNIQUE_VIOLATION};
#[derive(Debug, thiserror::Error)]
pub enum DatabaseError {
#[error("check violation ({constraint}): {source}")]
CheckViolation {
constraint: Constraint,
#[source]
source: sqlx::Error,
},
#[error("foreign key violation ({constraint}): {source}")]
ForeignKeyViolation {
constraint: Constraint,
#[source]
source: sqlx::Error,
},
#[error("unique violation ({constraint}): {source}")]
UniqueViolation {
constraint: Constraint,
#[source]
source: sqlx::Error,
},
#[error("internal error: {0}")]
Internal(#[source] sqlx::Error),
}
impl From<sqlx::Error> for DatabaseError {
fn from(err: sqlx::Error) -> Self {
match err {
sqlx::Error::Database(db_err)
if db_err.code().as_deref() == Some(PG_CHECK_VIOLATION) =>
{
let constraint = db_err
.constraint()
.map(Constraint::from)
.unwrap_or(Constraint::Unnamed);
DatabaseError::CheckViolation {
constraint,
source: sqlx::Error::Database(db_err),
}
}
sqlx::Error::Database(db_err)
if db_err.code().as_deref() == Some(PG_FOREIGN_KEY_VIOLATION) =>
{
let constraint = db_err
.constraint()
.map(Constraint::from)
.unwrap_or(Constraint::Unnamed);
DatabaseError::ForeignKeyViolation {
constraint,
source: sqlx::Error::Database(db_err),
}
}
sqlx::Error::Database(db_err)
if db_err.code().as_deref() == Some(PG_UNIQUE_VIOLATION) =>
{
let constraint = db_err
.constraint()
.map(Constraint::from)
.unwrap_or(Constraint::Unnamed);
DatabaseError::UniqueViolation {
constraint,
source: sqlx::Error::Database(db_err),
}
}
_ => DatabaseError::Internal(err),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
pub enum Constraint {
#[error("account member account constraint")]
AccountMemberAccount,
#[error("account member account user constraint")]
AccountMemberAccountUser,
#[error("account member user constraint")]
AccountMemberUser,
#[error("account member last member constraint")]
AccountMemberLastMember,
#[error("user app user constraint")]
UserAppUser,
#[error("user app secret app constraint")]
UserAppSecretApp,
#[error("access token user constraint")]
AccessTokenUser,
#[error("access token user app constraint")]
AccessTokenUserApp,
#[error("refresh token access token constraint")]
RefreshTokenAccessToken,
#[error("transaction account constraint")]
TransactionAccount,
#[error("recurring transaction account constraint")]
RecurringTransactionAccount,
#[error("unknown constraint ({0:?})")]
Unknown(String),
#[error("unnamed constraint")]
Unnamed,
}
impl From<&str> for Constraint {
fn from(constraint: &str) -> Self {
match constraint {
"fk_access_tokens_user" => Constraint::AccessTokenUser,
"fk_access_tokens_app" => Constraint::AccessTokenUserApp,
"fk_account_members_account" => Constraint::AccountMemberAccount,
"uq_account_members_account_user" => Constraint::AccountMemberAccountUser,
"fk_account_members_user" => Constraint::AccountMemberUser,
"ck_account_members_last_member" => Constraint::AccountMemberLastMember,
"fk_refresh_tokens_access_token" => Constraint::RefreshTokenAccessToken,
"fk_user_apps_user" => Constraint::UserAppUser,
"fk_user_app_secrets_app" => Constraint::UserAppSecretApp,
"fk_transactions_account" => Constraint::TransactionAccount,
"fk_recurring_transactions_account" => Constraint::RecurringTransactionAccount,
_ => Constraint::Unknown(constraint.to_string()),
}
}
}