RecordNotFound -> RecordNotUpdated

This commit is contained in:
Chris Tsang 2023-01-25 15:38:27 +08:00
parent 6b757413f3
commit b3e4d1c1cf
6 changed files with 16 additions and 43 deletions

View File

@ -55,11 +55,14 @@ pub enum DbErr {
/// A migration error /// A migration error
#[error("Migration Error: {0}")] #[error("Migration Error: {0}")]
Migration(String), Migration(String),
/// None of the records are being inserted into the database, /// None of the records are inserted,
/// if you insert with upsert expression that means /// that probably means all of them conflict with existing records in the table
/// all of them conflict with existing records in the database #[error("None of the records are inserted")]
#[error("None of the records are being inserted")]
RecordNotInserted, RecordNotInserted,
/// None of the records are updated, that means a WHERE condition has no matches.
/// May be the table is empty or the record does not exist
#[error("None of the records are updated")]
RecordNotUpdated,
} }
/// Runtime error /// Runtime error

View File

@ -74,9 +74,7 @@ impl Updater {
let statement = builder.build(&self.query); let statement = builder.build(&self.query);
let result = db.execute(statement).await?; let result = db.execute(statement).await?;
if self.check_record_exists && result.rows_affected() == 0 { if self.check_record_exists && result.rows_affected() == 0 {
return Err(DbErr::RecordNotFound( return Err(DbErr::RecordNotUpdated);
"None of the database rows are affected".to_owned(),
));
} }
Ok(UpdateResult { Ok(UpdateResult {
rows_affected: result.rows_affected(), rows_affected: result.rows_affected(),
@ -114,9 +112,7 @@ impl Updater {
// If we got `None` then we are updating a row that does not exist. // If we got `None` then we are updating a row that does not exist.
match found { match found {
Some(model) => Ok(model), Some(model) => Ok(model),
None => Err(DbErr::RecordNotFound( None => Err(DbErr::RecordNotUpdated),
"None of the database rows are affected".to_owned(),
)),
} }
} }
false => { false => {
@ -216,9 +212,7 @@ mod tests {
} }
.update(&db) .update(&db)
.await, .await,
Err(DbErr::RecordNotFound( Err(DbErr::RecordNotUpdated)
"None of the database rows are affected".to_owned()
))
); );
assert_eq!( assert_eq!(
@ -228,9 +222,7 @@ mod tests {
}) })
.exec(&db) .exec(&db)
.await, .await,
Err(DbErr::RecordNotFound( Err(DbErr::RecordNotUpdated)
"None of the database rows are affected".to_owned()
))
); );
assert_eq!( assert_eq!(
@ -240,9 +232,7 @@ mod tests {
}) })
.exec(&db) .exec(&db)
.await, .await,
Err(DbErr::RecordNotFound( Err(DbErr::RecordNotUpdated)
"None of the database rows are affected".to_owned()
))
); );
assert_eq!( assert_eq!(

View File

@ -45,12 +45,7 @@ pub async fn create_and_update(db: &DatabaseConnection) -> Result<(), DbErr> {
.exec(db) .exec(db)
.await; .await;
assert_eq!( assert_eq!(update_res, Err(DbErr::RecordNotUpdated));
update_res,
Err(DbErr::RecordNotFound(
"None of the database rows are affected".to_owned()
))
);
let update_res = Entity::update(updated_active_model) let update_res = Entity::update(updated_active_model)
.filter(Column::Id.eq(vec![1_u8, 2_u8, 3_u8])) // annotate it as Vec<u8> explicitly .filter(Column::Id.eq(vec![1_u8, 2_u8, 3_u8])) // annotate it as Vec<u8> explicitly

View File

@ -121,12 +121,7 @@ pub async fn test_update_deleted_customer(db: &DbConn) {
let customer_update_res = customer.update(db).await; let customer_update_res = customer.update(db).await;
assert_eq!( assert_eq!(customer_update_res, Err(DbErr::RecordNotUpdated));
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);

View File

@ -130,12 +130,7 @@ pub async fn create_and_update_repository(db: &DatabaseConnection) -> Result<(),
.exec(db) .exec(db)
.await; .await;
assert_eq!( assert_eq!(update_res, Err(DbErr::RecordNotUpdated));
update_res,
Err(DbErr::RecordNotFound(
"None of the database rows are affected".to_owned()
))
);
let update_res = Repository::update(updated_active_model) let update_res = Repository::update(updated_active_model)
.filter(repository::Column::Id.eq("unique-id-002".to_owned())) .filter(repository::Column::Id.eq("unique-id-002".to_owned()))

View File

@ -85,12 +85,7 @@ pub async fn create_and_update_metadata(db: &DatabaseConnection) -> Result<(), D
.exec(db) .exec(db)
.await; .await;
assert_eq!( assert_eq!(update_res, Err(DbErr::RecordNotUpdated));
update_res,
Err(DbErr::RecordNotFound(
"None of the database rows are affected".to_owned()
))
);
Ok(()) Ok(())
} }