Improve layout of ERD

This commit is contained in:
Peter Stockings
2026-01-29 19:30:11 +11:00
parent d72bb1f30f
commit ec12072a33
2 changed files with 30 additions and 8 deletions

View File

@@ -70,18 +70,40 @@ class Schema:
def generate_mermaid_er(self, schema_info):
"""Generates Mermaid ER diagram code from schema info."""
mermaid_lines = ["erDiagram"]
for table, info in schema_info.items():
mermaid_lines = [
"%%{init: {'theme': 'default', 'themeCSS': '.er.entityBox { fill: none; } .er.attributeBoxEven { fill: none; } .er.attributeBoxOdd { fill: none; }'}}%%",
"erDiagram"
]
# Sort tables for stable output
sorted_tables = sorted(schema_info.keys())
for table in sorted_tables:
info = schema_info[table]
mermaid_lines.append(f" {table} {{")
pks = set(info.get('primary_keys', []))
fks = {fk[0] for fk in info.get('foreign_keys', [])}
for column_name, data_type in info['columns']:
mermaid_data_type = self._map_data_type(data_type)
pk_marker = " PK" if column_name in info.get('primary_keys', []) else ""
mermaid_lines.append(f" {mermaid_data_type} {column_name}{pk_marker}")
markers = []
if column_name in pks:
markers.append("PK")
if column_name in fks:
markers.append("FK")
marker_str = f" {','.join(markers)}" if markers else ""
mermaid_lines.append(f" {mermaid_data_type} {column_name}{marker_str}")
mermaid_lines.append(" }")
for table, info in schema_info.items():
for fk_column, referenced_table, referenced_column in info['foreign_keys']:
relation = f" {table} }}|--|| {referenced_table} : \"{fk_column} to {referenced_column}\""
for table in sorted_tables:
info = schema_info[table]
# Sort foreign keys for stable output
sorted_fks = sorted(info.get('foreign_keys', []), key=lambda x: x[0])
for fk_column, referenced_table, referenced_column in sorted_fks:
relation = f" {referenced_table} ||--o{{ {table} : \"{fk_column}\""
mermaid_lines.append(relation)
return "\n".join(mermaid_lines)

File diff suppressed because one or more lines are too long

Before

Width:  |  Height:  |  Size: 135 KiB

After

Width:  |  Height:  |  Size: 136 KiB