Perform last member removal check on the database.
This commit is contained in:
@@ -134,13 +134,28 @@ where
|
||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||
{
|
||||
let member = db::delete_by_id(executor, user_id, account_id, id)
|
||||
.await?
|
||||
.await
|
||||
.map_err(classify_delete_error)?
|
||||
.map(decode)
|
||||
.transpose()?;
|
||||
|
||||
Ok(member)
|
||||
}
|
||||
|
||||
fn classify_delete_error(error: DatabaseError) -> RepositoryError {
|
||||
match &error {
|
||||
DatabaseError::CheckViolation {
|
||||
constraint: Constraint::AccountMemberLastMember,
|
||||
..
|
||||
} => RepositoryError::ReferenceViolation {
|
||||
relation: ReferenceRelation::AccountMember,
|
||||
kind: ReferenceViolationKind::LastRemaining,
|
||||
source: error,
|
||||
},
|
||||
_ => RepositoryError::Database(error),
|
||||
}
|
||||
}
|
||||
|
||||
fn decode(row: AccountMemberRow) -> Result<AccountMember, DecodeError> {
|
||||
Ok(AccountMember {
|
||||
id: row.id,
|
||||
@@ -232,6 +247,31 @@ mod tests {
|
||||
assert!(matches!(error, RepositoryError::Database(_)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn classifies_account_member_delete_last_member_check_violation() {
|
||||
let error = classify_delete_error(DatabaseError::CheckViolation {
|
||||
constraint: Constraint::AccountMemberLastMember,
|
||||
source: sqlx::Error::Protocol("test database cause".to_owned()),
|
||||
});
|
||||
assert!(matches!(
|
||||
error,
|
||||
RepositoryError::ReferenceViolation {
|
||||
relation: ReferenceRelation::AccountMember,
|
||||
kind: ReferenceViolationKind::LastRemaining,
|
||||
..
|
||||
}
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn keeps_unknown_delete_check_violations_generic() {
|
||||
let error = classify_delete_error(DatabaseError::CheckViolation {
|
||||
constraint: Constraint::Unnamed,
|
||||
source: sqlx::Error::Protocol("test database cause".to_owned()),
|
||||
});
|
||||
assert!(matches!(error, RepositoryError::Database(_)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn decodes_valid_rows() {
|
||||
let row = row();
|
||||
|
||||
@@ -62,22 +62,6 @@ where
|
||||
Ok(account)
|
||||
}
|
||||
|
||||
pub async fn get_by_id_locking<'e, E>(
|
||||
executor: E,
|
||||
user_id: Uuid,
|
||||
id: Uuid,
|
||||
) -> Result<Option<Account>, RepositoryError>
|
||||
where
|
||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||
{
|
||||
let account = db::get_by_id_locking(executor, user_id, id)
|
||||
.await?
|
||||
.map(decode)
|
||||
.transpose()?;
|
||||
|
||||
Ok(account)
|
||||
}
|
||||
|
||||
pub async fn get_by_ids<'e, E>(
|
||||
executor: E,
|
||||
user_id: Uuid,
|
||||
|
||||
Reference in New Issue
Block a user