From a1038490dd03cb9efc38bc95a147f377b2d91ecf Mon Sep 17 00:00:00 2001 From: Alex Groleau Date: Mon, 23 Mar 2026 12:05:34 -0400 Subject: [PATCH] tested nested merging with anchors --- d1.json | 819 ------------------------------------------ fixtures/merger.json | 101 ++++++ src/merger/mod.rs | 17 + src/tests/fixtures.rs | 6 + 4 files changed, 124 insertions(+), 819 deletions(-) delete mode 100644 d1.json diff --git a/d1.json b/d1.json deleted file mode 100644 index b5be71c..0000000 --- a/d1.json +++ /dev/null @@ -1,819 +0,0 @@ -{ - "database": { - "puncs": [], - "enums": [ - { - "id": "11111111-1111-1111-1111-111111111111", - "type": "relation_type", - "enum": "relation_type", - "values": [ - "foreign_key", - "polymorphic", - "graph" - ] - } - ], - "relations": [ - { - "id": "22222222-2222-2222-2222-222222222222", - "type": "relation", - "constraint": "fk_order_customer", - "source_type": "order", - "source_columns": [ - "customer_id" - ], - "destination_type": "person", - "destination_columns": [ - "id" - ], - "prefix": "customer" - }, - { - "id": "33333333-3333-3333-3333-333333333333", - "type": "relation", - "constraint": "fk_order_line_order", - "source_type": "order_line", - "source_columns": [ - "order_id" - ], - "destination_type": "order", - "destination_columns": [ - "id" - ], - "prefix": "lines" - }, - { - "id": "44444444-4444-4444-4444-444444444444", - "type": "relation", - "constraint": "fk_relationship_source_entity", - "source_type": "relationship", - "source_columns": [ - "source_id", - "source_type" - ], - "destination_type": "entity", - "destination_columns": [ - "id", - "type" - ], - "prefix": "source" - }, - { - "id": "55555555-5555-5555-5555-555555555555", - "type": "relation", - "constraint": "fk_relationship_target_entity", - "source_type": "relationship", - "source_columns": [ - "target_id", - "target_type" - ], - "destination_type": "entity", - "destination_columns": [ - "id", - "type" - ], - "prefix": "target" - } - ], - "types": [ - { - "name": "entity", - "schemas": [ - { - "$id": "entity", - "type": "object", - "properties": { - "id": { - "type": "string" - }, - "type": { - "type": "string" - }, - "archived": { - "type": "boolean" - }, - "created_by": { - "type": "string" - }, - "modified_by": { - "type": "string" - }, - "created_at": { - "type": "string", - "format": "date-time" - }, - "modified_at": { - "type": "string", - "format": "date-time" - } - }, - "required": [ - "id", - "type", - "created_by", - "created_at", - "modified_by", - "modified_at" - ] - } - ], - "hierarchy": [ - "entity" - ], - "fields": [ - "id", - "type", - "created_at", - "created_by", - "modified_at", - "modified_by", - "archived" - ], - "grouped_fields": { - "entity": [ - "id", - "type", - "created_at", - "created_by", - "modified_at", - "modified_by", - "archived" - ] - }, - "lookup_fields": [], - "historical": true, - "notify": true, - "relationship": false - }, - { - "name": "organization", - "schemas": [ - { - "$id": "organization", - "$ref": "entity", - "properties": { - "name": { - "type": "string" - } - } - } - ], - "hierarchy": [ - "organization", - "entity" - ], - "fields": [ - "id", - "type", - "name", - "created_at", - "created_by", - "modified_at", - "modified_by", - "archived" - ], - "grouped_fields": { - "organization": [ - "id", - "type", - "name" - ], - "entity": [ - "id", - "type", - "created_at", - "created_by", - "modified_at", - "modified_by", - "archived" - ] - }, - "lookup_fields": [], - "historical": true, - "notify": true, - "relationship": false - }, - { - "name": "user", - "schemas": [ - { - "$id": "user", - "$ref": "organization", - "properties": {} - } - ], - "hierarchy": [ - "user", - "organization", - "entity" - ], - "fields": [ - "id", - "type", - "name", - "created_at", - "created_by", - "modified_at", - "modified_by", - "archived" - ], - "grouped_fields": { - "user": [ - "id", - "type" - ], - "organization": [ - "id", - "type", - "name" - ], - "entity": [ - "id", - "type", - "created_at", - "created_by", - "modified_at", - "modified_by", - "archived" - ] - }, - "lookup_fields": [], - "historical": true, - "notify": true, - "relationship": false - }, - { - "name": "person", - "schemas": [ - { - "$id": "person", - "$ref": "user", - "properties": { - "first_name": { - "type": "string" - }, - "last_name": { - "type": "string" - }, - "date_of_birth": { - "type": "string" - }, - "pronouns": { - "type": "string" - }, - "contact_id": { - "type": "string" - }, - "contacts": { - "type": "array", - "items": { - "$ref": "contact", - "properties": { - "target": { - "oneOf": [ - { - "$ref": "phone_number" - }, - { - "$ref": "email_address" - } - ] - } - } - } - } - } - } - ], - "hierarchy": [ - "person", - "user", - "organization", - "entity" - ], - "fields": [ - "id", - "type", - "first_name", - "last_name", - "date_of_birth", - "pronouns", - "contact_id", - "name", - "created_at", - "created_by", - "modified_at", - "modified_by", - "archived" - ], - "grouped_fields": { - "person": [ - "id", - "type", - "first_name", - "last_name", - "date_of_birth", - "pronouns", - "contact_id" - ], - "user": [ - "id", - "type" - ], - "organization": [ - "id", - "type", - "name" - ], - "entity": [ - "id", - "type", - "created_at", - "created_by", - "modified_at", - "modified_by", - "archived" - ] - }, - "lookup_fields": [ - "first_name", - "last_name", - "date_of_birth", - "pronouns" - ], - "historical": true, - "notify": true, - "relationship": false - }, - { - "name": "order", - "schemas": [ - { - "$id": "order", - "$ref": "entity", - "properties": { - "total": { - "type": "number" - }, - "customer_id": { - "type": "string" - }, - "customer": { - "$ref": "person" - }, - "lines": { - "type": "array", - "items": { - "$ref": "order_line" - } - } - } - } - ], - "hierarchy": [ - "order", - "entity" - ], - "fields": [ - "id", - "type", - "total", - "customer_id", - "created_at", - "created_by", - "modified_at", - "modified_by", - "archived" - ], - "grouped_fields": { - "order": [ - "id", - "type", - "total", - "customer_id" - ], - "entity": [ - "id", - "type", - "created_at", - "created_by", - "modified_at", - "modified_by", - "archived" - ] - }, - "lookup_fields": [ - "id" - ], - "historical": true, - "notify": true, - "relationship": false - }, - { - "name": "order_line", - "schemas": [ - { - "$id": "order_line", - "$ref": "entity", - "properties": { - "order_id": { - "type": "string" - }, - "product": { - "type": "string" - }, - "price": { - "type": "number" - } - } - } - ], - "hierarchy": [ - "order_line", - "entity" - ], - "fields": [ - "id", - "type", - "order_id", - "product", - "price", - "created_at", - "created_by", - "modified_at", - "modified_by", - "archived" - ], - "grouped_fields": { - "order_line": [ - "id", - "type", - "order_id", - "product", - "price" - ], - "entity": [ - "id", - "type", - "created_at", - "created_by", - "modified_at", - "modified_by", - "archived" - ] - }, - "lookup_fields": [], - "historical": true, - "notify": true, - "relationship": false - }, - { - "name": "relationship", - "relationship": true, - "hierarchy": [ - "relationship", - "entity" - ], - "fields": [ - "source_id", - "source_type", - "target_id", - "target_type", - "id", - "type", - "name", - "archived", - "created_at", - "created_by", - "modified_at", - "modified_by" - ], - "grouped_fields": { - "entity": [ - "id", - "type", - "name", - "archived", - "created_at", - "created_by", - "modified_at", - "modified_by" - ], - "relationship": [ - "source_id", - "source_type", - "target_id", - "target_type" - ] - }, - "field_types": { - "id": "uuid", - "type": "text", - "archived": "boolean", - "source_id": "uuid", - "source_type": "text", - "target_id": "uuid", - "target_type": "text", - "name": "text", - "created_at": "timestamptz", - "created_by": "uuid", - "modified_at": "timestamptz", - "modified_by": "uuid" - }, - "schemas": [ - { - "$id": "relationship", - "$ref": "entity", - "properties": {} - } - ], - "lookup_fields": [], - "historical": true, - "notify": true - }, - { - "name": "contact", - "relationship": true, - "hierarchy": [ - "contact", - "relationship", - "entity" - ], - "fields": [ - "is_primary", - "source_id", - "source_type", - "target_id", - "target_type", - "id", - "type", - "name", - "archived", - "created_at", - "created_by", - "modified_at", - "modified_by" - ], - "grouped_fields": { - "entity": [ - "id", - "type", - "name", - "archived", - "created_at", - "created_by", - "modified_at", - "modified_by" - ], - "relationship": [ - "source_id", - "source_type", - "target_id", - "target_type" - ], - "contact": [ - "is_primary" - ] - }, - "field_types": { - "id": "uuid", - "type": "text", - "archived": "boolean", - "source_id": "uuid", - "source_type": "text", - "target_id": "uuid", - "target_type": "text", - "is_primary": "boolean", - "name": "text", - "created_at": "timestamptz", - "created_by": "uuid", - "modified_at": "timestamptz", - "modified_by": "uuid" - }, - "schemas": [ - { - "$id": "contact", - "$ref": "relationship", - "properties": { - "is_primary": { - "type": "boolean" - } - } - } - ], - "lookup_fields": [], - "historical": true, - "notify": true - }, - { - "name": "phone_number", - "hierarchy": [ - "phone_number", - "entity" - ], - "fields": [ - "number", - "id", - "type", - "name", - "archived", - "created_at", - "created_by", - "modified_at", - "modified_by" - ], - "grouped_fields": { - "entity": [ - "id", - "type", - "name", - "archived", - "created_at", - "created_by", - "modified_at", - "modified_by" - ], - "phone_number": [ - "number" - ] - }, - "field_types": { - "id": "uuid", - "type": "text", - "archived": "boolean", - "number": "text", - "name": "text", - "created_at": "timestamptz", - "created_by": "uuid", - "modified_at": "timestamptz", - "modified_by": "uuid" - }, - "schemas": [ - { - "$id": "phone_number", - "$ref": "entity", - "properties": { - "number": { - "type": "string" - } - } - } - ], - "lookup_fields": [], - "historical": true, - "notify": true, - "relationship": false - }, - { - "name": "email_address", - "hierarchy": [ - "email_address", - "entity" - ], - "fields": [ - "address", - "id", - "type", - "name", - "archived", - "created_at", - "created_by", - "modified_at", - "modified_by" - ], - "grouped_fields": { - "entity": [ - "id", - "type", - "name", - "archived", - "created_at", - "created_by", - "modified_at", - "modified_by" - ], - "email_address": [ - "address" - ] - }, - "field_types": { - "id": "uuid", - "type": "text", - "archived": "boolean", - "address": "text", - "name": "text", - "created_at": "timestamptz", - "created_by": "uuid", - "modified_at": "timestamptz", - "modified_by": "uuid" - }, - "schemas": [ - { - "$id": "email_address", - "$ref": "entity", - "properties": { - "address": { - "type": "string" - } - } - } - ], - "lookup_fields": [], - "historical": true, - "notify": true, - "relationship": false - }, - { - "name": "attachment", - "schemas": [ - { - "$id": "type_metadata", - "type": "object", - "properties": { - "type": { - "type": "string" - } - } - }, - { - "$id": "other_metadata", - "type": "object", - "properties": { - "other": { - "type": "string" - } - } - }, - { - "$id": "attachment", - "$ref": "entity", - "properties": { - "flags": { - "type": "array", - "items": { - "type": "string" - } - }, - "type_metadata": { - "$ref": "type_metadata" - }, - "other_metadata": { - "$ref": "other_metadata" - } - } - } - ], - "hierarchy": [ - "attachment", - "entity" - ], - "fields": [ - "id", - "type", - "flags", - "type_metadata", - "other_metadata", - "created_at", - "created_by", - "modified_at", - "modified_by", - "archived" - ], - "grouped_fields": { - "attachment": [ - "id", - "type", - "flags", - "type_metadata", - "other_metadata" - ], - "entity": [ - "id", - "type", - "created_at", - "created_by", - "modified_at", - "modified_by", - "archived" - ] - }, - "field_types": { - "id": "uuid", - "type": "text", - "flags": "_text", - "type_metadata": "jsonb", - "other_metadata": "jsonb", - "created_at": "timestamptz", - "created_by": "uuid", - "modified_at": "timestamptz", - "modified_by": "uuid", - "archived": "boolean" - }, - "lookup_fields": [], - "historical": true, - "notify": true, - "relationship": false - } - ] - } -} diff --git a/fixtures/merger.json b/fixtures/merger.json index 96ac37d..fb08267 100644 --- a/fixtures/merger.json +++ b/fixtures/merger.json @@ -2401,6 +2401,107 @@ ] ] } + }, + { + "description": "Anchor order and insert new line", + "action": "merge", + "data": { + "id": "abc", + "type": "order", + "lines": [ + { + "type": "order_line", + "product": "Widget", + "price": 99.0 + } + ] + }, + "schema_id": "order", + "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:line_id}}',", + " '{{timestamp}}',", + " '00000000-0000-0000-0000-000000000000',", + " 'order_line'", + ")" + ], + [ + "INSERT INTO agreego.\"order_line\" (", + " \"id\",", + " \"order_id\",", + " \"price\",", + " \"product\",", + " \"type\"", + ")", + "VALUES (", + " '{{uuid:line_id}}',", + " 'abc',", + " 99,", + " 'Widget',", + " 'order_line'", + ")" + ], + [ + "INSERT INTO agreego.change (", + " \"old\",", + " \"new\",", + " entity_id,", + " id,", + " kind,", + " modified_at,", + " modified_by", + ")", + "VALUES (", + " NULL,", + " '{", + " \"order_id\":\"abc\",", + " \"price\":99.0,", + " \"product\":\"Widget\",", + " \"type\":\"order_line\"", + " }',", + " '{{uuid:line_id}}',", + " '{{uuid}}',", + " 'create',", + " '{{timestamp}}',", + " '00000000-0000-0000-0000-000000000000'", + ")" + ], + [ + "SELECT pg_notify('entity', '{", + " \"complete\":{", + " \"created_at\":\"{{timestamp}}\",", + " \"created_by\":\"00000000-0000-0000-0000-000000000000\",", + " \"id\":\"{{uuid:line_id}}\",", + " \"modified_at\":\"{{timestamp}}\",", + " \"modified_by\":\"00000000-0000-0000-0000-000000000000\",", + " \"order_id\":\"abc\",", + " \"price\":99.0,", + " \"product\":\"Widget\",", + " \"type\":\"order_line\"", + " },", + " \"new\":{", + " \"order_id\":\"abc\",", + " \"price\":99.0,", + " \"product\":\"Widget\",", + " \"type\":\"order_line\"", + " }", + " }')" + ] + ] + } } ] } diff --git a/src/merger/mod.rs b/src/merger/mod.rs index 4d9bf2b..38d13b2 100644 --- a/src/merger/mod.rs +++ b/src/merger/mod.rs @@ -403,6 +403,23 @@ impl Merger { > { let type_name = type_def.name.as_str(); + // 🚀 Anchor Short-Circuit Optimization + // An anchor is STRICTLY a struct containing merely an `id` and `type`. + // We aggressively bypass Database SPI `SELECT` fetches because there are no primitive + // mutations to apply to the row. PostgreSQL inherently protects relationships via Foreign Keys downstream. + let is_anchor = entity_fields.len() == 2 + && entity_fields.contains_key("id") + && entity_fields.contains_key("type"); + + let has_valid_id = entity_fields + .get("id") + .and_then(|v| v.as_str()) + .map_or(false, |s| !s.is_empty()); + + if is_anchor && has_valid_id { + return Ok((entity_fields, None, None)); + } + let entity_fetched = self.fetch_entity(&entity_fields, type_def)?; let system_keys = vec![ diff --git a/src/tests/fixtures.rs b/src/tests/fixtures.rs index 7bd1c65..b541eef 100644 --- a/src/tests/fixtures.rs +++ b/src/tests/fixtures.rs @@ -8548,3 +8548,9 @@ 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(); } + +#[test] +fn test_merger_0_9() { + let path = format!("{}/fixtures/merger.json", env!("CARGO_MANIFEST_DIR")); + crate::tests::runner::run_test_case(&path, 0, 9).unwrap(); +}