Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion packages/cli/src/mdl_cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
12 changes: 12 additions & 0 deletions packages/cli/tests/test_export_erwin_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,15 @@ def test_export_erwin_honours_the_naming_config(tmp_path):
# logical/display name got underscores -> spaces; physical stayed raw
assert "<Name>counterparty id</Name>" in xml
assert "<Physical_Name>COUNTERPARTY</Physical_Name>" in xml


def test_export_erwin_scoped_to_subject_area(tmp_path):
"""`-s <SA>` 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 "<Subject_Area " in xml
# the Trading area's entities are present
assert "<Name>Counterparty</Name>" in xml
24 changes: 24 additions & 0 deletions packages/core/src/mdl_core/ir.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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 <Entity> into a conceptual + logical pair, and export folds them back.


class Term(_OntologyMixin, _Base):
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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):
Expand All @@ -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):
Expand Down Expand Up @@ -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)

Expand All @@ -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):
Expand All @@ -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):
Expand Down
Loading
Loading