From b3e4d1c1cf30217d904860e6d36bc51c5e78b123 Mon Sep 17 00:00:00 2001 From: Chris Tsang Date: Wed, 25 Jan 2023 15:38:27 +0800 Subject: [PATCH 1/2] RecordNotFound -> RecordNotUpdated --- src/error.rs | 11 +++++++---- src/executor/update.rs | 20 +++++--------------- tests/byte_primary_key_tests.rs | 7 +------ tests/crud/updates.rs | 7 +------ tests/string_primary_key_tests.rs | 7 +------ tests/uuid_tests.rs | 7 +------ 6 files changed, 16 insertions(+), 43 deletions(-) diff --git a/src/error.rs b/src/error.rs index 2ed87ffdc..90385678b 100644 --- a/src/error.rs +++ b/src/error.rs @@ -55,11 +55,14 @@ pub enum DbErr { /// A migration error #[error("Migration Error: {0}")] Migration(String), - /// None of the records are being inserted into the database, - /// if you insert with upsert expression that means - /// all of them conflict with existing records in the database - #[error("None of the records are being inserted")] + /// None of the records are inserted, + /// that probably means all of them conflict with existing records in the table + #[error("None of the records are inserted")] 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 diff --git a/src/executor/update.rs b/src/executor/update.rs index 3a42129db..26fe58a83 100644 --- a/src/executor/update.rs +++ b/src/executor/update.rs @@ -74,9 +74,7 @@ impl Updater { let statement = builder.build(&self.query); let result = db.execute(statement).await?; if self.check_record_exists && result.rows_affected() == 0 { - return Err(DbErr::RecordNotFound( - "None of the database rows are affected".to_owned(), - )); + return Err(DbErr::RecordNotUpdated); } Ok(UpdateResult { 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. match found { Some(model) => Ok(model), - None => Err(DbErr::RecordNotFound( - "None of the database rows are affected".to_owned(), - )), + None => Err(DbErr::RecordNotUpdated), } } false => { @@ -216,9 +212,7 @@ mod tests { } .update(&db) .await, - Err(DbErr::RecordNotFound( - "None of the database rows are affected".to_owned() - )) + Err(DbErr::RecordNotUpdated) ); assert_eq!( @@ -228,9 +222,7 @@ mod tests { }) .exec(&db) .await, - Err(DbErr::RecordNotFound( - "None of the database rows are affected".to_owned() - )) + Err(DbErr::RecordNotUpdated) ); assert_eq!( @@ -240,9 +232,7 @@ mod tests { }) .exec(&db) .await, - Err(DbErr::RecordNotFound( - "None of the database rows are affected".to_owned() - )) + Err(DbErr::RecordNotUpdated) ); assert_eq!( diff --git a/tests/byte_primary_key_tests.rs b/tests/byte_primary_key_tests.rs index 01b768505..63579b0d6 100644 --- a/tests/byte_primary_key_tests.rs +++ b/tests/byte_primary_key_tests.rs @@ -45,12 +45,7 @@ pub async fn create_and_update(db: &DatabaseConnection) -> Result<(), DbErr> { .exec(db) .await; - assert_eq!( - update_res, - Err(DbErr::RecordNotFound( - "None of the database rows are affected".to_owned() - )) - ); + assert_eq!(update_res, Err(DbErr::RecordNotUpdated)); let update_res = Entity::update(updated_active_model) .filter(Column::Id.eq(vec![1_u8, 2_u8, 3_u8])) // annotate it as Vec explicitly diff --git a/tests/crud/updates.rs b/tests/crud/updates.rs index 6cde55fa8..d1ade4fa0 100644 --- a/tests/crud/updates.rs +++ b/tests/crud/updates.rs @@ -121,12 +121,7 @@ pub async fn test_update_deleted_customer(db: &DbConn) { let customer_update_res = customer.update(db).await; - assert_eq!( - customer_update_res, - Err(DbErr::RecordNotFound( - "None of the database rows are affected".to_owned() - )) - ); + assert_eq!(customer_update_res, Err(DbErr::RecordNotUpdated)); assert_eq!(Customer::find().count(db).await.unwrap(), init_n_customers); diff --git a/tests/string_primary_key_tests.rs b/tests/string_primary_key_tests.rs index 9ed4df476..444969996 100644 --- a/tests/string_primary_key_tests.rs +++ b/tests/string_primary_key_tests.rs @@ -130,12 +130,7 @@ pub async fn create_and_update_repository(db: &DatabaseConnection) -> Result<(), .exec(db) .await; - assert_eq!( - update_res, - Err(DbErr::RecordNotFound( - "None of the database rows are affected".to_owned() - )) - ); + assert_eq!(update_res, Err(DbErr::RecordNotUpdated)); let update_res = Repository::update(updated_active_model) .filter(repository::Column::Id.eq("unique-id-002".to_owned())) diff --git a/tests/uuid_tests.rs b/tests/uuid_tests.rs index b0d077722..963b9fa82 100644 --- a/tests/uuid_tests.rs +++ b/tests/uuid_tests.rs @@ -85,12 +85,7 @@ pub async fn create_and_update_metadata(db: &DatabaseConnection) -> Result<(), D .exec(db) .await; - assert_eq!( - update_res, - Err(DbErr::RecordNotFound( - "None of the database rows are affected".to_owned() - )) - ); + assert_eq!(update_res, Err(DbErr::RecordNotUpdated)); Ok(()) } From cd4cf45df28ba5b8e8ce050736f036a419126f93 Mon Sep 17 00:00:00 2001 From: Chris Tsang Date: Wed, 25 Jan 2023 15:47:28 +0800 Subject: [PATCH 2/2] Changelog --- CHANGELOG.md | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 922305690..8cae3d778 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -64,6 +64,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ### Breaking changes +* Added to `RecordNotInserted` and `RecordNotUpdated` to `DbErr` * Added `ConnectionTrait::execute_unprepared` method https://github.com/SeaQL/sea-orm/pull/1327 * As part of https://github.com/SeaQL/sea-orm/pull/1311, the required method of `TryGetable` changed: ```rust @@ -91,6 +92,26 @@ impl ActiveModelBehavior for ActiveModel { // ... } ``` +* `DbErr::RecordNotFound("None of the database rows are affected")` is moved to a dedicated error variant `DbErr::RecordNotUpdated` +```rust +let res = Update::one(cake::ActiveModel { + name: Set("Cheese Cake".to_owned()), + ..model.into_active_model() + }) + .exec(&db) + .await; + +// then +assert_eq!( + res, + Err(DbErr::RecordNotFound( + "None of the database rows are affected".to_owned() + )) +); + +// now +assert_eq!(res, Err(DbErr::RecordNotUpdated)); +``` ## 0.10.7 - 2023-01-19