From 84f3837a669900cf84037121d9d3b69feaabb1f0 Mon Sep 17 00:00:00 2001 From: Shafkath Shuhan Date: Mon, 25 Apr 2022 19:19:01 -0400 Subject: [PATCH 1/2] add is_changed to ActiveModelTrait --- src/entity/active_model.rs | 5 +++++ src/lib.rs | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/entity/active_model.rs b/src/entity/active_model.rs index ceb4f726e..6d7f035e2 100644 --- a/src/entity/active_model.rs +++ b/src/entity/active_model.rs @@ -548,6 +548,11 @@ pub trait ActiveModelTrait: Clone + Debug { Ok(am) } + + /// Returns `true` if any columns were changed. + fn is_changed(&self) -> bool { + ::Column::iter().any(|col| !self.get(col).is_unchanged()) + } } /// A Trait for overriding the ActiveModel behavior diff --git a/src/lib.rs b/src/lib.rs index 1cc42f3a5..0668c7e62 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -263,7 +263,7 @@ //! ## Who's using SeaORM? //! //! SeaORM is the foundation of [StarfishQL](https://github.com/SeaQL/starfish-ql), an experimental graph database and query engine. -//! +//! //! For more projects, see [Built with SeaORM](https://github.com/SeaQL/sea-orm/blob/master/COMMUNITY.md#built-with-seaorm). //! //! ## License From 60ca990f2c01873267fdaf615db7f90c11402ecf Mon Sep 17 00:00:00 2001 From: Shafkath Shuhan Date: Sat, 7 May 2022 13:18:57 -0400 Subject: [PATCH 2/2] add test for `ActiveModelTrait::is_changed()` --- src/entity/active_model.rs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/entity/active_model.rs b/src/entity/active_model.rs index 6d7f035e2..0fd54b713 100644 --- a/src/entity/active_model.rs +++ b/src/entity/active_model.rs @@ -551,7 +551,8 @@ pub trait ActiveModelTrait: Clone + Debug { /// Returns `true` if any columns were changed. fn is_changed(&self) -> bool { - ::Column::iter().any(|col| !self.get(col).is_unchanged()) + ::Column::iter() + .any(|col| self.get(col).is_set() && !self.get(col).is_unchanged()) } } @@ -1093,4 +1094,13 @@ mod tests { Ok(()) } + + #[test] + fn test_active_model_is_changed() { + let mut fruit: fruit::ActiveModel = Default::default(); + assert!(!fruit.is_changed()); + + fruit.set(fruit::Column::Name, "apple".into()); + assert!(fruit.is_changed()); + } }