Skip to content

Commit 89b843b

Browse files
authored
Display diff when check fails, Sort tables in Mermaid output (#54)
* Add debug logging with unified diff output when changes are detected * Ensure mermaid tables are always sorted for consistency * Chores * Fix tests for new mermaid table sorting order
1 parent 759c9f9 commit 89b843b

File tree

3 files changed

+39
-25
lines changed

3 files changed

+39
-25
lines changed

paracelsus/cli.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import re
22
import sys
33
from dataclasses import asdict
4+
from difflib import unified_diff
45
from pathlib import Path
56
from textwrap import dedent
67
from typing import List, Optional
@@ -307,7 +308,20 @@ def inject(
307308
sys.exit(0)
308309
else:
309310
# If content is different then we failed the test.
310-
typer.echo("Changes detected.")
311+
# Generate unified diff
312+
diff = unified_diff(
313+
old_content.splitlines(keepends=True),
314+
new_content.splitlines(keepends=True),
315+
fromfile="old",
316+
tofile="new",
317+
lineterm="",
318+
)
319+
320+
typer.echo("Changes detected. Diff:")
321+
typer.echo("")
322+
for line in diff:
323+
typer.echo(line.rstrip())
324+
311325
sys.exit(1)
312326
else:
313327
# Dump newly generated contents back to file.

paracelsus/transformers/mermaid.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,10 +173,10 @@ def __str__(self) -> str:
173173
""")
174174
output = yaml_front_matter + output
175175
output += "erDiagram\n"
176-
for table in self.metadata.tables.values():
176+
for table in sorted(self.metadata.tables.values(), key=lambda t: t.name):
177177
output += self._table(table)
178178

179-
for table in self.metadata.tables.values():
179+
for table in sorted(self.metadata.tables.values(), key=lambda t: t.name):
180180
for column in table.columns.values():
181181
if len(column.foreign_keys) > 0:
182182
output += self._relationships(column)

tests/conftest.py

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,13 @@ def package_path() -> Generator[Path, None, None]:
6868
@pytest.fixture()
6969
def mermaid_full_string_preseve_column_sort() -> str:
7070
return """erDiagram
71-
users {
71+
comments {
7272
CHAR(32) id PK
73-
VARCHAR(100) display_name "nullable"
7473
DATETIME created
74+
CHAR(32) post FK "nullable"
75+
CHAR(32) author FK
76+
BOOLEAN live "nullable"
77+
TEXT content "nullable"
7578
}
7679
7780
posts {
@@ -82,18 +85,15 @@ def mermaid_full_string_preseve_column_sort() -> str:
8285
TEXT content "nullable"
8386
}
8487
85-
comments {
88+
users {
8689
CHAR(32) id PK
90+
VARCHAR(100) display_name "nullable"
8791
DATETIME created
88-
CHAR(32) post FK "nullable"
89-
CHAR(32) author FK
90-
BOOLEAN live "nullable"
91-
TEXT content "nullable"
9292
}
9393
94-
users ||--o{ posts : author
9594
posts ||--o{ comments : post
9695
users ||--o{ comments : author
96+
users ||--o{ posts : author
9797
"""
9898

9999

@@ -153,12 +153,6 @@ def fixture_expected_mermaid_smaller_graph() -> str:
153153
layout: dagre
154154
---
155155
erDiagram
156-
users {
157-
CHAR(32) id PK
158-
DATETIME created
159-
VARCHAR(100) display_name "nullable"
160-
}
161-
162156
posts {
163157
CHAR(32) id PK
164158
CHAR(32) author FK
@@ -167,6 +161,12 @@ def fixture_expected_mermaid_smaller_graph() -> str:
167161
BOOLEAN live "True if post is published,nullable"
168162
}
169163
164+
users {
165+
CHAR(32) id PK
166+
DATETIME created
167+
VARCHAR(100) display_name "nullable"
168+
}
169+
170170
users ||--o{ posts : author
171171
172172
```
@@ -191,10 +191,13 @@ def fixture_expected_mermaid_complete_graph() -> str:
191191
layout: dagre
192192
---
193193
erDiagram
194-
users {
194+
comments {
195195
CHAR(32) id PK
196+
CHAR(32) author FK
197+
CHAR(32) post FK "nullable"
198+
TEXT content "nullable"
196199
DATETIME created
197-
VARCHAR(100) display_name "nullable"
200+
BOOLEAN live "nullable"
198201
}
199202
200203
posts {
@@ -205,18 +208,15 @@ def fixture_expected_mermaid_complete_graph() -> str:
205208
BOOLEAN live "True if post is published,nullable"
206209
}
207210
208-
comments {
211+
users {
209212
CHAR(32) id PK
210-
CHAR(32) author FK
211-
CHAR(32) post FK "nullable"
212-
TEXT content "nullable"
213213
DATETIME created
214-
BOOLEAN live "nullable"
214+
VARCHAR(100) display_name "nullable"
215215
}
216216
217-
users ||--o{ posts : author
218217
posts ||--o{ comments : post
219218
users ||--o{ comments : author
219+
users ||--o{ posts : author
220220
221221
```
222222
<!-- END_SQLALCHEMY_DOCS -->

0 commit comments

Comments
 (0)