From ab2054424920c994f15e117beabc02075c1c245b Mon Sep 17 00:00:00 2001 From: Justin Ellison Date: Tue, 19 Mar 2024 22:37:45 -0500 Subject: [PATCH 1/4] Use 'UK' and column comments Mermaid supports a key type of 'UK' for unique key, better to use that instead of adding a comment. Also, if the SQLAlchemy column has a comment assigned, we should add that to the Mermaid comment. --- paracelsus/transformers/mermaid.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/paracelsus/transformers/mermaid.py b/paracelsus/transformers/mermaid.py index 14b07cd..bdebea8 100644 --- a/paracelsus/transformers/mermaid.py +++ b/paracelsus/transformers/mermaid.py @@ -29,15 +29,17 @@ def _column(self, column: Column) -> str: column_str += " PK" elif len(column.foreign_keys) > 0: column_str += " FK" + elif column.unique: + column_str += " UK" + + if column.comment: + column_str += f" \"{column.comment}\"" options = [] if column.nullable: options.append("nullable") - if column.unique: - options.append("unique") - if column.index: options.append("indexed") From a1340dc444f6fa30dad2a11bbdb294f6d12b252e Mon Sep 17 00:00:00 2001 From: Justin Ellison Date: Wed, 20 Mar 2024 08:46:38 -0500 Subject: [PATCH 2/4] Adding a comment to a FK column that directs the user to the referenced table --- paracelsus/transformers/mermaid.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/paracelsus/transformers/mermaid.py b/paracelsus/transformers/mermaid.py index bdebea8..f8e1519 100644 --- a/paracelsus/transformers/mermaid.py +++ b/paracelsus/transformers/mermaid.py @@ -20,6 +20,7 @@ def _table(self, table: Table) -> str: return output def _column(self, column: Column) -> str: + options = [] column_str = f"{column.type} {column.name}" if column.primary_key: @@ -29,13 +30,12 @@ def _column(self, column: Column) -> str: column_str += " PK" elif len(column.foreign_keys) > 0: column_str += " FK" + options.append(f"Foreign key references {column.table.name}") elif column.unique: column_str += " UK" if column.comment: - column_str += f" \"{column.comment}\"" - - options = [] + column_str += f' "{column.comment}"' if column.nullable: options.append("nullable") From b68ec3068eb3197990f259967cceef1dd1ff3c83 Mon Sep 17 00:00:00 2001 From: Justin Ellison Date: Wed, 20 Mar 2024 09:50:41 -0500 Subject: [PATCH 3/4] Adding comments to options instead of column_str, fixing tests --- paracelsus/transformers/mermaid.py | 2 +- tests/conftest.py | 2 +- tests/transformers/test_mermaid.py | 4 +++- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/paracelsus/transformers/mermaid.py b/paracelsus/transformers/mermaid.py index f8e1519..aba7bef 100644 --- a/paracelsus/transformers/mermaid.py +++ b/paracelsus/transformers/mermaid.py @@ -35,7 +35,7 @@ def _column(self, column: Column) -> str: column_str += " UK" if column.comment: - column_str += f' "{column.comment}"' + options.append(f"{column.comment}") if column.nullable: options.append("nullable") diff --git a/tests/conftest.py b/tests/conftest.py index ee125e6..5430302 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -23,7 +23,7 @@ class Post(Base): id = mapped_column(Uuid, primary_key=True, default=uuid4()) author = mapped_column(ForeignKey(User.id), nullable=False) created = mapped_column(DateTime, nullable=False, default=datetime.utcnow()) - live = mapped_column(Boolean, default=False) + live = mapped_column(Boolean, default=False, comment="True if post is published") content = mapped_column(Text, default="") class Comment(Base): diff --git a/tests/transformers/test_mermaid.py b/tests/transformers/test_mermaid.py index 0d4b8da..3b5f878 100644 --- a/tests/transformers/test_mermaid.py +++ b/tests/transformers/test_mermaid.py @@ -4,6 +4,7 @@ def test_mermaid(metaclass): mermaid = Mermaid(metaclass=metaclass) graph_string = str(mermaid) + print(graph_string) assert "users {" in graph_string assert "posts {" in graph_string @@ -14,5 +15,6 @@ def test_mermaid(metaclass): assert "users ||--o{ comments : author" in graph_string assert "CHAR(32) author FK" in graph_string - assert 'CHAR(32) post FK "nullable"' in graph_string + assert 'CHAR(32) post FK "Foreign key references comments,nullable"' in graph_string + assert 'BOOLEAN live "True if post is published,nullable"' in graph_string assert "DATETIME created" in graph_string From 41b3a19be7db808a23e14997df0097a9d3e82daf Mon Sep 17 00:00:00 2001 From: Justin Ellison Date: Wed, 20 Mar 2024 11:31:06 -0500 Subject: [PATCH 4/4] Implementing requested changes --- paracelsus/transformers/mermaid.py | 3 +-- tests/transformers/test_mermaid.py | 3 +-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/paracelsus/transformers/mermaid.py b/paracelsus/transformers/mermaid.py index aba7bef..6414150 100644 --- a/paracelsus/transformers/mermaid.py +++ b/paracelsus/transformers/mermaid.py @@ -30,12 +30,11 @@ def _column(self, column: Column) -> str: column_str += " PK" elif len(column.foreign_keys) > 0: column_str += " FK" - options.append(f"Foreign key references {column.table.name}") elif column.unique: column_str += " UK" if column.comment: - options.append(f"{column.comment}") + options.append(column.comment) if column.nullable: options.append("nullable") diff --git a/tests/transformers/test_mermaid.py b/tests/transformers/test_mermaid.py index 3b5f878..cd176e1 100644 --- a/tests/transformers/test_mermaid.py +++ b/tests/transformers/test_mermaid.py @@ -4,7 +4,6 @@ def test_mermaid(metaclass): mermaid = Mermaid(metaclass=metaclass) graph_string = str(mermaid) - print(graph_string) assert "users {" in graph_string assert "posts {" in graph_string @@ -15,6 +14,6 @@ def test_mermaid(metaclass): assert "users ||--o{ comments : author" in graph_string assert "CHAR(32) author FK" in graph_string - assert 'CHAR(32) post FK "Foreign key references comments,nullable"' in graph_string + assert 'CHAR(32) post FK "nullable"' in graph_string assert 'BOOLEAN live "True if post is published,nullable"' in graph_string assert "DATETIME created" in graph_string