setup_v10.py
3.08 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
"""准备 v10 环境:独立的元数据库 ai_ontology_v10 + 两套验收样例的业务表(库存补货 otlg_dm_*、4S 配件 otlg_dm2_*)。
./.venv/Scripts/python.exe scripts/setup_v10.py # 库不存在就建;存在就只重灌业务表
./.venv/Scripts/python.exe scripts/setup_v10.py --wipe # 连系统数据一起清空,回到全新状态(要先停掉 9200)
建完以后:
./.venv/Scripts/python.exe scripts/serve_v10.py # 9200
./.venv/Scripts/python.exe scripts/build_inv_ontology.py # 库存补货 → inventory 空间
./.venv/Scripts/python.exe scripts/build_dm2_ontology.py # 4S 配件 → dealer_parts 空间
系统设置(用哪个模型等)从开发库抄过来,免得新环境里模型没配好、提问用不了。
"""
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_V10", "ai_ontology_v10")
CONFIG_FROM = os.environ.get("CONFIG_FROM", "ai_ontology_dev")
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()
if DB in ("ai_ontology_dev", "ai_ontology_dev_v9", "ai_ontology_9101_v9"):
raise SystemExit(f"{DB} 是开发库或 v9 存档,不能拿来当 v10 的库")
root = create_engine(st.url_for(CONFIG_FROM), 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_v10", ignore_errors=True)
# 建表、放默认成员:用 v10 的库名起一个进程跑初始化
env = {**os.environ, "DB_NAME": DB, "DATA_DIR": "./data_v10"}
subprocess.run([sys.executable, "-c",
"from app.core import db; db.init_db(); from app.governance import users; users.seed(); print('系统表已建好')"],
cwd=ROOT, env=env, check=True)
with root.begin() as c:
c.execute(text(f"REPLACE INTO `{DB}`.otlg_config SELECT * FROM `{CONFIG_FROM}`.otlg_config"))
print(f"系统设置已从 {CONFIG_FROM} 同步")
for seed in ("seed_inventory_demo.py", "seed_dealer_parts_demo.py"):
subprocess.run([sys.executable, str(ROOT / "scripts" / seed)], cwd=ROOT, env=env, check=True)
print(f"\nv10 准备好了。业务表建在 {DB} 里:库存补货 otlg_dm_*,4S 配件 otlg_dm2_*。")
if __name__ == "__main__":
main()