Compare commits
7 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 75aac41362 | |||
| dbcef42401 | |||
| b6c5561d2f | |||
| e01b778d68 | |||
| 6eb134c0d6 | |||
| 7ccc4b7cce | |||
| 77bfa4cd18 |
@ -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,75 @@
|
||||
],
|
||||
"lookup_fields": [],
|
||||
"historical": true,
|
||||
"notify": true,
|
||||
"relationship": false
|
||||
},
|
||||
{
|
||||
"name": "attachment",
|
||||
"schemas": [
|
||||
{
|
||||
"$id": "attachment",
|
||||
"$ref": "entity",
|
||||
"properties": {
|
||||
"flags": {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
"metadata": {
|
||||
"type": "object",
|
||||
"additionalProperties": true
|
||||
}
|
||||
}
|
||||
}
|
||||
],
|
||||
"hierarchy": [
|
||||
"attachment",
|
||||
"entity"
|
||||
],
|
||||
"fields": [
|
||||
"id",
|
||||
"type",
|
||||
"flags",
|
||||
"metadata",
|
||||
"created_at",
|
||||
"created_by",
|
||||
"modified_at",
|
||||
"modified_by",
|
||||
"archived"
|
||||
],
|
||||
"grouped_fields": {
|
||||
"attachment": [
|
||||
"id",
|
||||
"type",
|
||||
"flags",
|
||||
"metadata"
|
||||
],
|
||||
"entity": [
|
||||
"id",
|
||||
"type",
|
||||
"created_at",
|
||||
"created_by",
|
||||
"modified_at",
|
||||
"modified_by",
|
||||
"archived"
|
||||
]
|
||||
},
|
||||
"field_types": {
|
||||
"id": "uuid",
|
||||
"type": "text",
|
||||
"flags": "_text",
|
||||
"metadata": "jsonb",
|
||||
"created_at": "timestamptz",
|
||||
"created_by": "uuid",
|
||||
"modified_at": "timestamptz",
|
||||
"modified_by": "uuid",
|
||||
"archived": "boolean"
|
||||
},
|
||||
"lookup_fields": [],
|
||||
"historical": true,
|
||||
"notify": true,
|
||||
"relationship": false
|
||||
}
|
||||
]
|
||||
@ -2180,6 +2258,101 @@
|
||||
]
|
||||
]
|
||||
}
|
||||
},
|
||||
{
|
||||
"description": "Insert attachment displaying side-by-side array literal and jsonb formatting translations",
|
||||
"action": "merge",
|
||||
"data": {
|
||||
"type": "attachment",
|
||||
"flags": [
|
||||
"urgent",
|
||||
"reviewed"
|
||||
],
|
||||
"metadata": {
|
||||
"size": 1024,
|
||||
"source": "upload"
|
||||
}
|
||||
},
|
||||
"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\",",
|
||||
" \"metadata\",",
|
||||
" \"type\"",
|
||||
")",
|
||||
"VALUES (",
|
||||
" '{\"urgent\",\"reviewed\"}',",
|
||||
" '{{uuid:attachment_id}}',",
|
||||
" '{\"size\":1024,\"source\":\"upload\"}',",
|
||||
" 'attachment'",
|
||||
")"
|
||||
],
|
||||
[
|
||||
"INSERT INTO agreego.change (",
|
||||
" \"old\",",
|
||||
" \"new\",",
|
||||
" entity_id,",
|
||||
" id,",
|
||||
" kind,",
|
||||
" modified_at,",
|
||||
" modified_by",
|
||||
")",
|
||||
"VALUES (",
|
||||
" NULL,",
|
||||
" '{",
|
||||
" \"flags\":[\"urgent\",\"reviewed\"],",
|
||||
" \"metadata\":{\"size\":1024,\"source\":\"upload\"},",
|
||||
" \"type\":\"attachment\"",
|
||||
" }',",
|
||||
" '{{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}}\",",
|
||||
" \"metadata\":{\"size\":1024,\"source\":\"upload\"},",
|
||||
" \"modified_at\":\"{{timestamp}}\",",
|
||||
" \"modified_by\":\"00000000-0000-0000-0000-000000000000\",",
|
||||
" \"type\":\"attachment\"",
|
||||
" },",
|
||||
" \"new\":{",
|
||||
" \"flags\":[\"urgent\",\"reviewed\"],",
|
||||
" \"metadata\":{\"size\":1024,\"source\":\"upload\"},",
|
||||
" \"type\":\"attachment\"",
|
||||
" }",
|
||||
" }')"
|
||||
]
|
||||
]
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
@ -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>,
|
||||
@ -760,28 +768,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(
|
||||
@ -833,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();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user