From 66215c7fd69ce20158a89b1d1db3c3db2b1d850f Mon Sep 17 00:00:00 2001 From: Debasish Bose Date: Sun, 1 Nov 2026 18:36:07 +1100 Subject: [PATCH] export: scoped erwin export of one subject area, with GUID preservation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Workflow: import a big erwin mart, iterate on ONE subject area, export just that slice and merge it back into the mart's SA as UPDATES (not duplicates). Part C (identity, done first): the importer now preserves each erwin object's original GUID in a new optional ir.py field erwin_id (on LogicalEntity/Attribute/KeyGroup/Relationship/ SubjectArea/Domain/Category; the entity GUID lives on LogicalEntity since one erwin splits into conceptual+logical). Additive/optional — non-erwin models and existing tests are unaffected. Export's _oid(obj) re-emits erwin_id when present, so erwin matches the object; a Modelith-authored object (no erwin_id) gets a stable synthetic GUID and comes in as NEW. Why GUID not name: the importer lowercases display names, which is irreversible for acronyms (USUBJID->usubjid->'Usubjid'), so name matching would duplicate SDTM key columns. Part A+B (scope + stubs): emit_erwin(model, *, subject_area=) exports only that area's member entities in full. A relationship from an in-scope entity to an outside one is KEPT, and the outside entity is emitted as a minimal STUB (its real GUID + name + business key only) so the FK resolves — in erwin the neighbour auto-includes in the area's view. Unscoped export is byte-identical to before. Surfaces: mdl export erwin --subject-area/-s ; /api/export/erwin?subject_area=. (Canvas Export-menu scoping deferred — CLI+API are the core.) Verified: scope to SA1 with a cross-SA FK -> SA1 entity full, the shared dimension as a BK-only stub with its real GUID, the FK retained; XSD-valid + round-trips, 0 validate errors. 16 emit tests + 3 CLI tests. Full suite 1034 passed. --- packages/cli/src/mdl_cli/main.py | 14 +- packages/cli/tests/test_export_erwin_cli.py | 12 + packages/core/src/mdl_core/ir.py | 24 ++ .../emit-erd/src/mdl_emit_erd/erwin_export.py | 242 ++++++++++++------ packages/emit-erd/tests/test_erwin_export.py | 133 ++++++++++ packages/reverse/src/mdl_reverse/erwin.py | 6 + packages/server/src/mdl_server/app.py | 11 +- 7 files changed, 361 insertions(+), 81 deletions(-) diff --git a/packages/cli/src/mdl_cli/main.py b/packages/cli/src/mdl_cli/main.py index 86ab047..f8d8a0e 100644 --- a/packages/cli/src/mdl_cli/main.py +++ b/packages/cli/src/mdl_cli/main.py @@ -3011,18 +3011,30 @@ def export_dbml_cmd( def export_erwin_cmd( model_dir: Path = typer.Option(Path("."), "--model-dir", "-m"), out: Path = typer.Option(None, "--out", "-o"), + subject_area: str = typer.Option( + None, + "--subject-area", + "-s", + help="Export only this subject area (name or id); FKs to entities outside it are kept, " + "and those entities are emitted as stubs so the FK resolves. Default: the whole model.", + ), ) -> None: """Export erwin XML (validates against the erwin XSD; round-trips via `mdl import erwin`). Targets the published erwin XSD and Modelith's own importer; real erwin Data Modeler open-ability is not guaranteed. A team naming convention for the exported display names is opt-in via the `export.erwin.naming` block in mdl-project.yaml (off by default = verbatim). + + With --subject-area, exports just that area's slice for merging back into the mart. An erwin + GUID preserved on import is re-emitted, so erwin UPDATES the matching object rather than + creating a duplicate (objects authored in Modelith have no GUID and come in as new). """ from mdl_emit_erd import emit_erwin repo = _load(model_dir) naming = repo.model.config.export.erwin.naming - _emit_text(emit_erwin(repo.model, naming=naming), out, "erwin.xml") + xml = emit_erwin(repo.model, naming=naming, subject_area=subject_area) + _emit_text(xml, out, "erwin.xml") @export_app.command("csv") diff --git a/packages/cli/tests/test_export_erwin_cli.py b/packages/cli/tests/test_export_erwin_cli.py index 8ac7bba..0c40aea 100644 --- a/packages/cli/tests/test_export_erwin_cli.py +++ b/packages/cli/tests/test_export_erwin_cli.py @@ -52,3 +52,15 @@ def test_export_erwin_honours_the_naming_config(tmp_path): # logical/display name got underscores -> spaces; physical stayed raw assert "counterparty id" in xml assert "COUNTERPARTY" in xml + + +def test_export_erwin_scoped_to_subject_area(tmp_path): + """`-s ` exports just that subject area's slice.""" + model = _import_model(tmp_path) + dst = tmp_path / "slice.erwin.xml" + r = runner.invoke(app, ["export", "erwin", "-m", str(model), "-s", "Trading", "-o", str(dst)]) + assert r.exit_code == 0, r.output + xml = dst.read_text() + assert 'Format="erwin"' in xml and "Counterparty" in xml diff --git a/packages/core/src/mdl_core/ir.py b/packages/core/src/mdl_core/ir.py index 7eb6894..d84d82f 100644 --- a/packages/core/src/mdl_core/ir.py +++ b/packages/core/src/mdl_core/ir.py @@ -216,6 +216,7 @@ class SubjectArea(_Base): name: str definition: str | None = None members: list[ULID] = Field(default_factory=list) + erwin_id: str | None = None # original erwin GUID (see the note on other objects) class ConceptualEntity(_OntologyMixin, _Base): @@ -234,6 +235,8 @@ class ConceptualEntity(_OntologyMixin, _Base): # Derived, not authored: logical entities that realise this concept. realised_by: list[ULID] = Field(default_factory=list) udp: Udp | None = None # user-defined properties (erwin UDPs) + # NOTE: the erwin GUID for an entity lives on the LogicalEntity, not here — the importer + # splits one erwin into a conceptual + logical pair, and export folds them back. class Term(_OntologyMixin, _Base): @@ -266,6 +269,10 @@ class Domain(_Base): allowed_values: list[str | int] | None = None # inline enumeration value_set: str | None = None # name-ref to a CodeSet for shared enumerations udp: Udp | None = None # user-defined properties (erwin UDPs) + # The original erwin GUID, preserved on import so a later `mdl export erwin` re-emits the + # SAME id and erwin matches (updates) the object instead of creating a duplicate. None for + # objects not sourced from erwin (export then mints a stable synthetic GUID). + erwin_id: str | None = None class CodeValue(_Base): @@ -301,6 +308,10 @@ class Attribute(_OntologyMixin, _Base): ontology: OntologyAlignment | None = None # legacy, migrated on load term_map: TermMap | None = None # R2RML predicate/datatype override (KG mapping) udp: Udp | None = None # user-defined properties (erwin UDPs) + # The original erwin GUID, preserved on import so a later `mdl export erwin` re-emits the + # SAME id and erwin matches (updates) the object instead of creating a duplicate. None for + # objects not sourced from erwin (export then mints a stable synthetic GUID). + erwin_id: str | None = None class LogicalEntity(_Base): @@ -320,6 +331,10 @@ class LogicalEntity(_Base): unmanaged: bool | None = None term_map: TermMap | None = None # R2RML subject/class override (KG mapping) udp: Udp | None = None # user-defined properties (erwin UDPs) + # The original erwin GUID, preserved on import so a later `mdl export erwin` re-emits the + # SAME id and erwin matches (updates) the object instead of creating a duplicate. None for + # objects not sourced from erwin (export then mints a stable synthetic GUID). + erwin_id: str | None = None class RelationshipEnd(_Base): @@ -349,6 +364,10 @@ class Relationship(_Base): inverse_verb_phrase: str | None = None enforce: RelationshipEnforce = Field(default_factory=RelationshipEnforce) udp: Udp | None = None # user-defined properties (erwin UDPs) + # The original erwin GUID, preserved on import so a later `mdl export erwin` re-emits the + # SAME id and erwin matches (updates) the object instead of creating a duplicate. None for + # objects not sourced from erwin (export then mints a stable synthetic GUID). + erwin_id: str | None = None model_config = ConfigDict(extra="forbid", populate_by_name=True) @@ -371,6 +390,10 @@ class KeyGroup(_Base): type: KeyGroupType = "pk" members: list[ULID] = Field(default_factory=list) # ordered attribute ULIDs udp: Udp | None = None # user-defined properties (erwin UDPs) + # The original erwin GUID, preserved on import so a later `mdl export erwin` re-emits the + # SAME id and erwin matches (updates) the object instead of creating a duplicate. None for + # objects not sourced from erwin (export then mints a stable synthetic GUID). + erwin_id: str | None = None class Category(_Base): @@ -394,6 +417,7 @@ class Category(_Base): exclusive: bool = True # disjoint subtypes (an instance is exactly one) materialization: CategoryMaterialization = "single_table" udp: Udp | None = None + erwin_id: str | None = None # original erwin GUID (see the note on other objects) class PhysicalColumn(_Base): diff --git a/packages/emit-erd/src/mdl_emit_erd/erwin_export.py b/packages/emit-erd/src/mdl_emit_erd/erwin_export.py index f2a7f80..28205cc 100644 --- a/packages/emit-erd/src/mdl_emit_erd/erwin_export.py +++ b/packages/emit-erd/src/mdl_emit_erd/erwin_export.py @@ -67,17 +67,25 @@ } -def _guid(ulid: str) -> str: - """A stable erwin-shaped GUID {8-4-4-4-12} derived from a ULID, so output is reproducible. +def _guid(key: str) -> str: + """A stable erwin-shaped GUID {8-4-4-4-12} derived from a key, so output is reproducible. - The importer only uses GUIDs as intra-document join keys, so any stable unique mapping - works; we fold the 26-char ULID into 32 hex-ish chars deterministically.""" + Used for synthetic keys (the model root, key-group members) and as the fallback for an + object with no preserved erwin GUID — any stable unique mapping works since the importer + only uses GUIDs as intra-document join keys.""" import hashlib - h = hashlib.sha1(ulid.encode("utf-8")).hexdigest() # noqa: S324 - not security, just a stable id + h = hashlib.sha1(key.encode("utf-8")).hexdigest() # noqa: S324 - not security, just a stable id return f"{{{h[:8]}-{h[8:12]}-{h[12:16]}-{h[16:20]}-{h[20:32]}}}" +def _oid(obj) -> str: + """The erwin GUID for a model object: its PRESERVED original `erwin_id` when the object came + from an erwin import (so a re-import matches/updates it, no duplicate), else a stable + synthetic GUID from its ULID (a Modelith-native object erwin will treat as new).""" + return getattr(obj, "erwin_id", None) or _guid(obj.id) + + def _apply_naming(name: str, cfg: ErwinNaming, which: str) -> str: """Apply the team naming convention to a LOGICAL/display name. `which` is 'entity' or 'attribute'. A no-op unless the config opts in; physical names never pass through here.""" @@ -119,11 +127,34 @@ def _ref_array(parent: ET.Element, array_tag: str, item_tag: str, guids: list[st el.text = g -def emit_erwin(model: Model, *, naming: ErwinNaming | None = None) -> str: - """Serialize `model` to an erwin XML document string (XSD-valid; round-trips via the - erwin importer). `naming` applies a team convention to logical/display names; when None - (the default) names are emitted verbatim.""" +def _scope_entity_ids(model: Model, subject_area: str) -> set[str]: + """The logical-entity ids that belong to `subject_area` (its name or id). SubjectArea.members + are CONCEPTUAL ids, so map each to the logical entity that realises it.""" + le_by_ce = {le.realises: le.id for le in model.logical_entities.values() if le.realises} + ids: set[str] = set() + for sa in model.subject_areas.values(): + if sa.name == subject_area or sa.id == subject_area: + for m in sa.members: + if m in le_by_ce: + ids.add(le_by_ce[m]) + return ids + + +def emit_erwin( + model: Model, *, naming: ErwinNaming | None = None, subject_area: str | None = None +) -> str: + """Serialize `model` to an erwin XML document string (XSD-valid; round-trips via the erwin + importer). + + `naming` applies a team convention to logical/display names (None = verbatim). + + `subject_area` (a name or id) scopes the export to ONE subject area: only that area's member + entities are emitted in full. A relationship from an in-scope entity to an entity OUTSIDE the + area is KEPT, and the outside entity is emitted as a minimal STUB (its GUID + name + business + key only) so the FK resolves — in erwin that neighbour is auto-included in the area's view. + None = the whole model (unchanged).""" cfg = naming or ErwinNaming() + scope = _scope_entity_ids(model, subject_area) if subject_area else None root = ET.Element("erwin") root.set("xmlns", _NS) @@ -137,63 +168,107 @@ def emit_erwin(model: Model, *, naming: ErwinNaming | None = None) -> str: _sub(mdl, "ModelEnvProps") _sub(mdl, "ModelProps") - # --- domains (attributes reference them by GUID) --------------------------------- - domain_guid_by_name: dict[str, str] = {} - if model.domains: - grp = _sub(mdl, "Domain_Groups") - for dom in model.domains.values(): - g = _guid(dom.id) - domain_guid_by_name[dom.name] = g - el = _sub(grp, "Domain") - el.set("id", g) - p = _sub(el, "DomainProps") - _prop(p, "Name", dom.name) - _prop(p, "Logical_Data_Type", _BASE_TO_ERWIN.get(dom.base_type, "VARCHAR")) - _prop(p, "Definition", dom.definition) - - # --- entities (fold conceptual + logical back into one ) ----------------- - # precompute, per logical entity: its conceptual, its pk member attribute-ids, and a - # GUID for each of its attributes (relationships reference attributes by GUID). + # conceptual lookup, PK membership, and GUID maps (GUIDs prefer the preserved erwin_id). ce_by_le: dict[str, ConceptualEntity] = {} for ce in model.conceptual_entities.values(): for le in model.logical_entities.values(): if le.realises == ce.id: ce_by_le[le.id] = ce - pk_members_by_le: dict[str, set[str]] = {} for kg in model.key_groups.values(): if kg.type == "pk": pk_members_by_le.setdefault(kg.entity, set()).update(kg.members) - - entity_guid: dict[str, str] = {le.id: _guid(le.id) for le in model.logical_entities.values()} + entity_guid: dict[str, str] = {le.id: _oid(le) for le in model.logical_entities.values()} attr_guid: dict[str, str] = {} for le in model.logical_entities.values(): for a in le.attributes: - attr_guid[a.id] = _guid(a.id) + attr_guid[a.id] = _oid(a) - if model.logical_entities: - egrp = _sub(mdl, "Entity_Groups") + # Which relationships are in scope, and which OUT-of-scope entities they drag in as stubs. + in_scope_rels = [] + stub_ids: set[str] = set() + for r in model.relationships.values(): + c, pnt = r.from_.entity, r.to.entity + if c not in entity_guid or pnt not in entity_guid: + continue + if scope is None or c in scope or pnt in scope: + in_scope_rels.append(r) + if scope is not None: + for end in (c, pnt): + if end not in scope: + stub_ids.add(end) + # subtype (Category) relationships also pull neighbours into scope + in_scope_cats = [] + for cat in model.categories.values(): + if cat.supertype not in entity_guid: + continue + subs = [s for s in cat.subtypes if s in entity_guid] + if scope is None or cat.supertype in scope or any(s in scope for s in subs): + in_scope_cats.append(cat) + if scope is not None: + for e in (cat.supertype, *subs): + if e not in scope: + stub_ids.add(e) + + def _emit(le): + return scope is None or le.id in scope + + # --- domains (only those referenced by an EXPORTED attribute) -------------------- + exported_domains: set[str] = set() + if scope is None: + exported_domains = {d.name for d in model.domains.values()} + else: for le in model.logical_entities.values(): + if _emit(le): + for a in le.attributes: + if a.domain: + exported_domains.add(a.domain) + domain_guid_by_name: dict[str, str] = {} + doms = [d for d in model.domains.values() if d.name in exported_domains] + if doms: + grp = _sub(mdl, "Domain_Groups") + for dom in doms: + g = _oid(dom) + domain_guid_by_name[dom.name] = g + el = _sub(grp, "Domain") + el.set("id", g) + p = _sub(el, "DomainProps") + _prop(p, "Name", dom.name) + _prop(p, "Logical_Data_Type", _BASE_TO_ERWIN.get(dom.base_type, "VARCHAR")) + _prop(p, "Definition", dom.definition) + + # --- entities (full for in-scope; stubs for cross-boundary neighbours) ----------- + full_les = [le for le in model.logical_entities.values() if _emit(le)] + stub_les = [le for le in model.logical_entities.values() + if le.id in stub_ids and not _emit(le)] + if full_les or stub_les: + egrp = _sub(mdl, "Entity_Groups") + for le in full_les: _emit_entity(egrp, le, ce_by_le.get(le.id), model, cfg, entity_guid, attr_guid, pk_members_by_le, domain_guid_by_name) + for le in stub_les: + _emit_entity_stub(egrp, le, ce_by_le.get(le.id), cfg, entity_guid, attr_guid, + pk_members_by_le) # --- relationships (FK + subtype-from-Category) ---------------------------------- rels = _sub(mdl, "Relationship_Groups") - _emit_fk_relationships(rels, model, entity_guid, attr_guid) - _emit_subtype_relationships(rels, model, entity_guid, attr_guid) + for r in in_scope_rels: + _emit_fk_relationship(rels, r, entity_guid, attr_guid) + for cat in in_scope_cats: + _emit_subtype_relationship(rels, cat, entity_guid, attr_guid) if not list(rels): mdl.remove(rels) - # --- subject areas ---------------------------------------------------------------- - if model.subject_areas: + # --- subject areas (the scoped one, or all) -------------------------------------- + le_by_ce: dict[str, str] = {le.realises: le.id for le in model.logical_entities.values() + if le.realises} + sas = [sa for sa in model.subject_areas.values() + if subject_area is None or sa.name == subject_area or sa.id == subject_area] + if sas: sgrp = _sub(mdl, "Subject_Area_Groups") - # members are conceptual-entity ids; map each to its entity GUID (importer resolves - # a subject-area member ref against the entity's conceptual id). - le_by_ce: dict[str, str] = {le.realises: le.id for le in model.logical_entities.values() - if le.realises} - for sa in model.subject_areas.values(): + for sa in sas: el = _sub(sgrp, "Subject_Area") - el.set("id", _guid(sa.id)) + el.set("id", _oid(sa)) p = _sub(el, "Subject_AreaProps") _prop(p, "Name", sa.name) _prop(p, "Definition", sa.definition) @@ -228,7 +303,7 @@ def _emit_entity(egrp, le: LogicalEntity, ce, model, cfg, entity_guid, attr_guid kgrp = _sub(el, "Key_Group_Groups") for kg in kgs: kel = _sub(kgrp, "Key_Group") - kel.set("id", _guid(kg.id)) + kel.set("id", _oid(kg)) kp = _sub(kel, "Key_GroupProps") _prop(kp, "Name", kg.name) _prop(kp, "Key_Group_Type", _KG_TYPE.get(kg.type, "Alternate_Key")) @@ -263,43 +338,56 @@ def _emit_attribute(agrp, a: Attribute, cfg, attr_guid, is_pk: bool, domain_guid _prop(p, "Null_Option_Type", "NOT_NULL" if not a.nullable else "NULL") -def _emit_fk_relationships(rels, model, entity_guid, attr_guid) -> None: - for r in model.relationships.values(): - child = r.from_.entity # from = child/many side - parent = r.to.entity # to = parent/one side - if child not in entity_guid or parent not in entity_guid: +def _emit_entity_stub(egrp, le: LogicalEntity, ce, cfg, entity_guid, attr_guid, + pk_members_by_le) -> None: + """A cross-boundary neighbour: emit just enough for the FK to resolve — the entity's GUID + (its original erwin_id, so erwin matches the real mart entity), name, and its business-key + attribute(s) only (NOT the full attribute list). No key groups, no non-key columns.""" + el = _sub(egrp, "Entity") + el.set("id", entity_guid[le.id]) + p = _sub(el, "EntityProps") + display = ce.name if ce else le.name + _prop(p, "Name", _apply_naming(display, cfg, "entity")) + _prop(p, "Physical_Name", le.physical_name or le.name) + pk_ids = pk_members_by_le.get(le.id, set()) + bk = [a for a in le.attributes if a.role == "business_key" or a.id in pk_ids] + if bk: + agrp = _sub(el, "Attribute_Groups") + for a in bk: + _emit_attribute(agrp, a, cfg, attr_guid, True, {}) + + +def _emit_fk_relationship(rels, r, entity_guid, attr_guid) -> None: + child = r.from_.entity # from = child/many side + parent = r.to.entity # to = parent/one side + el = _sub(rels, "Relationship") + el.set("id", _oid(r)) + p = _sub(el, "RelationshipProps") + _prop(p, "Name", r.name) + _prop(p, "Definition", r.definition) + _prop(p, "Type", "Identifying" if r.identifying else "Non_Identifying") + _prop(p, "Cardinality", _CARD.get(r.cardinality, "Zero_One_or_More")) + _prop(p, "Parent_To_Child_Verb_Phrase", r.verb_phrase) + _prop(p, "Child_To_Parent_Verb_Phrase", r.inverse_verb_phrase) + _ref(p, "Parent_Entity_Ref", entity_guid[parent]) + _ref(p, "Child_Entity_Ref", entity_guid[child]) + + +def _emit_subtype_relationship(rels, cat, entity_guid, attr_guid) -> None: + """Re-emit a Category as subtype relationships (supertype -> each subtype), carrying the + discriminator, so the importer rebuilds the Category.""" + for sub_id in cat.subtypes: + if sub_id not in entity_guid: continue el = _sub(rels, "Relationship") - el.set("id", _guid(r.id)) + el.set("id", _guid(f"{cat.id}:{sub_id}")) p = _sub(el, "RelationshipProps") - _prop(p, "Name", r.name) - _prop(p, "Definition", r.definition) - _prop(p, "Type", "Identifying" if r.identifying else "Non_Identifying") - _prop(p, "Cardinality", _CARD.get(r.cardinality, "Zero_One_or_More")) - _prop(p, "Parent_To_Child_Verb_Phrase", r.verb_phrase) - _prop(p, "Child_To_Parent_Verb_Phrase", r.inverse_verb_phrase) - _ref(p, "Parent_Entity_Ref", entity_guid[parent]) - _ref(p, "Child_Entity_Ref", entity_guid[child]) - - -def _emit_subtype_relationships(rels, model, entity_guid, attr_guid) -> None: - """Re-emit each Category as subtype relationships (supertype -> each subtype), carrying the - discriminator, so the importer rebuilds the Category.""" - for cat in model.categories.values(): - if cat.supertype not in entity_guid: - continue - for sub_id in cat.subtypes: - if sub_id not in entity_guid: - continue - el = _sub(rels, "Relationship") - el.set("id", _guid(f"{cat.id}:{sub_id}")) - p = _sub(el, "RelationshipProps") - _prop(p, "Name", f"{cat.name}_{sub_id[-6:]}") - _prop(p, "Type", "Subtype_Complete" if cat.complete else "Subtype_Incomplete") - _ref(p, "Parent_Entity_Ref", entity_guid[cat.supertype]) # supertype = parent - _ref(p, "Child_Entity_Ref", entity_guid[sub_id]) # subtype = child - if cat.discriminator and cat.discriminator in attr_guid: - _ref(p, "Subtype_Discriminator_Ref", attr_guid[cat.discriminator]) + _prop(p, "Name", f"{cat.name}_{sub_id[-6:]}") + _prop(p, "Type", "Subtype_Complete" if cat.complete else "Subtype_Incomplete") + _ref(p, "Parent_Entity_Ref", entity_guid[cat.supertype]) # supertype = parent + _ref(p, "Child_Entity_Ref", entity_guid[sub_id]) # subtype = child + if cat.discriminator and cat.discriminator in attr_guid: + _ref(p, "Subtype_Discriminator_Ref", attr_guid[cat.discriminator]) def _pretty(root: ET.Element) -> str: diff --git a/packages/emit-erd/tests/test_erwin_export.py b/packages/emit-erd/tests/test_erwin_export.py index 21e01ef..931ab74 100644 --- a/packages/emit-erd/tests/test_erwin_export.py +++ b/packages/emit-erd/tests/test_erwin_export.py @@ -169,3 +169,136 @@ def test_naming_is_reproducible_and_valid_xml(): """Deterministic GUIDs -> identical output for the same model (golden-friendly).""" m = _source_model() assert emit_erwin(m) == emit_erwin(m) + + +# --- GUID preservation (Part C: re-import updates, not duplicates) ---------------- + + +def test_import_preserves_the_erwin_guid(): + """Every imported object carries its original erwin GUID in `erwin_id`, so a later export + can re-emit it and erwin matches the object instead of duplicating it.""" + m = _source_model() + assert all(le.erwin_id for le in m.logical_entities.values()) + assert all(a.erwin_id for le in m.logical_entities.values() for a in le.attributes) + assert all(kg.erwin_id for kg in m.key_groups.values()) + assert all(r.erwin_id for r in m.relationships.values()) + assert all(sa.erwin_id for sa in m.subject_areas.values()) + assert all(d.erwin_id for d in m.domains.values()) + + +def test_export_re_emits_the_original_guid(): + """The preserved GUID appears verbatim in the export (stable identity across the round-trip).""" + m = _source_model() + xml = emit_erwin(m) + le = next(iter(m.logical_entities.values())) + sa = next(iter(m.subject_areas.values())) + assert f'id="{le.erwin_id}"' in xml + assert f'id="{sa.erwin_id}"' in xml + + +# --- scoped export (Parts A + B) -------------------------------------------------- + + +def _two_sa_model(): + """A mart with two subject areas and a CROSS-SA FK: Events.ae (SA1) -> Reference.dim (SA2). + All objects carry erwin_id so we can assert GUID preservation in the slice too.""" + from mdl_core.ids import new_ulid + from mdl_core.ir import ( + Attribute, + ConceptualEntity, + KeyGroup, + LogicalEntity, + Model, + ProjectConfig, + Relationship, + RelationshipEnd, + SubjectArea, + ) + + m = Model(ProjectConfig(name="mart")) + # SA2: shared dimension, NOT in SA1 + dim_ce = ConceptualEntity(id=new_ulid(), name="DimSubject") + m.add(dim_ce) + dim_bk = Attribute(id=new_ulid(), name="usubjid", role="business_key", nullable=False, + domain="string", erwin_id="{DIM-A}") + dim = LogicalEntity(id=new_ulid(), name="dim_subject", physical_name="DIM_SUBJECT", + realises=dim_ce.id, attributes=[dim_bk], erwin_id="{DIM}") + m.add(dim) + m.add(KeyGroup(id=new_ulid(), entity=dim.id, name="pk_dim", type="pk", + members=[dim_bk.id], erwin_id="{DIM-KG}")) + m.add(SubjectArea(id=new_ulid(), name="Reference", members=[dim_ce.id], erwin_id="{SA2}")) + # SA1: ae with a cross-SA FK to dim_subject + ae_ce = ConceptualEntity(id=new_ulid(), name="Ae") + m.add(ae_ce) + ae_bk = Attribute(id=new_ulid(), name="aeseq", role="business_key", nullable=False, + domain="integer", erwin_id="{AE-A1}") + ae_fk = Attribute(id=new_ulid(), name="usubjid", role="attribute", nullable=False, + domain="string", erwin_id="{AE-A2}") + ae = LogicalEntity(id=new_ulid(), name="ae", physical_name="AE", realises=ae_ce.id, + attributes=[ae_bk, ae_fk], erwin_id="{AE}") + m.add(ae) + m.add(KeyGroup(id=new_ulid(), entity=ae.id, name="pk_ae", type="pk", + members=[ae_bk.id], erwin_id="{AE-KG}")) + m.add(SubjectArea(id=new_ulid(), name="Events", members=[ae_ce.id], erwin_id="{SA1}")) + m.add(Relationship( + id=new_ulid(), name="ae_for_subject", + **{"from": RelationshipEnd(entity=ae.id, attributes=[ae_fk.id])}, + to=RelationshipEnd(entity=dim.id, attributes=[dim_bk.id]), + cardinality="many_to_one", verb_phrase="describes", erwin_id="{REL}")) + return m + + +def _entity_attr_counts(xml): + import re + + out = {} + for mm in re.finditer(r".*?", xml, re.S): + block = mm.group(0) + name = re.search(r"([^<]+)", block).group(1) + out[name] = block.count(" Erwi ), definition=obj.props.get("Definition"), udp=_resolve_udp(obj, udp_name_by_def_id), + erwin_id=obj.id, ) ) @@ -421,6 +422,7 @@ def import_erwin(source: str | Path, *, project_name: str | None = None) -> Erwi role="business_key" if is_key else "attribute", nullable=_nullable(a.props.get("Null_Option_Type"), is_key), udp=_resolve_udp(a, udp_name_by_def_id), + erwin_id=a.id, ) attrs.append(attr) attr_le_by_erwin_id[a.id] = attr.id @@ -441,6 +443,7 @@ def import_erwin(source: str | Path, *, project_name: str | None = None) -> Erwi definition=obj.props.get("Definition"), attributes=attrs, udp=_resolve_udp(obj, udp_name_by_def_id), + erwin_id=obj.id, # the erwin GUID; export folds conceptual+logical back to it ) model.add(le) le_by_erwin_id[obj.id] = le @@ -464,6 +467,7 @@ def import_erwin(source: str | Path, *, project_name: str | None = None) -> Erwi type=_key_group_ir_type(kg), members=members, udp=_resolve_udp(kg, udp_name_by_def_id), + erwin_id=kg.id, ) ) @@ -563,6 +567,7 @@ def _build_relationships( verb_phrase=obj.props.get("Parent_To_Child_Verb_Phrase"), inverse_verb_phrase=obj.props.get("Child_To_Parent_Verb_Phrase"), udp=_resolve_udp(obj, udp_name_by_def_id), + erwin_id=obj.id, ) ) @@ -625,6 +630,7 @@ def _build_subject_areas(ordered, model, ce_id_by_entity, warnings) -> None: name=sname, definition=obj.props.get("Definition"), members=members, + erwin_id=obj.id, ) ) diff --git a/packages/server/src/mdl_server/app.py b/packages/server/src/mdl_server/app.py index 6add840..baf6e9f 100644 --- a/packages/server/src/mdl_server/app.py +++ b/packages/server/src/mdl_server/app.py @@ -210,9 +210,10 @@ def delete_layout(view: str | None = None) -> JSONResponse: # the client previews and proposes (it does not write the model itself). @app.get("/api/export/{fmt}") - def export_model(fmt: str, dialect: str = "postgres"): + def export_model(fmt: str, dialect: str = "postgres", subject_area: str = None): """Export the model to an interchange format. Returns text/plain (or CSV) so - the browser can offer it as a download.""" + the browser can offer it as a download. `subject_area` (erwin only) scopes the export + to one subject area.""" from fastapi.responses import PlainTextResponse from mdl_emit_erd import emit_csv, emit_dbml, emit_erwin, emit_mermaid, emit_sql_ddl @@ -226,7 +227,11 @@ def export_model(fmt: str, dialect: str = "postgres"): text, media, ext = emit_dbml(model), "text/plain", "dbml" elif fmt == "erwin": # the team naming convention (if any) travels in the project config, same as the CLI - text = emit_erwin(model, naming=model.config.export.erwin.naming) + text = emit_erwin( + model, + naming=model.config.export.erwin.naming, + subject_area=subject_area or None, + ) media, ext = "application/xml", "xml" elif fmt == "csv": text, media, ext = emit_csv(model), "text/csv", "csv"