Skip to content

Commit

Permalink
Revert save to return Self
Browse files Browse the repository at this point in the history
  • Loading branch information
tyt2y3 committed Dec 25, 2021
1 parent cf685fe commit d5c9c65
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 51 deletions.
4 changes: 2 additions & 2 deletions examples/basic/src/operation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
16 changes: 8 additions & 8 deletions src/entity/active_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<<Self::Entity as EntityTrait>::Model, DbErr>
async fn save<'a, C>(self, db: &'a C) -> Result<Self, DbErr>
where
<Self::Entity as EntityTrait>::Model: IntoActiveModel<Self>,
Self: ActiveModelBehavior + 'a,
C: ConnectionTrait<'a>,
{
let am = self;
let mut is_update = true;
for key in <Self::Entity as EntityTrait>::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
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
//!
Expand Down
2 changes: 1 addition & 1 deletion tests/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion tests/crud/updates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions tests/query_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down
46 changes: 23 additions & 23 deletions tests/relational_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand All @@ -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");

Expand All @@ -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");

Expand Down Expand Up @@ -103,23 +103,23 @@ pub async fn right_join() {
profit_margin: Set(10.4),
..Default::default()
}
.save(&ctx.db)
.insert(&ctx.db)
.await
.expect("could not insert bakery");

let customer_kate = customer::ActiveModel {
name: Set("Kate".to_owned()),
..Default::default()
}
.save(&ctx.db)
.insert(&ctx.db)
.await
.expect("could not insert customer");

let _customer_jim = customer::ActiveModel {
name: Set("Jim".to_owned()),
..Default::default()
}
.save(&ctx.db)
.insert(&ctx.db)
.await
.expect("could not insert customer");

Expand All @@ -131,7 +131,7 @@ pub async fn right_join() {

..Default::default()
}
.save(&ctx.db)
.insert(&ctx.db)
.await
.expect("could not insert order");

Expand Down Expand Up @@ -189,23 +189,23 @@ pub async fn inner_join() {
profit_margin: Set(10.4),
..Default::default()
}
.save(&ctx.db)
.insert(&ctx.db)
.await
.expect("could not insert bakery");

let customer_kate = customer::ActiveModel {
name: Set("Kate".to_owned()),
..Default::default()
}
.save(&ctx.db)
.insert(&ctx.db)
.await
.expect("could not insert customer");

let _customer_jim = customer::ActiveModel {
name: Set("Jim".to_owned()),
..Default::default()
}
.save(&ctx.db)
.insert(&ctx.db)
.await
.expect("could not insert customer");

Expand All @@ -217,7 +217,7 @@ pub async fn inner_join() {

..Default::default()
}
.save(&ctx.db)
.insert(&ctx.db)
.await
.expect("could not insert order");

Expand All @@ -229,7 +229,7 @@ pub async fn inner_join() {

..Default::default()
}
.save(&ctx.db)
.insert(&ctx.db)
.await
.expect("could not insert order");

Expand Down Expand Up @@ -279,15 +279,15 @@ pub async fn group_by() {
profit_margin: Set(10.4),
..Default::default()
}
.save(&ctx.db)
.insert(&ctx.db)
.await
.expect("could not insert bakery");

let customer_kate = customer::ActiveModel {
name: Set("Kate".to_owned()),
..Default::default()
}
.save(&ctx.db)
.insert(&ctx.db)
.await
.expect("could not insert customer");

Expand All @@ -299,7 +299,7 @@ pub async fn group_by() {

..Default::default()
}
.save(&ctx.db)
.insert(&ctx.db)
.await
.expect("could not insert order");

Expand All @@ -311,7 +311,7 @@ pub async fn group_by() {

..Default::default()
}
.save(&ctx.db)
.insert(&ctx.db)
.await
.expect("could not insert order");

Expand Down Expand Up @@ -374,15 +374,15 @@ pub async fn having() {
profit_margin: Set(10.4),
..Default::default()
}
.save(&ctx.db)
.insert(&ctx.db)
.await
.expect("could not insert bakery");

let customer_kate = customer::ActiveModel {
name: Set("Kate".to_owned()),
..Default::default()
}
.save(&ctx.db)
.insert(&ctx.db)
.await
.expect("could not insert customer");

Expand All @@ -394,7 +394,7 @@ pub async fn having() {

..Default::default()
}
.save(&ctx.db)
.insert(&ctx.db)
.await
.expect("could not insert order");

Expand All @@ -406,15 +406,15 @@ pub async fn having() {

..Default::default()
}
.save(&ctx.db)
.insert(&ctx.db)
.await
.expect("could not insert order");

let customer_bob = customer::ActiveModel {
name: Set("Bob".to_owned()),
..Default::default()
}
.save(&ctx.db)
.insert(&ctx.db)
.await
.expect("could not insert customer");

Expand All @@ -426,7 +426,7 @@ pub async fn having() {

..Default::default()
}
.save(&ctx.db)
.insert(&ctx.db)
.await
.expect("could not insert order");

Expand All @@ -438,7 +438,7 @@ pub async fn having() {

..Default::default()
}
.save(&ctx.db)
.insert(&ctx.db)
.await
.expect("could not insert order");

Expand Down
20 changes: 10 additions & 10 deletions tests/sequential_op_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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()
};

Expand All @@ -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()
};

Expand All @@ -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()),

Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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()),

Expand All @@ -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)
Expand Down
Loading

0 comments on commit d5c9c65

Please sign in to comment.