diff --git a/fixtures/merger.json b/fixtures/merger.json index 5ce4e41..eafb4db 100644 --- a/fixtures/merger.json +++ b/fixtures/merger.json @@ -132,6 +132,15 @@ "columns": [ "name" ] + }, + { + "table": "relationship", + "columns": [ + "type", + "source_id", + "target_id", + "start_date" + ] } ], "types": [ @@ -573,6 +582,8 @@ "source_type", "target_id", "target_type", + "start_date", + "end_date", "id", "type", "name", @@ -597,7 +608,9 @@ "source_id", "source_type", "target_id", - "target_type" + "target_type", + "start_date", + "end_date" ] }, "field_types": { @@ -608,6 +621,8 @@ "source_type": "text", "target_id": "uuid", "target_type": "text", + "start_date": "timestamptz", + "end_date": "timestamptz", "name": "text", "created_at": "timestamptz", "created_by": "uuid", @@ -620,7 +635,16 @@ "properties": {} } }, - "lookup_fields": [], + "lookup_fields": [ + "type", + "source_id", + "target_id", + "start_date" + ], + "field_defaults": { + "start_date": "0001-01-01T00:00:00Z", + "end_date": "9999-12-31T23:59:59Z" + }, "historical": true, "notify": true }, @@ -2766,6 +2790,17 @@ " '00000000-0000-0000-0000-000000000000'", ")" ], + [ + "(SELECT COALESCE(to_jsonb(t1.*), '{}') || COALESCE(to_jsonb(t2.*), '{}') || COALESCE(to_jsonb(t3.*), '{}')", + "FROM agreego.\"entity\" t1", + "LEFT JOIN agreego.\"relationship\" t2 ON t2.id = t1.id", + "LEFT JOIN agreego.\"contact\" t3 ON t3.id = t1.id", + "WHERE", + " (t1.\"type\" = 'contact'", + " AND \"source_id\" = '{{uuid:generated_0}}'", + " AND \"target_id\" = '{{uuid:generated_1}}'", + " AND \"start_date\" = '{{timestamp}}'))" + ], [ "INSERT INTO agreego.\"entity\" (", " \"created_at\",", @@ -2887,6 +2922,17 @@ " '00000000-0000-0000-0000-000000000000'", ")" ], + [ + "(SELECT COALESCE(to_jsonb(t1.*), '{}') || COALESCE(to_jsonb(t2.*), '{}') || COALESCE(to_jsonb(t3.*), '{}')", + "FROM agreego.\"entity\" t1", + "LEFT JOIN agreego.\"relationship\" t2 ON t2.id = t1.id", + "LEFT JOIN agreego.\"contact\" t3 ON t3.id = t1.id", + "WHERE", + " (t1.\"type\" = 'contact'", + " AND \"source_id\" = '{{uuid:generated_0}}'", + " AND \"target_id\" = '{{uuid:generated_5}}'", + " AND \"start_date\" = '{{timestamp}}'))" + ], [ "INSERT INTO agreego.\"entity\" (", " \"created_at\",", @@ -3008,6 +3054,17 @@ " '00000000-0000-0000-0000-000000000000'", ")" ], + [ + "(SELECT COALESCE(to_jsonb(t1.*), '{}') || COALESCE(to_jsonb(t2.*), '{}') || COALESCE(to_jsonb(t3.*), '{}')", + "FROM agreego.\"entity\" t1", + "LEFT JOIN agreego.\"relationship\" t2 ON t2.id = t1.id", + "LEFT JOIN agreego.\"contact\" t3 ON t3.id = t1.id", + "WHERE", + " (t1.\"type\" = 'contact'", + " AND \"source_id\" = '{{uuid:generated_0}}'", + " AND \"target_id\" = '{{uuid:generated_9}}'", + " AND \"start_date\" = '{{timestamp}}'))" + ], [ "INSERT INTO agreego.\"entity\" (", " \"created_at\",", diff --git a/fixtures/queryer.json b/fixtures/queryer.json index ffe5348..9a8b4d9 100644 --- a/fixtures/queryer.json +++ b/fixtures/queryer.json @@ -260,12 +260,9 @@ }, "lookup_fields": [], "null_fields": [], - "default_fields": [ - "id", - "type", - "created_at", - "archived" - ], + "field_defaults": { + "archived": false + }, "variations": [ "bot", "organization", diff --git a/src/database/type.rs b/src/database/type.rs index e47f852..73ef571 100644 --- a/src/database/type.rs +++ b/src/database/type.rs @@ -36,7 +36,7 @@ pub struct Type { #[serde(default)] pub null_fields: Vec, #[serde(default)] - pub default_fields: Vec, + pub field_defaults: IndexMap, pub field_types: Option, #[serde(default)] pub schemas: IndexMap>, diff --git a/src/merger/mod.rs b/src/merger/mod.rs index 0c1067e..803e71a 100644 --- a/src/merger/mod.rs +++ b/src/merger/mod.rs @@ -335,6 +335,17 @@ impl Merger { } } + // Hydrate missing fields from field_defaults for entity_type and its hierarchy + for parent_type_name in &type_def.hierarchy { + if let Some(parent_type) = self.db.types.get(parent_type_name) { + for (k, v) in &parent_type.field_defaults { + if !entity_fields.contains_key(k) { + entity_fields.insert(k.clone(), v.clone()); + } + } + } + } + let mut current_org_id = None; if let Some(compiled_props) = schema.obj.compiled_properties.get() { if let Some(org_schema) = compiled_props.get("organization_id") { diff --git a/src/tests/mod.rs b/src/tests/mod.rs index fcee8b2..16b4ad9 100644 --- a/src/tests/mod.rs +++ b/src/tests/mod.rs @@ -104,7 +104,7 @@ fn test_library_api() { }, "types": { "source_schema": { - "default_fields": [], + "field_defaults": {}, "field_types": null, "fields": [], "grouped_fields": null, @@ -169,7 +169,7 @@ fn test_library_api() { "variations": ["source_schema"] }, "target_schema": { - "default_fields": [], + "field_defaults": {}, "field_types": null, "fields": [], "grouped_fields": null,