forked from fleetbase/fleetbase
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mermaid.py
43 lines (39 loc) · 1.47 KB
/
mermaid.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
from __future__ import print_function
import re
def cleanname(name):
return re.sub(r'[^\d\w_]', '', name)
def format_table_name(full_name):
parts = full_name.split('.')
if len(parts) == 2 and not parts[0].endswith('_sandbox'):
return cleanname(parts[0] + '_' + parts[1])
else:
return None
print('erDiagram')
print('')
for table in catalog.tables:
formatted_name = format_table_name(table.fullName)
if formatted_name:
print(' ' + formatted_name + ' {')
for column in table.columns:
print(' ' + cleanname(column.columnDataType.name) + ' ' + cleanname(column.name),
end='')
if column.isPartOfPrimaryKey():
print(' PK', end='')
elif column.isPartOfForeignKey():
print(' FK', end='')
elif column.isPartOfUniqueIndex():
print(' UK', end='')
if column.hasRemarks():
print(' "' + ' '.join(column.remarks.splitlines()) + '"',
end='')
print()
print(' }')
print('')
for table in catalog.tables:
formatted_name = format_table_name(table.fullName)
if formatted_name:
for childTable in table.referencingTables:
child_formatted_name = format_table_name(childTable.fullName)
if child_formatted_name:
print(' ' + formatted_name + ' ||--o{ ' +
child_formatted_name + ' : "foreign key"')