Throw error if none of the db rows are affected
This commit is contained in:
parent
beb3ec62dc
commit
11781082ba
@ -3,6 +3,7 @@ pub enum DbErr {
|
|||||||
Conn(String),
|
Conn(String),
|
||||||
Exec(String),
|
Exec(String),
|
||||||
Query(String),
|
Query(String),
|
||||||
|
RecordNotFound(String),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl std::error::Error for DbErr {}
|
impl std::error::Error for DbErr {}
|
||||||
@ -13,6 +14,7 @@ impl std::fmt::Display for DbErr {
|
|||||||
Self::Conn(s) => write!(f, "Connection Error: {}", s),
|
Self::Conn(s) => write!(f, "Connection Error: {}", s),
|
||||||
Self::Exec(s) => write!(f, "Execution Error: {}", s),
|
Self::Exec(s) => write!(f, "Execution Error: {}", s),
|
||||||
Self::Query(s) => write!(f, "Query Error: {}", s),
|
Self::Query(s) => write!(f, "Query Error: {}", s),
|
||||||
|
Self::RecordNotFound(s) => write!(f, "RecordNotFound Error: {}", s),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
use crate::{
|
use crate::{
|
||||||
error::*, ActiveModelTrait, DatabaseConnection, DbBackend, EntityTrait, Insert, PrimaryKeyTrait,
|
error::*, ActiveModelTrait, DatabaseConnection, DbBackend, EntityTrait, Insert,
|
||||||
Statement, TryFromU64,
|
PrimaryKeyTrait, Statement, TryFromU64,
|
||||||
};
|
};
|
||||||
use sea_query::InsertStatement;
|
use sea_query::InsertStatement;
|
||||||
use std::{future::Future, marker::PhantomData};
|
use std::{future::Future, marker::PhantomData};
|
||||||
|
@ -73,6 +73,11 @@ where
|
|||||||
// Only Statement impl Send
|
// Only Statement impl Send
|
||||||
async fn exec_update(statement: Statement, db: &DatabaseConnection) -> Result<UpdateResult, DbErr> {
|
async fn exec_update(statement: Statement, db: &DatabaseConnection) -> Result<UpdateResult, DbErr> {
|
||||||
let result = db.execute(statement).await?;
|
let result = db.execute(statement).await?;
|
||||||
|
if result.rows_affected() <= 0 {
|
||||||
|
return Err(DbErr::RecordNotFound(
|
||||||
|
"None of the database rows are affected".to_owned(),
|
||||||
|
));
|
||||||
|
}
|
||||||
Ok(UpdateResult {
|
Ok(UpdateResult {
|
||||||
rows_affected: result.rows_affected(),
|
rows_affected: result.rows_affected(),
|
||||||
})
|
})
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
pub use super::*;
|
pub use super::*;
|
||||||
use rust_decimal_macros::dec;
|
use rust_decimal_macros::dec;
|
||||||
|
use sea_orm::DbErr;
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
pub async fn test_update_cake(db: &DbConn) {
|
pub async fn test_update_cake(db: &DbConn) {
|
||||||
@ -119,10 +120,14 @@ pub async fn test_update_deleted_customer(db: &DbConn) {
|
|||||||
..Default::default()
|
..Default::default()
|
||||||
};
|
};
|
||||||
|
|
||||||
let _customer_update_res: customer::ActiveModel = customer
|
let customer_update_res = customer.update(db).await;
|
||||||
.update(db)
|
|
||||||
.await
|
assert_eq!(
|
||||||
.expect("could not update customer");
|
customer_update_res,
|
||||||
|
Err(DbErr::RecordNotFound(
|
||||||
|
"None of the database rows are affected".to_owned()
|
||||||
|
))
|
||||||
|
);
|
||||||
|
|
||||||
assert_eq!(Customer::find().count(db).await.unwrap(), init_n_customers);
|
assert_eq!(Customer::find().count(db).await.unwrap(), init_n_customers);
|
||||||
|
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
pub mod common;
|
pub mod common;
|
||||||
|
|
||||||
pub use common::{bakery_chain::*, setup::*, TestContext};
|
pub use common::{bakery_chain::*, setup::*, TestContext};
|
||||||
use sea_orm::{entity::prelude::*, DatabaseConnection, IntoActiveModel};
|
use sea_orm::{entity::prelude::*, DatabaseConnection, IntoActiveModel, Set};
|
||||||
|
|
||||||
#[sea_orm_macros::test]
|
#[sea_orm_macros::test]
|
||||||
#[cfg(any(
|
#[cfg(any(
|
||||||
@ -40,5 +40,20 @@ pub async fn create_metadata(db: &DatabaseConnection) -> Result<(), DbErr> {
|
|||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
let update_res = Metadata::update(metadata::ActiveModel {
|
||||||
|
value: Set("0.22".to_owned()),
|
||||||
|
..metadata.clone().into_active_model()
|
||||||
|
})
|
||||||
|
.filter(metadata::Column::Uuid.eq(Uuid::default()))
|
||||||
|
.exec(db)
|
||||||
|
.await;
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
update_res,
|
||||||
|
Err(DbErr::RecordNotFound(
|
||||||
|
"None of the database rows are affected".to_owned()
|
||||||
|
))
|
||||||
|
);
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user