- 新增 app/api、app/service 数据分析 Agent 全套服务与接口 - schemas.py 重构为 schemas 包(analyst schema) - 新增 SQL 防注入、guardrail、缓存、字典、LLM 等服务 - 新增 tests 测试套件与 scripts/dev、scripts/setup 脚本 - 补充需求规格、架构说明书、开发清单、表设计等文档
52 lines
1.6 KiB
Python
52 lines
1.6 KiB
Python
"""执行一个 SQL 文件(UTF-8,按分号切分)。
|
|
|
|
用法:python scripts/setup/apply_sql_file.py <sql文件路径>
|
|
用途:把 docs/项目框架设计/表设计/ 下的建表 SQL 灌进 jinrong_agent(避免命令行中文路径/编码问题)。
|
|
"""
|
|
import sys
|
|
import pathlib
|
|
|
|
import pymysql
|
|
|
|
|
|
def load_env(path=".env"):
|
|
env = {}
|
|
p = pathlib.Path(path)
|
|
if p.exists():
|
|
for line in p.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
|
|
|
|
|
|
def main() -> None:
|
|
if len(sys.argv) < 2:
|
|
print("用法:python scripts/setup/apply_sql_file.py <sql文件路径>")
|
|
sys.exit(2)
|
|
sql_file = sys.argv[1]
|
|
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",
|
|
autocommit=True,
|
|
)
|
|
raw = pathlib.Path(sql_file).read_text(encoding="utf-8")
|
|
# 去掉 -- 注释行后按分号切分
|
|
text = "\n".join(l for l in raw.splitlines() if not l.strip().startswith("--"))
|
|
stmts = [s.strip() for s in text.split(";") if s.strip()]
|
|
with conn.cursor() as cur:
|
|
for s in stmts:
|
|
cur.execute(s)
|
|
conn.close()
|
|
print(f"OK: 执行了 {len(stmts)} 条语句 <- {sql_file}")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|