-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb_diagram.py
95 lines (77 loc) · 3.06 KB
/
db_diagram.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import pymysql
import os
from graphviz import Digraph
def generate_db_diagram(host, port, user, password, db_name, output_file="db_diagram"):
"""Generate a database diagram using Graphviz"""
try:
# Connect to database
conn = pymysql.connect(
host=host,
port=port,
user=user,
password=password,
database=db_name
)
cursor = conn.cursor()
# Create a new directed graph
dot = Digraph(comment='CBO Database Schema')
dot.attr('node', shape='record', style='filled', fillcolor='lightblue')
# Get table structures
cursor.execute("""
SELECT table_name
FROM information_schema.tables
WHERE table_schema = %s
""", (db_name,))
tables = cursor.fetchall()
# Process each table
for table in tables:
table_name = table[0]
# Get column information
cursor.execute("""
SELECT column_name, data_type, is_nullable, column_key
FROM information_schema.columns
WHERE table_schema = %s AND table_name = %s
ORDER BY ordinal_position
""", (db_name, table_name))
columns = cursor.fetchall()
# Format table node
table_label = f"{{{table_name}|"
for col in columns:
col_name, data_type, is_nullable, column_key = col
pk_marker = "PK" if column_key == "PRI" else ""
nullable = "NULL" if is_nullable == "YES" else "NOT NULL"
table_label += f"{pk_marker} {col_name} : {data_type} {nullable}\\l"
table_label += "}"
dot.node(table_name, label=table_label)
# Get foreign key relationships
cursor.execute("""
SELECT
table_name, column_name,
referenced_table_name, referenced_column_name
FROM information_schema.key_column_usage
WHERE table_schema = %s
AND referenced_table_name IS NOT NULL
""", (db_name,))
relations = cursor.fetchall()
# Add edges for foreign key relationships
for relation in relations:
table, column, ref_table, ref_column = relation
dot.edge(table, ref_table, label=f"{column} -> {ref_column}")
# Close connection
conn.close()
# Render the diagram
dot.render(output_file, format='pdf', cleanup=True)
print(f"Database diagram has been saved to {output_file}.pdf")
return True
except Exception as e:
print(f"Error generating database diagram: {str(e)}")
return False
if __name__ == "__main__":
# Database connection parameters
host = "t1brime-dev.ru"
port = 3306
user = "toonbrime"
password = "Bebra228"
db_name = "CBO"
# Generate diagram
generate_db_diagram(host, port, user, password, db_name)