Compare commits
11 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 9038607729 | |||
| 9f6c27c3b8 | |||
| 75aac41362 | |||
| dbcef42401 | |||
| b6c5561d2f | |||
| e01b778d68 | |||
| 6eb134c0d6 | |||
| 7ccc4b7cce | |||
| 77bfa4cd18 | |||
| b47a5abd26 | |||
| fcd8310ed8 |
File diff suppressed because it is too large
Load Diff
102
m1.json
Normal file
102
m1.json
Normal file
@ -0,0 +1,102 @@
|
||||
{
|
||||
"description": "Attachment with both type_metadata and other_metadata ad-hoc structures",
|
||||
"action": "merge",
|
||||
"data": {
|
||||
"type": "attachment",
|
||||
"flags": [
|
||||
"urgent",
|
||||
"reviewed"
|
||||
],
|
||||
"other_metadata": {
|
||||
"other": "hello"
|
||||
},
|
||||
"type_metadata": {
|
||||
"type": "type_metadata"
|
||||
}
|
||||
},
|
||||
"expect": {
|
||||
"success": true,
|
||||
"sql": [
|
||||
[
|
||||
"INSERT INTO agreego.\"entity\" (",
|
||||
" \"created_at\",",
|
||||
" \"created_by\",",
|
||||
" \"id\",",
|
||||
" \"modified_at\",",
|
||||
" \"modified_by\",",
|
||||
" \"type\"",
|
||||
")",
|
||||
"VALUES (",
|
||||
" '{{timestamp}}',",
|
||||
" '00000000-0000-0000-0000-000000000000',",
|
||||
" '{{uuid:attachment_id}}',",
|
||||
" '{{timestamp}}',",
|
||||
" '00000000-0000-0000-0000-000000000000',",
|
||||
" 'attachment'",
|
||||
")"
|
||||
],
|
||||
[
|
||||
"INSERT INTO agreego.\"attachment\" (",
|
||||
" \"flags\",",
|
||||
" \"id\",",
|
||||
" \"other_metadata\",",
|
||||
" \"type\",",
|
||||
" \"type_metadata\"",
|
||||
")",
|
||||
"VALUES (",
|
||||
" '{\"urgent\",\"reviewed\"}',",
|
||||
" '{{uuid:attachment_id}}',",
|
||||
" '{\"other\":\"hello\"}',",
|
||||
" 'attachment',",
|
||||
" '{\"type\":\"type_metadata\"}'",
|
||||
")"
|
||||
],
|
||||
[
|
||||
"INSERT INTO agreego.change (",
|
||||
" \"old\",",
|
||||
" \"new\",",
|
||||
" entity_id,",
|
||||
" id,",
|
||||
" kind,",
|
||||
" modified_at,",
|
||||
" modified_by",
|
||||
")",
|
||||
"VALUES (",
|
||||
" NULL,",
|
||||
" '{",
|
||||
" \"flags\":[\"urgent\",\"reviewed\"],",
|
||||
" \"other_metadata\":{\"other\":\"hello\"},",
|
||||
" \"type\":\"attachment\",",
|
||||
" \"type_metadata\":{\"type\":\"type_metadata\"}",
|
||||
" }',",
|
||||
" '{{uuid:attachment_id}}',",
|
||||
" '{{uuid}}',",
|
||||
" 'create',",
|
||||
" '{{timestamp}}',",
|
||||
" '00000000-0000-0000-0000-000000000000'",
|
||||
")"
|
||||
],
|
||||
[
|
||||
"SELECT pg_notify('entity', '{",
|
||||
" \"complete\":{",
|
||||
" \"created_at\":\"{{timestamp}}\",",
|
||||
" \"created_by\":\"00000000-0000-0000-0000-000000000000\",",
|
||||
" \"flags\":[\"urgent\",\"reviewed\"],",
|
||||
" \"id\":\"{{uuid:attachment_id}}\",",
|
||||
" \"modified_at\":\"{{timestamp}}\",",
|
||||
" \"modified_by\":\"00000000-0000-0000-0000-000000000000\",",
|
||||
" \"other_metadata\":{\"other\":\"hello\"},",
|
||||
" \"type\":\"attachment\",",
|
||||
" \"type_metadata\":{\"type\":\"type_metadata\"}",
|
||||
" },",
|
||||
" \"new\":{",
|
||||
" \"flags\":[\"urgent\",\"reviewed\"],",
|
||||
" \"other_metadata\":{\"other\":\"hello\"},",
|
||||
" \"type\":\"attachment\",",
|
||||
" \"type_metadata\":{\"type\":\"type_metadata\"}",
|
||||
" }",
|
||||
" }')"
|
||||
]
|
||||
]
|
||||
}
|
||||
}
|
||||
@ -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,
|
||||
|
||||
@ -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;
|
||||
@ -175,7 +176,7 @@ impl Merger {
|
||||
|
||||
// Attempt to extract relative object type name
|
||||
let relative_type_name = match relative.get("type").and_then(|v| v.as_str()) {
|
||||
Some(t) => t,
|
||||
Some(t) => t.to_string(),
|
||||
None => continue,
|
||||
};
|
||||
|
||||
@ -184,7 +185,7 @@ impl Merger {
|
||||
// Call central Database O(1) graph logic
|
||||
let relative_relation = self.db.get_relation(
|
||||
&type_def.name,
|
||||
relative_type_name,
|
||||
&relative_type_name,
|
||||
&relation_name,
|
||||
Some(&relative_keys),
|
||||
);
|
||||
@ -199,11 +200,16 @@ impl Merger {
|
||||
}
|
||||
}
|
||||
|
||||
let merged_relative = match self.merge_internal(Value::Object(relative), notifications)? {
|
||||
let mut merged_relative = match self.merge_internal(Value::Object(relative), notifications)? {
|
||||
Value::Object(m) => m,
|
||||
_ => continue,
|
||||
};
|
||||
|
||||
merged_relative.insert(
|
||||
"type".to_string(),
|
||||
Value::String(relative_type_name),
|
||||
);
|
||||
|
||||
Self::apply_entity_relation(
|
||||
&mut entity_fields,
|
||||
&relation.source_columns,
|
||||
@ -321,8 +327,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(),
|
||||
@ -620,11 +627,7 @@ impl Merger {
|
||||
for key in &sorted_keys {
|
||||
columns.push(format!("\"{}\"", key));
|
||||
let val = entity_pairs.get(key).unwrap();
|
||||
if val.as_str() == Some("") {
|
||||
values.push("NULL".to_string());
|
||||
} else {
|
||||
values.push(Self::quote_literal(val));
|
||||
}
|
||||
values.push(Self::format_sql_value(val, key, entity_type));
|
||||
}
|
||||
|
||||
if columns.is_empty() {
|
||||
@ -658,7 +661,11 @@ impl Merger {
|
||||
if val.as_str() == Some("") {
|
||||
set_clauses.push(format!("\"{}\" = NULL", key));
|
||||
} else {
|
||||
set_clauses.push(format!("\"{}\" = {}", key, Self::quote_literal(val)));
|
||||
set_clauses.push(format!(
|
||||
"\"{}\" = {}",
|
||||
key,
|
||||
Self::format_sql_value(val, key, entity_type)
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
@ -680,6 +687,7 @@ impl Merger {
|
||||
|
||||
fn merge_entity_change(
|
||||
&self,
|
||||
type_obj: &Type,
|
||||
entity_fields: &serde_json::Map<String, Value>,
|
||||
entity_fetched: Option<&serde_json::Map<String, Value>>,
|
||||
entity_change_kind: Option<&str>,
|
||||
@ -694,7 +702,8 @@ impl Merger {
|
||||
let id_str = entity_fields.get("id").unwrap();
|
||||
let type_name = entity_fields.get("type").unwrap();
|
||||
|
||||
let mut changes = serde_json::Map::new();
|
||||
let mut old_vals = serde_json::Map::new();
|
||||
let mut new_vals = serde_json::Map::new();
|
||||
let is_update = change_kind == "update" || change_kind == "delete";
|
||||
|
||||
if !is_update {
|
||||
@ -707,7 +716,7 @@ impl Merger {
|
||||
];
|
||||
for (k, v) in entity_fields {
|
||||
if !system_keys.contains(k) {
|
||||
changes.insert(k.clone(), v.clone());
|
||||
new_vals.insert(k.clone(), v.clone());
|
||||
}
|
||||
}
|
||||
} else {
|
||||
@ -724,12 +733,13 @@ impl Merger {
|
||||
if let Some(fetched) = entity_fetched {
|
||||
let old_val = fetched.get(k).unwrap_or(&Value::Null);
|
||||
if v != old_val {
|
||||
changes.insert(k.clone(), v.clone());
|
||||
new_vals.insert(k.clone(), v.clone());
|
||||
old_vals.insert(k.clone(), old_val.clone());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
changes.insert("type".to_string(), type_name.clone());
|
||||
new_vals.insert("type".to_string(), type_name.clone());
|
||||
}
|
||||
|
||||
let mut complete = entity_fields.clone();
|
||||
@ -743,33 +753,48 @@ impl Merger {
|
||||
}
|
||||
}
|
||||
|
||||
let new_val_obj = Value::Object(new_vals);
|
||||
let old_val_obj = if old_vals.is_empty() {
|
||||
Value::Null
|
||||
} else {
|
||||
Value::Object(old_vals)
|
||||
};
|
||||
|
||||
let mut notification = serde_json::Map::new();
|
||||
notification.insert("complete".to_string(), Value::Object(complete));
|
||||
if is_update {
|
||||
notification.insert("changes".to_string(), Value::Object(changes.clone()));
|
||||
notification.insert("new".to_string(), new_val_obj.clone());
|
||||
|
||||
if old_val_obj != Value::Null {
|
||||
notification.insert("old".to_string(), old_val_obj.clone());
|
||||
}
|
||||
|
||||
let change_sql = format!(
|
||||
"INSERT INTO agreego.change (changes, entity_id, id, kind, modified_at, modified_by) VALUES ({}, {}, {}, {}, {}, {})",
|
||||
Self::quote_literal(&Value::Object(changes)),
|
||||
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(
|
||||
@ -821,6 +846,34 @@ impl Merger {
|
||||
}
|
||||
}
|
||||
|
||||
fn format_sql_value(val: &Value, key: &str, entity_type: &Type) -> String {
|
||||
if val.as_str() == Some("") {
|
||||
return "NULL".to_string();
|
||||
}
|
||||
|
||||
let mut is_pg_array = false;
|
||||
if let Some(field_types_map) = entity_type.field_types.as_ref().and_then(|v| v.as_object()) {
|
||||
if let Some(t_val) = field_types_map.get(key) {
|
||||
if let Some(t_str) = t_val.as_str() {
|
||||
if t_str.starts_with('_') {
|
||||
is_pg_array = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if is_pg_array && val.is_array() {
|
||||
let mut s = val.to_string();
|
||||
if s.starts_with('[') && s.ends_with(']') {
|
||||
s.replace_range(0..1, "{");
|
||||
s.replace_range(s.len() - 1..s.len(), "}");
|
||||
}
|
||||
Self::quote_literal(&Value::String(s))
|
||||
} else {
|
||||
Self::quote_literal(val)
|
||||
}
|
||||
}
|
||||
|
||||
fn quote_literal(val: &Value) -> String {
|
||||
match val {
|
||||
Value::Null => "NULL".to_string(),
|
||||
|
||||
@ -8536,3 +8536,9 @@ fn test_merger_0_7() {
|
||||
let path = format!("{}/fixtures/merger.json", env!("CARGO_MANIFEST_DIR"));
|
||||
crate::tests::runner::run_test_case(&path, 0, 7).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_merger_0_8() {
|
||||
let path = format!("{}/fixtures/merger.json", env!("CARGO_MANIFEST_DIR"));
|
||||
crate::tests::runner::run_test_case(&path, 0, 8).unwrap();
|
||||
}
|
||||
|
||||
@ -13,28 +13,40 @@ impl<'a> ValidationContext<'a> {
|
||||
) -> Result<bool, ValidationError> {
|
||||
let current = self.instance;
|
||||
if let Some(obj) = current.as_object() {
|
||||
// Entity Bound Implicit Type Validation
|
||||
if let Some(lookup_key) = self.schema.id.as_ref().or(self.schema.r#ref.as_ref()) {
|
||||
let base_type_name = lookup_key.split('.').next_back().unwrap_or("").to_string();
|
||||
if let Some(type_def) = self.db.types.get(&base_type_name)
|
||||
&& let Some(type_val) = obj.get("type")
|
||||
// Entity implicit type validation
|
||||
// Use the specific schema id or ref as a fallback
|
||||
if let Some(identifier) = self.schema.id.as_ref().or(self.schema.r#ref.as_ref()) {
|
||||
// Kick in if the data object has a type field
|
||||
if let Some(type_val) = obj.get("type")
|
||||
&& let Some(type_str) = type_val.as_str()
|
||||
{
|
||||
if type_def.variations.contains(type_str) {
|
||||
// Ensure it passes strict mode
|
||||
result.evaluated_keys.insert("type".to_string());
|
||||
// Get the string or the final segment as the base
|
||||
let base = identifier.split('.').next_back().unwrap_or("").to_string();
|
||||
// Check if the base is a global type name
|
||||
if let Some(type_def) = self.db.types.get(&base) {
|
||||
// Ensure the instance type is a variation of the global type
|
||||
if type_def.variations.contains(type_str) {
|
||||
// Ensure it passes strict mode
|
||||
result.evaluated_keys.insert("type".to_string());
|
||||
} else {
|
||||
result.errors.push(ValidationError {
|
||||
code: "CONST_VIOLATED".to_string(), // Aligning with original const override errors
|
||||
message: format!(
|
||||
"Type '{}' is not a valid descendant for this entity bound schema",
|
||||
type_str
|
||||
),
|
||||
path: format!("{}/type", self.path),
|
||||
});
|
||||
}
|
||||
} else {
|
||||
result.errors.push(ValidationError {
|
||||
code: "CONST_VIOLATED".to_string(), // Aligning with original const override errors
|
||||
message: format!(
|
||||
"Type '{}' is not a valid descendant for this entity bound schema",
|
||||
type_str
|
||||
),
|
||||
path: format!("{}/type", self.path),
|
||||
});
|
||||
// Ad-Hoc schemas natively use strict schema discriminator strings instead of variation inheritance
|
||||
if type_str == identifier {
|
||||
result.evaluated_keys.insert("type".to_string());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(min) = self.schema.min_properties
|
||||
&& (obj.len() as f64) < min
|
||||
{
|
||||
@ -44,6 +56,7 @@ impl<'a> ValidationContext<'a> {
|
||||
path: self.path.to_string(),
|
||||
});
|
||||
}
|
||||
|
||||
if let Some(max) = self.schema.max_properties
|
||||
&& (obj.len() as f64) > max
|
||||
{
|
||||
@ -53,6 +66,7 @@ impl<'a> ValidationContext<'a> {
|
||||
path: self.path.to_string(),
|
||||
});
|
||||
}
|
||||
|
||||
if let Some(ref req) = self.schema.required {
|
||||
for field in req {
|
||||
if !obj.contains_key(field) {
|
||||
@ -114,10 +128,14 @@ impl<'a> ValidationContext<'a> {
|
||||
|
||||
// Entity Bound Implicit Type Interception
|
||||
if key == "type"
|
||||
&& let Some(lookup_key) = sub_schema.id.as_ref().or(sub_schema.r#ref.as_ref())
|
||||
&& let Some(schema_bound) = sub_schema.id.as_ref().or(sub_schema.r#ref.as_ref())
|
||||
{
|
||||
let base_type_name = lookup_key.split('.').next_back().unwrap_or("").to_string();
|
||||
if let Some(type_def) = self.db.types.get(&base_type_name)
|
||||
let physical_type_name = schema_bound
|
||||
.split('.')
|
||||
.next_back()
|
||||
.unwrap_or("")
|
||||
.to_string();
|
||||
if let Some(type_def) = self.db.types.get(&physical_type_name)
|
||||
&& let Some(instance_type) = child_instance.as_str()
|
||||
&& type_def.variations.contains(instance_type)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user