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
7 changes: 4 additions & 3 deletions canvas/src/sme/ImportExportMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,10 @@ export function ImportExportMenu({
</label>
)}
{formats.map((f) => {
// erwin export honours a single active subject-area filter (exports just that
// slice); every other format always exports the whole model.
const scoped = f.id === "erwin" ? scopeSubjectArea : null;
// erwin and SQL DDL export honour a single active subject-area filter (export just
// that slice, like erwin's own subject-area export); every other format always
// exports the whole model.
const scoped = f.id === "erwin" || f.id === "sql" ? scopeSubjectArea : null;
return (
<a
key={f.id}
Expand Down
12 changes: 11 additions & 1 deletion packages/cli/src/mdl_cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2975,12 +2975,22 @@ def export_sql_cmd(
model_dir: Path = typer.Option(Path("."), "--model-dir", "-m"),
out: Path = typer.Option(None, "--out", "-o"),
dialect: str = typer.Option("postgres", "--dialect", help="postgres|snowflake|duckdb|..."),
subject_area: str = typer.Option(
None,
"--subject-area",
"-s",
help="Export only this subject area (name or id). Cross-area FKs are dropped (no stub "
"table for the out-of-area neighbour); re-establish them by hand in the target model. "
"Default: the whole model.",
),
) -> None:
"""Export CREATE TABLE DDL (PK/FK/UNIQUE/NOT NULL) for a chosen SQL dialect."""
from mdl_emit_erd import emit_sql_ddl

repo = _load(model_dir)
_emit_text(emit_sql_ddl(repo.model, dialect=dialect), out, "sql")
_emit_text(
emit_sql_ddl(repo.model, dialect=dialect, subject_area=subject_area or None), out, "sql"
)


@export_app.command("mermaid")
Expand Down
61 changes: 53 additions & 8 deletions packages/emit-erd/src/mdl_emit_erd/model_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,26 @@ class Table:
pk_columns: list[str] = field(default_factory=list) # ordered


def _dedupe(ids: list[str]) -> list[str]:
"""Order-preserving de-duplication. A key group whose members list the same attribute twice
(seen in real erwin-imported models) would otherwise emit `PRIMARY KEY (col, col)`, which
erwin's DDL importer rejects with 'Cannot add the same Attribute to Key'."""
seen: set[str] = set()
out: list[str] = []
for i in ids:
if i not in seen:
seen.add(i)
out.append(i)
return out


def _pk_attr_ids(model: Model, le: LogicalEntity) -> list[str]:
"""Ordered PK attribute ids: a `pk` KeyGroup wins (member order preserved), else
the `role: business_key` attributes. Mirrors the dbt emitter."""
"""Ordered, de-duplicated PK attribute ids: a `pk` KeyGroup wins (member order preserved),
else the `role: business_key` attributes. Mirrors the dbt emitter."""
for kg in model.key_groups.values():
if kg.entity == le.id and kg.type == "pk":
return list(kg.members)
return [a.id for a in le.attributes if a.role == "business_key"]
return _dedupe(list(kg.members))
return _dedupe([a.id for a in le.attributes if a.role == "business_key"])


def _single_unique_attr_ids(model: Model, le: LogicalEntity) -> set[str]:
Expand All @@ -65,12 +78,34 @@ def _single_unique_attr_ids(model: Model, le: LogicalEntity) -> set[str]:
return out


def build_tables(model: Model, adapter) -> list[Table]:
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 build_tables(model: Model, adapter, *, subject_area: str | None = None) -> list[Table]:
"""Project the model's logical entities into ER tables with columns, PKs, and FKs.

`adapter` is a dbt PlatformAdapter (from mdl_emit_dbt.platforms.get_adapter) used
only for domain -> SQL type mapping, so DDL types match what dbt would emit."""
ent_by_id = model.logical_entities
only for domain -> SQL type mapping, so DDL types match what dbt would emit.

`subject_area` (a name or id) scopes the projection to ONE subject area, like erwin's
export: only that area's entities become tables, and a relationship with an end OUTSIDE
the area is DROPPED (no stub table for the neighbour). The neighbours already exist in the
target model; cross-area FKs are re-established there by hand. None = the whole model."""
scope = _scope_entity_ids(model, subject_area) if subject_area else None
ent_by_id = {
eid: le for eid, le in model.logical_entities.items()
if scope is None or eid in scope
}
name_by_id = {le.id: le.name for le in ent_by_id.values()}
tables: dict[str, Table] = {}

Expand Down Expand Up @@ -112,6 +147,14 @@ def build_tables(model: Model, adapter) -> list[Table]:
parent_ent = ent_by_id.get(rel.to.entity)
c_names = _resolve_names(child_ent, rel.from_.attributes)
p_names = _resolve_names(parent_ent, rel.to.attributes) if parent_ent else []
# A FK must reference the PARENT's key, or erwin refuses to create it ("there is not a
# primary or unique key in table '<parent>' that contains column ..."). When the
# relationship doesn't name the parent columns, default to the parent's PK so the
# REFERENCES points at a real key.
if not p_names and parent_ent is not None:
pk_ids = _pk_attr_ids(model, parent_ent)
by_id = {a.id: a.name for a in parent_ent.attributes}
p_names = [by_id[i] for i in pk_ids if i in by_id]
if not c_names:
continue # nothing concrete to anchor a FK to
child_unique = rel.cardinality in ("one_to_one",) or _cols_unique(model, child_ent, c_names)
Expand All @@ -132,7 +175,9 @@ def _resolve_names(entity, attr_ids: list[str]) -> list[str]:
if entity is None:
return []
by_id = {a.id: a.name for a in entity.attributes}
return [by_id[i] for i in attr_ids if i in by_id]
# de-dupe: a FK column set with a repeated member would emit `FOREIGN KEY (col, col)`,
# which erwin rejects the same way a duplicated PK column does.
return _dedupe([by_id[i] for i in attr_ids if i in by_id])


def _cols_unique(model: Model, entity, col_names: list[str]) -> bool:
Expand Down
12 changes: 9 additions & 3 deletions packages/emit-erd/src/mdl_emit_erd/sql_ddl.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,26 @@ def _ident(name: str, dialect: str) -> str:
return f"{q}{name}{q}"


def emit_sql_ddl(model: Model, *, dialect: str = "postgres") -> str:
def emit_sql_ddl(
model: Model, *, dialect: str = "postgres", subject_area: str | None = None
) -> str:
"""CREATE TABLE statements with PK/UNIQUE/NOT NULL and FK REFERENCES.

`dialect` selects the SQL type mapping via the dbt platform adapters
(postgres|snowflake|redshift|duckdb|...); an unknown dialect falls back to duckdb's
generic types."""
generic types.

`subject_area` (a name or id) scopes the export to ONE subject area, like the erwin export:
only that area's tables are emitted and cross-area FKs are dropped (no stub table for the
out-of-area neighbour). None = the whole model."""
from mdl_emit_dbt.platforms import available_adapters, get_adapter

adapter_name = dialect if dialect in available_adapters() else "duckdb"
# postgres isn't a warehouse adapter; map it to duckdb's ANSI-ish types.
if dialect == "postgres":
adapter_name = "duckdb"
adapter = get_adapter(adapter_name)
tables = build_tables(model, adapter)
tables = build_tables(model, adapter, subject_area=subject_area)

out: list[str] = [f"-- SQL DDL ({dialect}) generated by Modelith", ""]
for t in tables:
Expand Down
147 changes: 147 additions & 0 deletions packages/emit-erd/tests/test_emit_erd.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,153 @@ def test_sql_ddl_has_tables_pk_and_fk(model):
assert "REFERENCES" in ddl


def test_ddl_dedupes_a_duplicated_pk_member():
"""A `pk` key group with the same attribute listed twice (seen in real erwin-imported
models) must NOT emit `PRIMARY KEY (col, col)` — erwin's DDL importer rejects that with
'Cannot add the same Attribute to Key'. The column appears exactly once."""
from mdl_core.ids import new_ulid
from mdl_core.ir import (
Attribute,
ConceptualEntity,
KeyGroup,
LogicalEntity,
Model,
ProjectConfig,
)

m = Model(ProjectConfig(name="dup"))
ce = ConceptualEntity(id=new_ulid(), name="Account")
m.add(ce)
acc_id = Attribute(id=new_ulid(), name="account_id", role="business_key", nullable=False,
domain="integer")
le = LogicalEntity(id=new_ulid(), name="account", realises=ce.id, attributes=[acc_id])
m.add(le)
# the bug condition: the PK key group lists account_id TWICE
m.add(KeyGroup(id=new_ulid(), entity=le.id, name="pk_account", type="pk",
members=[acc_id.id, acc_id.id]))

ddl = emit_sql_ddl(m, dialect="postgres")
pk_line = next(ln for ln in ddl.splitlines() if "PRIMARY KEY" in ln)
assert pk_line.count("account_id") == 1, pk_line


def test_ddl_fk_defaults_to_parent_pk_when_unspecified():
"""When a relationship doesn't name the parent columns, the FK REFERENCES the parent's PK
(not nothing), so erwin can actually create the key instead of dropping it with 'there is
not a primary or unique key in table ... that contains column'."""
from mdl_core.ids import new_ulid
from mdl_core.ir import (
Attribute,
ConceptualEntity,
KeyGroup,
LogicalEntity,
Model,
ProjectConfig,
Relationship,
RelationshipEnd,
)

m = Model(ProjectConfig(name="fk"))
pce = ConceptualEntity(id=new_ulid(), name="Account")
m.add(pce)
pk_attr = Attribute(id=new_ulid(), name="account_id", role="business_key", nullable=False,
domain="integer")
parent = LogicalEntity(id=new_ulid(), name="account", realises=pce.id, attributes=[pk_attr])
m.add(parent)
m.add(KeyGroup(id=new_ulid(), entity=parent.id, name="pk_account", type="pk",
members=[pk_attr.id]))
cce = ConceptualEntity(id=new_ulid(), name="Trade")
m.add(cce)
c_pk = Attribute(id=new_ulid(), name="trade_id", role="business_key", nullable=False,
domain="integer")
c_fk = Attribute(id=new_ulid(), name="account_id", role="attribute", nullable=False,
domain="integer")
child = LogicalEntity(id=new_ulid(), name="trade", realises=cce.id, attributes=[c_pk, c_fk])
m.add(child)
# relationship names the CHILD FK column but NOT the parent column
m.add(Relationship(
id=new_ulid(), name="trade_for_account",
**{"from": RelationshipEnd(entity=child.id, attributes=[c_fk.id])},
to=RelationshipEnd(entity=parent.id, attributes=[]),
cardinality="many_to_one"))

ddl = emit_sql_ddl(m, dialect="postgres")
fk_line = next(ln for ln in ddl.splitlines() if "FOREIGN KEY" in ln)
assert "REFERENCES account (account_id)" in fk_line, fk_line


def test_sql_ddl_scopes_to_a_subject_area_and_drops_cross_area_fk():
"""A scoped DDL export emits only the subject area's tables, keeps within-area FKs, and
DROPS cross-area FKs with NO stub table for the out-of-area neighbour (it already exists in
the target model; the FK is re-established there by hand)."""
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"))
# out-of-area neighbour
acc_ce = ConceptualEntity(id=new_ulid(), name="Account")
m.add(acc_ce)
acc_pk = Attribute(id=new_ulid(), name="account_id", role="business_key", nullable=False,
domain="integer")
acc = LogicalEntity(id=new_ulid(), name="account", realises=acc_ce.id, attributes=[acc_pk])
m.add(acc)
m.add(KeyGroup(id=new_ulid(), entity=acc.id, name="pk_acc", type="pk", members=[acc_pk.id]))
m.add(SubjectArea(id=new_ulid(), name="Core", members=[acc_ce.id]))
# in-area entities: trade (pk) and trade_leg (within-area FK to trade), plus a cross-area
# FK trade -> account
t_ce = ConceptualEntity(id=new_ulid(), name="Trade")
m.add(t_ce)
t_pk = Attribute(id=new_ulid(), name="trade_id", role="business_key", nullable=False,
domain="integer")
t_fk = Attribute(id=new_ulid(), name="account_id", role="attribute", nullable=False,
domain="integer")
trade = LogicalEntity(id=new_ulid(), name="trade", realises=t_ce.id, attributes=[t_pk, t_fk])
m.add(trade)
m.add(KeyGroup(id=new_ulid(), entity=trade.id, name="pk_trade", type="pk",
members=[t_pk.id]))
leg_ce = ConceptualEntity(id=new_ulid(), name="TradeLeg")
m.add(leg_ce)
leg_pk = Attribute(id=new_ulid(), name="leg_id", role="business_key", nullable=False,
domain="integer")
leg_fk = Attribute(id=new_ulid(), name="trade_id", role="attribute", nullable=False,
domain="integer")
leg = LogicalEntity(id=new_ulid(), name="trade_leg", realises=leg_ce.id,
attributes=[leg_pk, leg_fk])
m.add(leg)
m.add(KeyGroup(id=new_ulid(), entity=leg.id, name="pk_leg", type="pk", members=[leg_pk.id]))
m.add(SubjectArea(id=new_ulid(), name="Trading", members=[t_ce.id, leg_ce.id]))
# within-area FK: trade_leg -> trade
m.add(Relationship(
id=new_ulid(), name="leg_of_trade",
**{"from": RelationshipEnd(entity=leg.id, attributes=[leg_fk.id])},
to=RelationshipEnd(entity=trade.id, attributes=[t_pk.id]), cardinality="many_to_one"))
# cross-area FK: trade -> account (Core)
m.add(Relationship(
id=new_ulid(), name="trade_for_account",
**{"from": RelationshipEnd(entity=trade.id, attributes=[t_fk.id])},
to=RelationshipEnd(entity=acc.id, attributes=[acc_pk.id]), cardinality="many_to_one"))

ddl = emit_sql_ddl(m, dialect="postgres", subject_area="Trading")
# only the Trading tables are present; the Core neighbour is NOT emitted (no stub)
assert "CREATE TABLE trade (" in ddl
assert "CREATE TABLE trade_leg (" in ddl
assert "CREATE TABLE account" not in ddl
# the within-area FK survives; the cross-area FK CONSTRAINT to account is dropped (the
# account_id COLUMN on trade stays as an ordinary attribute).
assert "REFERENCES trade (" in ddl
assert "REFERENCES account" not in ddl


def test_mermaid_is_an_erdiagram_with_markers(model):
mer = emit_mermaid(model)
assert mer.startswith("erDiagram")
Expand Down
10 changes: 7 additions & 3 deletions packages/server/src/mdl_server/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,15 +212,19 @@ def delete_layout(view: str | None = None) -> JSONResponse:
@app.get("/api/export/{fmt}")
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. `subject_area` (erwin only) scopes the export
to one subject area."""
the browser can offer it as a download. `subject_area` (erwin and sql) 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

model = _load().model
try:
if fmt == "sql":
text, media, ext = emit_sql_ddl(model, dialect=dialect), "text/plain", "sql"
text, media, ext = (
emit_sql_ddl(model, dialect=dialect, subject_area=subject_area or None),
"text/plain",
"sql",
)
elif fmt == "mermaid":
text, media, ext = emit_mermaid(model), "text/plain", "mmd"
elif fmt == "dbml":
Expand Down
Loading
Loading