From 1f3fa1dc19e0ec6e495e43677c047284e5e84b86 Mon Sep 17 00:00:00 2001 From: Chris Tsang Date: Wed, 21 Jun 2023 23:14:57 +0800 Subject: [PATCH] Changelog with Tweaks --- CHANGELOG.md | 16 +++++++++++++-- src/executor/insert.rs | 39 ++++++++++++++++++++----------------- tests/empty_insert_tests.rs | 4 ++-- tests/upsert_tests.rs | 4 ++-- 4 files changed, 39 insertions(+), 24 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1261e77f..324cddf2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -306,12 +306,24 @@ assert_eq!(migration.status(), MigrationStatus::Pending); * Added `TryInsert` that does not panic on empty inserts https://github.com/SeaQL/sea-orm/pull/1708 ```rust // now, you can do: -let empty_insert = Bakery::insert_many(std::iter::empty()) +let res = Bakery::insert_many(std::iter::empty()) .on_empty_do_nothing() .exec(db) .await; -assert!(matches!(empty_insert, TryInsertResult::Empty)); +assert!(matches!(res, Ok(TryInsertResult::Empty))); +``` +* On conflict do nothing not resulting in Err https://github.com/SeaQL/sea-orm/pull/1712 +```rust +let on = OnConflict::column(Column::Id).do_nothing().to_owned(); + +// Existing behaviour +let res = Entity::insert_many([..]).on_conflict(on).exec(db).await; +assert!(matches!(res, Err(DbErr::RecordNotInserted))); + +// New API; now you can: +let res = Entity::insert_many([..]).on_conflict(on).do_nothing().exec(db).await; +assert!(matches!(res, Ok(TryInsertResult::Conflicted))); ``` ### Upgrades diff --git a/src/executor/insert.rs b/src/executor/insert.rs index 1af3c338..cf6f320f 100644 --- a/src/executor/insert.rs +++ b/src/executor/insert.rs @@ -44,19 +44,20 @@ where { /// Execute an insert operation #[allow(unused_mut)] - pub async fn exec<'a, C>(self, db: &'a C) -> TryInsertResult, DbErr>> + pub async fn exec<'a, C>(self, db: &'a C) -> Result>, DbErr> where C: ConnectionTrait, A: 'a, { if self.insert_struct.columns.is_empty() { - return TryInsertResult::Empty; + return Ok(TryInsertResult::Empty); } - let temp = self.insert_struct.exec(db).await; - if matches!(temp, Err(DbErr::RecordNotInserted)) { - return TryInsertResult::Conflicted; + let res = self.insert_struct.exec(db).await; + match res { + Ok(res) => Ok(TryInsertResult::Inserted(res)), + Err(DbErr::RecordNotInserted) => Ok(TryInsertResult::Conflicted), + Err(err) => Err(err), } - TryInsertResult::Inserted(temp) } /// Execute an insert operation without returning (don't use `RETURNING` syntax) @@ -64,40 +65,42 @@ where pub async fn exec_without_returning<'a, C>( self, db: &'a C, - ) -> TryInsertResult> + ) -> Result, DbErr> where ::Model: IntoActiveModel, C: ConnectionTrait, A: 'a, { if self.insert_struct.columns.is_empty() { - return TryInsertResult::Empty; + return Ok(TryInsertResult::Empty); } - let temp = self.insert_struct.exec_without_returning(db).await; - if matches!(temp, Err(DbErr::RecordNotInserted)) { - return TryInsertResult::Conflicted; + let res = self.insert_struct.exec_without_returning(db).await; + match res { + Ok(res) => Ok(TryInsertResult::Inserted(res)), + Err(DbErr::RecordNotInserted) => Ok(TryInsertResult::Conflicted), + Err(err) => Err(err), } - TryInsertResult::Inserted(temp) } /// Execute an insert operation and return the inserted model (use `RETURNING` syntax if database supported) pub async fn exec_with_returning<'a, C>( self, db: &'a C, - ) -> TryInsertResult::Model, DbErr>> + ) -> Result::Model>, DbErr> where ::Model: IntoActiveModel, C: ConnectionTrait, A: 'a, { if self.insert_struct.columns.is_empty() { - return TryInsertResult::Empty; + return Ok(TryInsertResult::Empty); } - let temp = self.insert_struct.exec_with_returning(db).await; - if matches!(temp, Err(DbErr::RecordNotInserted)) { - return TryInsertResult::Conflicted; + let res = self.insert_struct.exec_with_returning(db).await; + match res { + Ok(res) => Ok(TryInsertResult::Inserted(res)), + Err(DbErr::RecordNotInserted) => Ok(TryInsertResult::Conflicted), + Err(err) => Err(err), } - TryInsertResult::Inserted(temp) } } diff --git a/tests/empty_insert_tests.rs b/tests/empty_insert_tests.rs index 5c17b5e5..e3a8bfa7 100644 --- a/tests/empty_insert_tests.rs +++ b/tests/empty_insert_tests.rs @@ -35,12 +35,12 @@ pub async fn test(db: &DbConn) { .exec(db) .await; - assert!(matches!(res, TryInsertResult::Inserted(_))); + assert!(matches!(res, Ok(TryInsertResult::Inserted(_)))); let empty_insert = Bakery::insert_many(std::iter::empty::()) .on_empty_do_nothing() .exec(db) .await; - assert!(matches!(empty_insert, TryInsertResult::Empty)); + assert!(matches!(empty_insert, Ok(TryInsertResult::Empty))); } diff --git a/tests/upsert_tests.rs b/tests/upsert_tests.rs index 05009b5b..ff3e381f 100644 --- a/tests/upsert_tests.rs +++ b/tests/upsert_tests.rs @@ -55,7 +55,7 @@ pub async fn create_insert_default(db: &DatabaseConnection) -> Result<(), DbErr> .exec(db) .await; - assert_eq!(res.err(), Some(DbErr::RecordNotInserted)); + assert!(matches!(res, Err(DbErr::RecordNotInserted))); let res = Entity::insert_many([ ActiveModel { id: Set(1) }, @@ -68,7 +68,7 @@ pub async fn create_insert_default(db: &DatabaseConnection) -> Result<(), DbErr> .exec(db) .await; - assert!(matches!(res, TryInsertResult::Conflicted)); + assert!(matches!(res, Ok(TryInsertResult::Conflicted))); Ok(()) }