62 lines
2.3 KiB
Python
62 lines
2.3 KiB
Python
|
|
import pymysql, sys
|
||
|
|
|
||
|
|
TARGETS = [
|
||
|
|
("charge_records", "energy"),
|
||
|
|
("charge_records", "start_time"),
|
||
|
|
("charge_records", "end_time"),
|
||
|
|
("energy_stats", "total_energy"),
|
||
|
|
("download_tasks", "completed_at"),
|
||
|
|
("organizations", "parent_id"),
|
||
|
|
]
|
||
|
|
|
||
|
|
SQL = """
|
||
|
|
SELECT TABLE_NAME, COLUMN_NAME, COLUMN_TYPE, DATA_TYPE, IS_NULLABLE,
|
||
|
|
COLUMN_DEFAULT, EXTRA, COLUMN_COMMENT
|
||
|
|
FROM information_schema.COLUMNS
|
||
|
|
WHERE TABLE_SCHEMA = %s AND ((TABLE_NAME, COLUMN_NAME) IN (%s))
|
||
|
|
ORDER BY TABLE_NAME, COLUMN_NAME
|
||
|
|
"""
|
||
|
|
|
||
|
|
IDX = """
|
||
|
|
SELECT TABLE_NAME, INDEX_NAME, GROUP_CONCAT(COLUMN_NAME ORDER BY SEQ_IN_INDEX) cols, NON_UNIQUE
|
||
|
|
FROM information_schema.STATISTICS
|
||
|
|
WHERE TABLE_SCHEMA=%s AND TABLE_NAME IN ('organizations','download_tasks','energy_stats','charge_records','charge_orders')
|
||
|
|
GROUP BY TABLE_NAME, INDEX_NAME, NON_UNIQUE ORDER BY TABLE_NAME, INDEX_NAME
|
||
|
|
"""
|
||
|
|
|
||
|
|
FK = """
|
||
|
|
SELECT TABLE_NAME, CONSTRAINT_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
|
||
|
|
ORDER BY TABLE_NAME, CONSTRAINT_NAME
|
||
|
|
"""
|
||
|
|
|
||
|
|
for db in ("pms_dev", "pms"):
|
||
|
|
conn = pymysql.connect(host="10.8.0.252", user="root", password="Hbhyg731024@", database=db, charset="utf8mb4")
|
||
|
|
cur = conn.cursor()
|
||
|
|
print("=" * 78)
|
||
|
|
print("DB:", db)
|
||
|
|
print("=" * 78)
|
||
|
|
placeholders = ",".join(["(%s,%s)"] * len(TARGETS))
|
||
|
|
flat = [db] + [x for t in TARGETS for x in t]
|
||
|
|
cur.execute(SQL % ("%s", placeholders), flat)
|
||
|
|
for r in cur.fetchall():
|
||
|
|
print(f" {r[0]}.{r[1]:<14} type={r[2]:<18} data={r[3]:<10} null={r[4]:<4} default={str(r[5]):<20} extra={r[6]:<20} comment={r[7]}")
|
||
|
|
print(" -- 索引 --")
|
||
|
|
cur.execute(IDX, (db,))
|
||
|
|
for r in cur.fetchall():
|
||
|
|
print(f" {r[0]:<18} {r[1]:<38} ({r[2]}) uniq={0 if r[3]==0 else 1}")
|
||
|
|
print(" -- 外键 --")
|
||
|
|
cur.execute(FK, (db,))
|
||
|
|
for r in cur.fetchall():
|
||
|
|
print(f" {r[0]:<18} {r[1]:<40} {r[2]} -> {r[3]}.{r[4]}")
|
||
|
|
# row counts
|
||
|
|
print(" -- 行数 --")
|
||
|
|
for t in ("charge_records", "energy_stats", "download_tasks", "organizations", "charge_orders"):
|
||
|
|
try:
|
||
|
|
cur.execute(f"SELECT COUNT(*) FROM {t}")
|
||
|
|
print(f" {t:<18} {cur.fetchone()[0]}")
|
||
|
|
except Exception as e:
|
||
|
|
print(f" {t:<18} ERR {e}")
|
||
|
|
conn.close()
|