Files
group_xinghuo_jinrong/scripts/dev/dump_schema.py
T

56 lines
1.6 KiB
Python
Raw Normal View History

"""导出数据分析 Agent 可读表的列结构(用于 NL2SQL 的 schema 元数据注入)。"""
import pathlib
import pymysql
def load_env(path=".env"):
env = {}
for line in pathlib.Path(path).read_text(encoding="utf-8").splitlines():
line = line.strip()
if not line or line.startswith("#") or "=" not in line:
continue
k, v = line.split("=", 1)
env[k.strip()] = v.strip()
return env
TABLES = [
("jinrong_core", "core_customer"),
("jinrong_core", "core_customer_risk"),
("jinrong_core", "core_customer_advisor"),
("jinrong_core", "core_holding"),
("jinrong_core", "core_trade"),
("jinrong_core", "core_cash_flow"),
("jinrong_core", "core_product"),
("jinrong_core", "core_product_nav"),
("jinrong_core", "core_staff"),
("jinrong_core", "core_risk_grade"),
("jinrong_agent", "risk_alert"),
]
def main():
env = load_env()
conn = pymysql.connect(
host=env.get("MYSQL_HOST", "127.0.0.1"),
port=int(env.get("MYSQL_PORT", "3306")),
user=env.get("MYSQL_USER", "root"),
password=env.get("MYSQL_PASSWORD", ""),
charset="utf8mb4",
)
with conn.cursor() as cur:
for db, tbl in TABLES:
cur.execute(
"SELECT column_name, data_type FROM information_schema.columns "
"WHERE table_schema=%s AND table_name=%s ORDER BY ordinal_position",
(db, tbl),
)
cols = [f"{c[0]}:{c[1]}" for c in cur.fetchall()]
print(f"{tbl} ({db}) -> {', '.join(cols)}")
conn.close()
if __name__ == "__main__":
main()