-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexport_schema.py
More file actions
76 lines (63 loc) · 2.1 KB
/
export_schema.py
File metadata and controls
76 lines (63 loc) · 2.1 KB
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
#!/usr/bin/env python3
"""
Export full database schema with column names and types
"""
import mysql.connector
from mysql.connector import Error
import os
from dotenv import load_dotenv
import json
load_dotenv()
def get_table_schema(db_name, table_name):
"""Get detailed column schema for a table."""
try:
connection = mysql.connector.connect(
host=os.getenv("DB_HOST"),
user=os.getenv("DB_USER"),
password=os.getenv("DB_PASSWORD"),
port=int(os.getenv("DB_PORT", "3306")),
database=db_name
)
cursor = connection.cursor()
cursor.execute(f"DESCRIBE {table_name}")
columns = cursor.fetchall()
schema = []
for column in columns:
col_name, col_type, nullable, key, default, extra = column[:6]
schema.append({
"name": col_name,
"type": col_type,
"nullable": nullable == "YES",
"key": key,
"default": str(default) if default is not None else None,
"extra": extra
})
cursor.close()
connection.close()
return schema
except Error as e:
print(f"Error: {e}")
return None
if __name__ == "__main__":
# Use first database as reference
db = "Bishop_State_Community_College"
tables = ["cohort", "course", "financial_aid"]
result = {}
for table in tables:
print(f"\nExporting schema for {table}...")
schema = get_table_schema(db, table)
if schema:
result[table] = schema
print(f" Found {len(schema)} columns")
# Save to JSON file
with open("database_schema.json", "w") as f:
json.dump(result, f, indent=2)
print(f"\nSchema exported to database_schema.json")
# Also print a summary
print("\n" + "="*80)
print("SCHEMA SUMMARY")
print("="*80)
for table, schema in result.items():
print(f"\n{table.upper()} ({len(schema)} columns):")
for col in schema:
print(f" - {col['name']}: {col['type']}")