setup_9101.py
2.75 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
"""准备 9101 的干净环境:独立的元数据库 + 4S 场景的 10 张业务表。本体、数据源、流程卡一概不建,留给人自己走。
./.venv/Scripts/python.exe scripts/setup_9101.py # 库不存在就建;存在就只重灌业务表
./.venv/Scripts/python.exe scripts/setup_9101.py --wipe # 连系统数据一起清空,回到全新状态(要先停掉 9101)
会从开发库把「系统设置」(用哪个模型等)抄过来,免得新环境里模型没配好、提问用不了。
"""
from __future__ import annotations
import argparse
import os
import shutil
import subprocess
import sys
from pathlib import Path
ROOT = Path(__file__).resolve().parent.parent
sys.path.insert(0, str(ROOT))
sys.stdout.reconfigure(encoding="utf-8")
DB = os.environ.get("DB_NAME_9101", "ai_ontology_9101")
def main() -> None:
ap = argparse.ArgumentParser()
ap.add_argument("--wipe", action="store_true", help="清空系统数据,回到全新状态")
args = ap.parse_args()
from sqlalchemy import create_engine, text
from app.core.config import get_settings
st = get_settings()
dev = st.db_name
if DB == dev:
raise SystemExit("9101 的库不能和开发库同名")
root = create_engine(st.url_for(dev), future=True)
with root.begin() as c:
exists = c.execute(text("SELECT COUNT(*) FROM information_schema.schemata WHERE schema_name = :n"), {"n": DB}).scalar()
if exists and args.wipe:
c.execute(text(f"DROP DATABASE `{DB}`"))
exists = 0
print(f"已删除旧库 {DB}")
if not exists:
c.execute(text(f"CREATE DATABASE `{DB}` DEFAULT CHARACTER SET utf8mb4"))
print(f"已建库 {DB}")
if args.wipe:
shutil.rmtree(ROOT / "data_9101", ignore_errors=True)
# 建表、放默认成员:用 9101 的库名起一个进程跑初始化
env = {**os.environ, "DB_NAME": DB, "DATA_DIR": "./data_9101"}
subprocess.run([sys.executable, "-c",
"from app.core import db; db.init_db(); from app.governance import users; users.seed(); print('ok')"],
cwd=ROOT, env=env, check=True)
# 系统设置抄开发库的(模型、检索参数)
with root.begin() as c:
c.execute(text(f"REPLACE INTO `{DB}`.otlg_config SELECT * FROM `{dev}`.otlg_config"))
print("系统设置已从开发库同步")
# 业务表
subprocess.run([sys.executable, str(ROOT / "scripts" / "seed_dealer_parts_demo.py")], cwd=ROOT, env=env, check=True)
print(f"\n9101 准备好了。启动:./.venv/Scripts/python.exe scripts/serve_9101.py,然后打开 http://localhost:9101\n"
f"接数据源时库名填 {DB},表范围填 otlg_dm2_*。")
if __name__ == "__main__":
main()