diff --git a/fixtures/merger.json b/fixtures/merger.json index 91ea4cf..ee70960 100644 --- a/fixtures/merger.json +++ b/fixtures/merger.json @@ -143,7 +143,8 @@ ] }, "lookup_fields": [], - "historical": false, + "historical": true, + "notify": true, "relationship": false }, { @@ -191,6 +192,7 @@ }, "lookup_fields": [], "historical": true, + "notify": true, "relationship": false }, { @@ -239,6 +241,7 @@ }, "lookup_fields": [], "historical": true, + "notify": true, "relationship": false }, { @@ -341,6 +344,7 @@ "pronouns" ], "historical": true, + "notify": true, "relationship": false }, { @@ -395,6 +399,7 @@ "id" ], "historical": true, + "notify": true, "relationship": false }, { @@ -452,6 +457,7 @@ }, "lookup_fields": [], "historical": true, + "notify": true, "relationship": false }, { @@ -515,7 +521,8 @@ } ], "lookup_fields": [], - "historical": true + "historical": true, + "notify": true }, { "name": "contact", @@ -588,7 +595,8 @@ } ], "lookup_fields": [], - "historical": true + "historical": true, + "notify": true }, { "name": "phone_number", @@ -646,6 +654,7 @@ ], "lookup_fields": [], "historical": true, + "notify": true, "relationship": false }, { @@ -704,6 +713,7 @@ ], "lookup_fields": [], "historical": true, + "notify": true, "relationship": false } ] diff --git a/src/database/type.rs b/src/database/type.rs index f71bda5..f3554f7 100644 --- a/src/database/type.rs +++ b/src/database/type.rs @@ -15,6 +15,8 @@ pub struct Type { #[serde(default)] pub historical: bool, #[serde(default)] + pub notify: bool, + #[serde(default)] pub sensitive: bool, #[serde(default)] pub ownable: bool, diff --git a/src/merger/mod.rs b/src/merger/mod.rs index a109321..da6e8ef 100644 --- a/src/merger/mod.rs +++ b/src/merger/mod.rs @@ -3,6 +3,7 @@ pub mod cache; +use crate::database::r#type::Type; use crate::database::Database; use serde_json::Value; use std::sync::Arc; @@ -321,8 +322,9 @@ impl Merger { } } - // 7. Perform change tracking + // 7. Perform change tracking dynamically suppressing noise based on type bounds! let notify_sql = self.merge_entity_change( + type_def, &entity_fields, entity_fetched.as_ref(), entity_change_kind.as_deref(), @@ -680,6 +682,7 @@ impl Merger { fn merge_entity_change( &self, + type_obj: &Type, entity_fields: &serde_json::Map, entity_fetched: Option<&serde_json::Map>, entity_change_kind: Option<&str>, @@ -760,28 +763,33 @@ impl Merger { notification.insert("old".to_string(), old_val_obj.clone()); } - let change_sql = format!( - "INSERT INTO agreego.change (\"old\", \"new\", entity_id, id, kind, modified_at, modified_by) VALUES ({}, {}, {}, {}, {}, {}, {})", - Self::quote_literal(&old_val_obj), - Self::quote_literal(&new_val_obj), - Self::quote_literal(id_str), - Self::quote_literal(&Value::String(uuid::Uuid::new_v4().to_string())), - Self::quote_literal(&Value::String(change_kind.to_string())), - Self::quote_literal(&Value::String(timestamp.to_string())), - Self::quote_literal(&Value::String(user_id.to_string())) - ); + let mut notify_sql = None; + if type_obj.historical { + let change_sql = format!( + "INSERT INTO agreego.change (\"old\", \"new\", entity_id, id, kind, modified_at, modified_by) VALUES ({}, {}, {}, {}, {}, {}, {})", + Self::quote_literal(&old_val_obj), + Self::quote_literal(&new_val_obj), + Self::quote_literal(id_str), + Self::quote_literal(&Value::String(uuid::Uuid::new_v4().to_string())), + Self::quote_literal(&Value::String(change_kind.to_string())), + Self::quote_literal(&Value::String(timestamp.to_string())), + Self::quote_literal(&Value::String(user_id.to_string())) + ); - let notify_sql = format!( - "SELECT pg_notify('entity', {})", - Self::quote_literal(&Value::String(Value::Object(notification).to_string())) - ); + self + .db + .execute(&change_sql, None) + .map_err(|e| format!("Executor Error in change: {:?}", e))?; + } - self - .db - .execute(&change_sql, None) - .map_err(|e| format!("Executor Error in change: {:?}", e))?; + if type_obj.notify { + notify_sql = Some(format!( + "SELECT pg_notify('entity', {})", + Self::quote_literal(&Value::String(Value::Object(notification).to_string())) + )); + } - Ok(Some(notify_sql)) + Ok(notify_sql) } fn compare_entities(