From d5c9c65079d8bdecf5d9c0371114f05e44622cca Mon Sep 17 00:00:00 2001 From: Chris Tsang Date: Sat, 25 Dec 2021 22:55:39 +0800 Subject: [PATCH] Revert `save` to return Self --- examples/basic/src/operation.rs | 4 +-- src/entity/active_model.rs | 16 ++++++------ src/lib.rs | 2 +- tests/basic.rs | 2 +- tests/crud/updates.rs | 2 +- tests/query_tests.rs | 6 ++--- tests/relational_tests.rs | 46 ++++++++++++++++----------------- tests/sequential_op_tests.rs | 20 +++++++------- tests/stream_tests.rs | 4 +-- 9 files changed, 51 insertions(+), 51 deletions(-) diff --git a/examples/basic/src/operation.rs b/examples/basic/src/operation.rs index 787cbc960..b5104095b 100644 --- a/examples/basic/src/operation.rs +++ b/examples/basic/src/operation.rs @@ -46,14 +46,14 @@ pub async fn save_active_model(db: &DbConn) -> Result<(), DbErr> { name: Set("Banana".to_owned()), ..Default::default() }; - let mut banana: fruit::ActiveModel = banana.save(db).await?.into_active_model(); + let mut banana: fruit::ActiveModel = banana.save(db).await?; println!(); println!("Inserted: {:?}\n", banana); banana.name = Set("Banana Mongo".to_owned()); - let banana: fruit::ActiveModel = banana.save(db).await?.into_active_model(); + let banana: fruit::ActiveModel = banana.save(db).await?; println!(); println!("Updated: {:?}\n", banana); diff --git a/src/entity/active_model.rs b/src/entity/active_model.rs index d01145e34..18dca6960 100644 --- a/src/entity/active_model.rs +++ b/src/entity/active_model.rs @@ -381,28 +381,28 @@ pub trait ActiveModelTrait: Clone + Debug { Self::after_save(model, false) } - /// Insert the model if primary key is not_set, update otherwise. + /// Insert the model if primary key is `NotSet`, update otherwise. /// Only works if the entity has auto increment primary key. - async fn save<'a, C>(self, db: &'a C) -> Result<::Model, DbErr> + async fn save<'a, C>(self, db: &'a C) -> Result where ::Model: IntoActiveModel, Self: ActiveModelBehavior + 'a, C: ConnectionTrait<'a>, { - let am = self; let mut is_update = true; for key in ::PrimaryKey::iter() { let col = key.into_column(); - if am.is_not_set(col) { + if self.is_not_set(col) { is_update = false; break; } } - if !is_update { - am.insert(db).await + let res = if !is_update { + self.insert(db).await } else { - am.update(db).await - } + self.update(db).await + }?; + Ok(res.into_active_model()) } /// Delete an active model by its primary key diff --git a/src/lib.rs b/src/lib.rs index 422fe932c..a44d19cd1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -207,7 +207,7 @@ //! }; //! //! // create, because primary key `id` is `NotSet` -//! let mut banana = banana.save(db).await?.into_active_model(); +//! let mut banana = banana.save(db).await?; //! //! banana.name = Set("Banana Mongo".to_owned()); //! diff --git a/tests/basic.rs b/tests/basic.rs index d7d342d1f..def00f436 100644 --- a/tests/basic.rs +++ b/tests/basic.rs @@ -45,7 +45,7 @@ async fn crud_cake(db: &DbConn) -> Result<(), DbErr> { ..Default::default() }; - let mut apple = apple.save(db).await?.into_active_model(); + let mut apple = apple.save(db).await?; println!(); println!("Inserted: {:?}", apple); diff --git a/tests/crud/updates.rs b/tests/crud/updates.rs index 6c3cf690a..6cde55fa8 100644 --- a/tests/crud/updates.rs +++ b/tests/crud/updates.rs @@ -108,7 +108,7 @@ pub async fn test_update_deleted_customer(db: &DbConn) { init_n_customers + 1 ); - let customer_id = customer.id; + let customer_id = customer.id.clone().unwrap(); let _ = customer.delete(db).await; assert_eq!(Customer::find().count(db).await.unwrap(), init_n_customers); diff --git a/tests/query_tests.rs b/tests/query_tests.rs index e8b3c0daf..9d22c7b04 100644 --- a/tests/query_tests.rs +++ b/tests/query_tests.rs @@ -43,7 +43,7 @@ pub async fn find_one_with_result() { let result = Bakery::find().one(&ctx.db).await.unwrap().unwrap(); - assert_eq!(result.id, bakery.id); + assert_eq!(result.id, bakery.id.unwrap()); ctx.delete().await; } @@ -83,13 +83,13 @@ pub async fn find_by_id_with_result() { .await .expect("could not insert bakery"); - let result = Bakery::find_by_id(bakery.id.clone()) + let result = Bakery::find_by_id(bakery.id.clone().unwrap()) .one(&ctx.db) .await .unwrap() .unwrap(); - assert_eq!(result.id, bakery.id); + assert_eq!(result.id, bakery.id.unwrap()); ctx.delete().await; } diff --git a/tests/relational_tests.rs b/tests/relational_tests.rs index 196bb93cf..05e081ffd 100644 --- a/tests/relational_tests.rs +++ b/tests/relational_tests.rs @@ -24,7 +24,7 @@ pub async fn left_join() { profit_margin: Set(10.4), ..Default::default() } - .save(&ctx.db) + .insert(&ctx.db) .await .expect("could not insert bakery"); @@ -38,7 +38,7 @@ pub async fn left_join() { bakery_id: Set(Some(bakery.id.clone())), ..Default::default() } - .save(&ctx.db) + .insert(&ctx.db) .await .expect("could not insert baker"); @@ -48,7 +48,7 @@ pub async fn left_join() { bakery_id: Set(None), ..Default::default() } - .save(&ctx.db) + .insert(&ctx.db) .await .expect("could not insert baker"); @@ -103,7 +103,7 @@ pub async fn right_join() { profit_margin: Set(10.4), ..Default::default() } - .save(&ctx.db) + .insert(&ctx.db) .await .expect("could not insert bakery"); @@ -111,7 +111,7 @@ pub async fn right_join() { name: Set("Kate".to_owned()), ..Default::default() } - .save(&ctx.db) + .insert(&ctx.db) .await .expect("could not insert customer"); @@ -119,7 +119,7 @@ pub async fn right_join() { name: Set("Jim".to_owned()), ..Default::default() } - .save(&ctx.db) + .insert(&ctx.db) .await .expect("could not insert customer"); @@ -131,7 +131,7 @@ pub async fn right_join() { ..Default::default() } - .save(&ctx.db) + .insert(&ctx.db) .await .expect("could not insert order"); @@ -189,7 +189,7 @@ pub async fn inner_join() { profit_margin: Set(10.4), ..Default::default() } - .save(&ctx.db) + .insert(&ctx.db) .await .expect("could not insert bakery"); @@ -197,7 +197,7 @@ pub async fn inner_join() { name: Set("Kate".to_owned()), ..Default::default() } - .save(&ctx.db) + .insert(&ctx.db) .await .expect("could not insert customer"); @@ -205,7 +205,7 @@ pub async fn inner_join() { name: Set("Jim".to_owned()), ..Default::default() } - .save(&ctx.db) + .insert(&ctx.db) .await .expect("could not insert customer"); @@ -217,7 +217,7 @@ pub async fn inner_join() { ..Default::default() } - .save(&ctx.db) + .insert(&ctx.db) .await .expect("could not insert order"); @@ -229,7 +229,7 @@ pub async fn inner_join() { ..Default::default() } - .save(&ctx.db) + .insert(&ctx.db) .await .expect("could not insert order"); @@ -279,7 +279,7 @@ pub async fn group_by() { profit_margin: Set(10.4), ..Default::default() } - .save(&ctx.db) + .insert(&ctx.db) .await .expect("could not insert bakery"); @@ -287,7 +287,7 @@ pub async fn group_by() { name: Set("Kate".to_owned()), ..Default::default() } - .save(&ctx.db) + .insert(&ctx.db) .await .expect("could not insert customer"); @@ -299,7 +299,7 @@ pub async fn group_by() { ..Default::default() } - .save(&ctx.db) + .insert(&ctx.db) .await .expect("could not insert order"); @@ -311,7 +311,7 @@ pub async fn group_by() { ..Default::default() } - .save(&ctx.db) + .insert(&ctx.db) .await .expect("could not insert order"); @@ -374,7 +374,7 @@ pub async fn having() { profit_margin: Set(10.4), ..Default::default() } - .save(&ctx.db) + .insert(&ctx.db) .await .expect("could not insert bakery"); @@ -382,7 +382,7 @@ pub async fn having() { name: Set("Kate".to_owned()), ..Default::default() } - .save(&ctx.db) + .insert(&ctx.db) .await .expect("could not insert customer"); @@ -394,7 +394,7 @@ pub async fn having() { ..Default::default() } - .save(&ctx.db) + .insert(&ctx.db) .await .expect("could not insert order"); @@ -406,7 +406,7 @@ pub async fn having() { ..Default::default() } - .save(&ctx.db) + .insert(&ctx.db) .await .expect("could not insert order"); @@ -414,7 +414,7 @@ pub async fn having() { name: Set("Bob".to_owned()), ..Default::default() } - .save(&ctx.db) + .insert(&ctx.db) .await .expect("could not insert customer"); @@ -426,7 +426,7 @@ pub async fn having() { ..Default::default() } - .save(&ctx.db) + .insert(&ctx.db) .await .expect("could not insert order"); @@ -438,7 +438,7 @@ pub async fn having() { ..Default::default() } - .save(&ctx.db) + .insert(&ctx.db) .await .expect("could not insert order"); diff --git a/tests/sequential_op_tests.rs b/tests/sequential_op_tests.rs index de2e0dd84..30d9b0417 100644 --- a/tests/sequential_op_tests.rs +++ b/tests/sequential_op_tests.rs @@ -42,7 +42,7 @@ async fn seed_data(db: &DatabaseConnection) { let baker_1 = baker::ActiveModel { name: Set("Baker 1".to_owned()), contact_details: Set(serde_json::json!({})), - bakery_id: Set(Some(bakery.id.clone())), + bakery_id: Set(Some(bakery.id.clone().unwrap())), ..Default::default() } .save(db) @@ -52,7 +52,7 @@ async fn seed_data(db: &DatabaseConnection) { let _baker_2 = baker::ActiveModel { name: Set("Baker 2".to_owned()), contact_details: Set(serde_json::json!({})), - bakery_id: Set(Some(bakery.id.clone())), + bakery_id: Set(Some(bakery.id.clone().unwrap())), ..Default::default() } .save(db) @@ -64,7 +64,7 @@ async fn seed_data(db: &DatabaseConnection) { price: Set(dec!(10.25)), gluten_free: Set(false), serial: Set(Uuid::new_v4()), - bakery_id: Set(Some(bakery.id.clone())), + bakery_id: Set(Some(bakery.id.clone().unwrap())), ..Default::default() }; @@ -75,7 +75,7 @@ async fn seed_data(db: &DatabaseConnection) { let cake_baker = cakes_bakers::ActiveModel { cake_id: Set(cake_insert_res.last_insert_id as i32), - baker_id: Set(baker_1.id.clone()), + baker_id: Set(baker_1.id.clone().unwrap()), ..Default::default() }; @@ -97,8 +97,8 @@ async fn seed_data(db: &DatabaseConnection) { .expect("could not insert customer"); let kate_order_1 = order::ActiveModel { - bakery_id: Set(bakery.id.clone()), - customer_id: Set(customer_kate.id.clone()), + bakery_id: Set(bakery.id.clone().unwrap()), + customer_id: Set(customer_kate.id.clone().unwrap()), total: Set(dec!(99.95)), placed_at: Set(Utc::now().naive_utc()), @@ -112,7 +112,7 @@ async fn seed_data(db: &DatabaseConnection) { cake_id: Set(cake_insert_res.last_insert_id as i32), price: Set(dec!(10.00)), quantity: Set(12), - order_id: Set(kate_order_1.id.clone()), + order_id: Set(kate_order_1.id.clone().unwrap()), ..Default::default() } .save(db) @@ -123,7 +123,7 @@ async fn seed_data(db: &DatabaseConnection) { cake_id: Set(cake_insert_res.last_insert_id as i32), price: Set(dec!(50.00)), quantity: Set(2), - order_id: Set(kate_order_1.id.clone()), + order_id: Set(kate_order_1.id.clone().unwrap()), ..Default::default() } .save(db) @@ -243,7 +243,7 @@ async fn create_order(db: &DatabaseConnection, cake: cake::Model) { let order = order::ActiveModel { bakery_id: Set(cake.bakery_id.unwrap()), - customer_id: Set(another_customer.id.clone()), + customer_id: Set(another_customer.id.clone().unwrap()), total: Set(dec!(200.00)), placed_at: Set(Utc::now().naive_utc()), @@ -257,7 +257,7 @@ async fn create_order(db: &DatabaseConnection, cake: cake::Model) { cake_id: Set(cake.id), price: Set(dec!(10.00)), quantity: Set(300), - order_id: Set(order.id.clone()), + order_id: Set(order.id.clone().unwrap()), ..Default::default() } .save(db) diff --git a/tests/stream_tests.rs b/tests/stream_tests.rs index ba0705414..37e6c21d9 100644 --- a/tests/stream_tests.rs +++ b/tests/stream_tests.rs @@ -24,14 +24,14 @@ pub async fn stream() -> Result<(), DbErr> { .save(&ctx.db) .await?; - let result = Bakery::find_by_id(bakery.id.clone()) + let result = Bakery::find_by_id(bakery.id.clone().unwrap()) .stream(&ctx.db) .await? .next() .await .unwrap()?; - assert_eq!(result.id, bakery.id); + assert_eq!(result.id, bakery.id.unwrap()); ctx.delete().await;