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
18 changes: 18 additions & 0 deletions packages/cli/src/mdl_cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3007,6 +3007,24 @@ def export_dbml_cmd(
_emit_text(emit_dbml(repo.model), out, "dbml")


@export_app.command("erwin")
def export_erwin_cmd(
model_dir: Path = typer.Option(Path("."), "--model-dir", "-m"),
out: Path = typer.Option(None, "--out", "-o"),
) -> 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).
"""
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")


@export_app.command("csv")
def export_csv_cmd(
model_dir: Path = typer.Option(Path("."), "--model-dir", "-m"),
Expand Down
54 changes: 54 additions & 0 deletions packages/cli/tests/test_export_erwin_cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
"""CLI: `mdl export erwin` — writes erwin XML, honours the export.erwin.naming config."""

from __future__ import annotations

from pathlib import Path

from typer.testing import CliRunner

from mdl_cli.main import app

runner = CliRunner()

_REPO = Path(__file__).resolve().parents[3]
_FIXTURE = _REPO / "demos" / "fixtures" / "sample.erwin.xml"


def _import_model(tmp_path: Path) -> Path:
out = tmp_path / "model"
r = runner.invoke(app, ["import", "erwin", str(_FIXTURE), "-o", str(out)])
assert r.exit_code == 0, r.output
return out


def test_export_erwin_writes_a_document(tmp_path):
model = _import_model(tmp_path)
dst = tmp_path / "out.erwin.xml"
r = runner.invoke(app, ["export", "erwin", "-m", str(model), "-o", str(dst)])
assert r.exit_code == 0, r.output
xml = dst.read_text()
assert 'FileVersion="9.8.0000"' in xml and 'Format="erwin"' in xml
assert "<Entity " in xml


def test_export_erwin_honours_the_naming_config(tmp_path):
model = _import_model(tmp_path)
# opt into the team convention via the project file
proj = model / "mdl-project.yaml"
text = proj.read_text()
import re

text = re.sub(
r"export:\n(?: .*\n?)*",
"export:\n erwin:\n naming:\n replace_underscore_with_space: true\n",
text,
)
proj.write_text(text)

dst = tmp_path / "named.erwin.xml"
r = runner.invoke(app, ["export", "erwin", "-m", str(model), "-o", str(dst)])
assert r.exit_code == 0, r.output
xml = dst.read_text()
# logical/display name got underscores -> spaces; physical stayed raw
assert "<Name>counterparty id</Name>" in xml
assert "<Physical_Name>COUNTERPARTY</Physical_Name>" in xml
35 changes: 35 additions & 0 deletions packages/core/src/mdl_core/ir.py
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,39 @@ class ScaffoldConfig(_Base):
dbt_project_dir: str | None = None


class ErwinNaming(_Base):
"""Team naming convention applied to LOGICAL/display names when exporting erwin XML
(`mdl export erwin`). erwin does not enforce naming at the schema level, so a convention
is the exporter's job. All default to a no-op, so export is verbatim unless a team opts in.

Only the entity/attribute DISPLAY names (erwin `Name`) are rewritten; `Physical_Name`
always stays the raw snake-case physical identifier for the database.
"""

model_config = ConfigDict(extra="allow")

entity_case: Literal["verbatim", "title", "upper", "lower"] = "verbatim"
attribute_case: Literal["verbatim", "title", "upper", "lower"] = "verbatim"
# replace '_' with ' ' in the display name (e.g. adverse_event -> "Adverse Event")
replace_underscore_with_space: bool = False


class ErwinExportConfig(_Base):
"""Settings for `mdl export erwin`."""

model_config = ConfigDict(extra="allow")

naming: ErwinNaming = Field(default_factory=ErwinNaming)


class ExportConfig(_Base):
"""Per-format export settings. Today only erwin has options; others may follow."""

model_config = ConfigDict(extra="allow")

erwin: ErwinExportConfig = Field(default_factory=ErwinExportConfig)


class ProjectConfig(_Base):
model_config = ConfigDict(extra="allow")

Expand All @@ -590,6 +623,8 @@ class ProjectConfig(_Base):
# How this project was bootstrapped (import/init layout). Default `model`; recorded so
# a re-run reproduces the layout. See ScaffoldConfig.
scaffold: ScaffoldConfig = Field(default_factory=ScaffoldConfig)
# Per-format export settings (e.g. the erwin naming convention). See ExportConfig.
export: ExportConfig = Field(default_factory=ExportConfig)


# --- The in-memory graph ---------------------------------------------------
Expand Down
3 changes: 2 additions & 1 deletion packages/emit-erd/src/mdl_emit_erd/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@

from mdl_emit_erd.csv_export import emit_csv
from mdl_emit_erd.dbml import emit_dbml
from mdl_emit_erd.erwin_export import emit_erwin
from mdl_emit_erd.mermaid import emit_mermaid
from mdl_emit_erd.sql_ddl import emit_sql_ddl

__all__ = ["emit_sql_ddl", "emit_mermaid", "emit_dbml", "emit_csv"]
__all__ = ["emit_sql_ddl", "emit_mermaid", "emit_dbml", "emit_csv", "emit_erwin"]
Loading
Loading