Perform last member removal check on the database.
This commit is contained in:
+25
-1
@@ -9,10 +9,17 @@ pub mod user_app_secrets;
|
||||
pub mod user_apps;
|
||||
pub mod users;
|
||||
|
||||
use error_codes::{PG_FOREIGN_KEY_VIOLATION, PG_UNIQUE_VIOLATION};
|
||||
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,
|
||||
@@ -34,6 +41,19 @@ pub enum DatabaseError {
|
||||
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) =>
|
||||
{
|
||||
@@ -76,6 +96,9 @@ pub enum Constraint {
|
||||
#[error("account member user constraint")]
|
||||
AccountMemberUser,
|
||||
|
||||
#[error("account member last member constraint")]
|
||||
AccountMemberLastMember,
|
||||
|
||||
#[error("user app user constraint")]
|
||||
UserAppUser,
|
||||
|
||||
@@ -101,6 +124,7 @@ impl From<&str> for Constraint {
|
||||
"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_user_apps_user" => Constraint::UserAppUser,
|
||||
"fk_user_app_secrets_app" => Constraint::UserAppSecretApp,
|
||||
"fk_transactions_account" => Constraint::TransactionAccount,
|
||||
|
||||
Reference in New Issue
Block a user