|
| 1 | +"""Build a mermaid graph representation using raw strings without additional dependencies""" |
| 2 | +from enum import Enum |
| 3 | + |
| 4 | +from dlt.common.schema.typing import ( |
| 5 | + TColumnSchema, |
| 6 | + TReferenceCardinality, |
| 7 | + TStoredSchema, |
| 8 | + TTableReferenceStandalone, |
| 9 | + TTableSchema, |
| 10 | +) |
| 11 | + |
| 12 | + |
| 13 | +INDENT = " " |
| 14 | + |
| 15 | + |
| 16 | +def schema_to_mermaid( |
| 17 | + schema: TStoredSchema, |
| 18 | + *, |
| 19 | + references: list[TTableReferenceStandalone], |
| 20 | + hide_columns: bool = False, |
| 21 | + hide_descriptions: bool = False, |
| 22 | + include_dlt_tables: bool = True, |
| 23 | +) -> str: |
| 24 | + mermaid_er_diagram = "erDiagram\n" |
| 25 | + |
| 26 | + for table_name, table_schema in schema["tables"].items(): |
| 27 | + if not include_dlt_tables and table_name.startswith("_dlt"): |
| 28 | + continue |
| 29 | + |
| 30 | + mermaid_er_diagram += INDENT + _to_mermaid_table( |
| 31 | + table_schema, |
| 32 | + hide_columns=hide_columns, |
| 33 | + hide_descriptions=hide_descriptions, |
| 34 | + ) |
| 35 | + |
| 36 | + for ref in references: |
| 37 | + if not include_dlt_tables: |
| 38 | + if ref["table"].startswith("_dlt") or ref["referenced_table"].startswith("_dlt"): |
| 39 | + continue |
| 40 | + |
| 41 | + mermaid_er_diagram += INDENT + _to_mermaid_reference(ref) |
| 42 | + |
| 43 | + return mermaid_er_diagram |
| 44 | + |
| 45 | + |
| 46 | +def _to_mermaid_table( |
| 47 | + table: TTableSchema, hide_columns: bool = False, hide_descriptions: bool = False |
| 48 | +) -> str: |
| 49 | + mermaid_table: str = table["name"] |
| 50 | + mermaid_table += "{\n" |
| 51 | + |
| 52 | + if hide_columns is False: |
| 53 | + for column in table["columns"].values(): |
| 54 | + mermaid_table += INDENT + _to_mermaid_column( |
| 55 | + column, |
| 56 | + hide_descriptions=hide_descriptions, |
| 57 | + ) |
| 58 | + |
| 59 | + mermaid_table += "}\n" |
| 60 | + return mermaid_table |
| 61 | + |
| 62 | + |
| 63 | +# TODO add scale & precision to `data_type` |
| 64 | +def _to_mermaid_column(column: TColumnSchema, hide_descriptions: bool = False) -> str: |
| 65 | + mermaid_col = column["data_type"] + " " + column["name"] |
| 66 | + keys = [] |
| 67 | + if column.get("primary_key"): |
| 68 | + keys.append("PK") |
| 69 | + |
| 70 | + if column.get("unique"): |
| 71 | + keys.append("UK") |
| 72 | + |
| 73 | + if keys: |
| 74 | + mermaid_col += " " + ",".join(keys) |
| 75 | + |
| 76 | + if hide_descriptions is False: |
| 77 | + if description := column.get("description"): |
| 78 | + mermaid_col += f' "{description}"' |
| 79 | + |
| 80 | + mermaid_col += "\n" |
| 81 | + return mermaid_col |
| 82 | + |
| 83 | + |
| 84 | +class TMermaidArrows(str, Enum): |
| 85 | + ONE_TO_MANY = "||--|{" |
| 86 | + MANY_TO_ONE = "}|--||" |
| 87 | + ZERO_TO_MANY = "|o--|{" |
| 88 | + MANY_TO_ZERO = "}|--o|" |
| 89 | + ONE_TO_MORE = "||--o{" |
| 90 | + MORE_TO_ONE = "}o--||" |
| 91 | + ONE_TO_ONE = "||--||" |
| 92 | + MANY_TO_MANY = "}|--|{" |
| 93 | + ZERO_TO_ONE = "|o--o|" |
| 94 | + |
| 95 | + |
| 96 | +_CARDINALITY_ARROW: dict[TReferenceCardinality, TMermaidArrows] = { |
| 97 | + "one_to_many": TMermaidArrows.ONE_TO_MANY, |
| 98 | + "many_to_one": TMermaidArrows.MANY_TO_ONE, |
| 99 | + "zero_to_many": TMermaidArrows.ZERO_TO_MANY, |
| 100 | + "many_to_zero": TMermaidArrows.MANY_TO_ZERO, |
| 101 | + "one_to_one": TMermaidArrows.ONE_TO_ONE, |
| 102 | + "many_to_many": TMermaidArrows.MANY_TO_MANY, |
| 103 | + "zero_to_one": TMermaidArrows.ZERO_TO_ONE, |
| 104 | + "one_to_zero": TMermaidArrows.ZERO_TO_ONE, |
| 105 | +} |
| 106 | + |
| 107 | + |
| 108 | +def _to_mermaid_reference(ref: TTableReferenceStandalone) -> str: |
| 109 | + """Builds references in the following format using cardinality and label to describe |
| 110 | + the relationship |
| 111 | +
|
| 112 | + <left-entity> [<relationship> <right-entity> : <relationship-label>] |
| 113 | + """ |
| 114 | + left_table = ref.get("table") |
| 115 | + right_table = ref.get("referenced_table") |
| 116 | + cardinality = ref.get("cardinality", "one_to_many") |
| 117 | + label = ref.get("label", '""') |
| 118 | + arrow: str = _CARDINALITY_ARROW.get(cardinality).value |
| 119 | + |
| 120 | + mermaid_reference = f"{left_table} {arrow} {right_table}" |
| 121 | + if label: |
| 122 | + mermaid_reference += f" : {label}" |
| 123 | + |
| 124 | + mermaid_reference += "\n" |
| 125 | + return mermaid_reference |
0 commit comments