Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 75aac41362 | |||
| dbcef42401 | |||
| b6c5561d2f | |||
| e01b778d68 | |||
| 6eb134c0d6 |
@ -715,6 +715,74 @@
|
|||||||
"historical": true,
|
"historical": true,
|
||||||
"notify": true,
|
"notify": true,
|
||||||
"relationship": false
|
"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
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
@ -2190,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\"",
|
||||||
|
" }",
|
||||||
|
" }')"
|
||||||
|
]
|
||||||
|
]
|
||||||
|
}
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|||||||
@ -176,7 +176,7 @@ impl Merger {
|
|||||||
|
|
||||||
// Attempt to extract relative object type name
|
// Attempt to extract relative object type name
|
||||||
let relative_type_name = match relative.get("type").and_then(|v| v.as_str()) {
|
let relative_type_name = match relative.get("type").and_then(|v| v.as_str()) {
|
||||||
Some(t) => t,
|
Some(t) => t.to_string(),
|
||||||
None => continue,
|
None => continue,
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -185,7 +185,7 @@ impl Merger {
|
|||||||
// Call central Database O(1) graph logic
|
// Call central Database O(1) graph logic
|
||||||
let relative_relation = self.db.get_relation(
|
let relative_relation = self.db.get_relation(
|
||||||
&type_def.name,
|
&type_def.name,
|
||||||
relative_type_name,
|
&relative_type_name,
|
||||||
&relation_name,
|
&relation_name,
|
||||||
Some(&relative_keys),
|
Some(&relative_keys),
|
||||||
);
|
);
|
||||||
@ -200,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,
|
Value::Object(m) => m,
|
||||||
_ => continue,
|
_ => continue,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
merged_relative.insert(
|
||||||
|
"type".to_string(),
|
||||||
|
Value::String(relative_type_name),
|
||||||
|
);
|
||||||
|
|
||||||
Self::apply_entity_relation(
|
Self::apply_entity_relation(
|
||||||
&mut entity_fields,
|
&mut entity_fields,
|
||||||
&relation.source_columns,
|
&relation.source_columns,
|
||||||
@ -622,11 +627,7 @@ impl Merger {
|
|||||||
for key in &sorted_keys {
|
for key in &sorted_keys {
|
||||||
columns.push(format!("\"{}\"", key));
|
columns.push(format!("\"{}\"", key));
|
||||||
let val = entity_pairs.get(key).unwrap();
|
let val = entity_pairs.get(key).unwrap();
|
||||||
if val.as_str() == Some("") {
|
values.push(Self::format_sql_value(val, key, entity_type));
|
||||||
values.push("NULL".to_string());
|
|
||||||
} else {
|
|
||||||
values.push(Self::quote_literal(val));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if columns.is_empty() {
|
if columns.is_empty() {
|
||||||
@ -660,7 +661,11 @@ impl Merger {
|
|||||||
if val.as_str() == Some("") {
|
if val.as_str() == Some("") {
|
||||||
set_clauses.push(format!("\"{}\" = NULL", key));
|
set_clauses.push(format!("\"{}\" = NULL", key));
|
||||||
} else {
|
} else {
|
||||||
set_clauses.push(format!("\"{}\" = {}", key, Self::quote_literal(val)));
|
set_clauses.push(format!(
|
||||||
|
"\"{}\" = {}",
|
||||||
|
key,
|
||||||
|
Self::format_sql_value(val, key, entity_type)
|
||||||
|
));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -841,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 {
|
fn quote_literal(val: &Value) -> String {
|
||||||
match val {
|
match val {
|
||||||
Value::Null => "NULL".to_string(),
|
Value::Null => "NULL".to_string(),
|
||||||
|
|||||||
@ -8536,3 +8536,9 @@ fn test_merger_0_7() {
|
|||||||
let path = format!("{}/fixtures/merger.json", env!("CARGO_MANIFEST_DIR"));
|
let path = format!("{}/fixtures/merger.json", env!("CARGO_MANIFEST_DIR"));
|
||||||
crate::tests::runner::run_test_case(&path, 0, 7).unwrap();
|
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