feat(testdata): 一键换库——history-test.db→history.db、原库留底 history-y.db(session 同理),-wal/-shm 随行,失败自动回滚;留底已存在则拒绝覆盖

This commit is contained in:
lianjianwei2024
2026-10-10 10:37:00 +08:00
parent 233a85d490
commit 1725ea3a8b
2 changed files with 58 additions and 0 deletions

View File

@@ -1101,6 +1101,49 @@ def gen_prices(hc, dd, cfg, rng, words, log):
def sha_src_of(s): def sha_src_of(s):
return hashlib.sha256(("testdata-price|%s" % s).encode("utf-8")).hexdigest() return hashlib.sha256(("testdata-price|%s" % s).encode("utf-8")).hexdigest()
# ---------------- 一键换库(测试库换上,原库留底 -y) ----------------
def switch_dbs(dd, log=print):
"""history-test.db→history.db(原 history.db→history-y.db 留底),session 同理。
-wal/-shm 随主文件一起走;任何一步失败自动逆序回滚。
软件运行中会因文件占用失败——先关闭造价审核软件再点。"""
rh, th, rs, ts = db_paths(dd)
yh = os.path.join(dd, "history-y.db")
ys = os.path.join(dd, "session-y.db")
EXT = ("-wal", "-shm")
for p in (th, ts):
if not os.path.exists(p):
raise RuntimeError("!! %s 不存在——请先点「开始生成」" % os.path.basename(p))
done = [] # 已完成的 (src, dst),回滚时逆序
def mv(a, b):
os.rename(a, b)
done.append((a, b))
try:
for t, r, y in ((th, rh, yh), (ts, rs, ys)):
if os.path.exists(r):
if os.path.exists(y):
raise RuntimeError("留底 %s 已存在(上次换库留的)。确认覆盖请先手动删除它。"
% os.path.basename(y))
mv(r, y)
for e in EXT:
if os.path.exists(r + e):
mv(r + e, y + e)
mv(t, r)
for e in EXT:
if os.path.exists(t + e):
mv(t + e, r + e)
log("%s 已换上,原库留底为 %s" % (os.path.basename(t), os.path.basename(y))
if os.path.exists(y) else "%s 已换上" % os.path.basename(t))
except OSError as e:
for a2, b2 in reversed(done):
try:
os.rename(b2, a2)
except Exception:
pass
raise RuntimeError("换库失败已回滚:%s(多半是造价审核软件还在运行占用文件,请先关闭再试)" % e)
return yh, ys
# ---------------- 版本对齐(测试库搬到别的母体后用) ---------------- # ---------------- 版本对齐(测试库搬到别的母体后用) ----------------
def align_testdata(dd, log=print): def align_testdata(dd, log=print):

View File

@@ -183,6 +183,17 @@ class H(BaseHTTPRequestHandler):
self._send(200, {"ok": True, "msg": "已对齐 %d 个批次" % n}) self._send(200, {"ok": True, "msg": "已对齐 %d 个批次" % n})
except Exception as e: except Exception as e:
self._send(200, {"ok": False, "msg": str(e)}) self._send(200, {"ok": False, "msg": str(e)})
elif self.path == "/api/switch":
dd = G.find_data_dir()
if not dd:
self._send(200, {"ok": False, "msg": "找不到数据目录"})
else:
try:
G.switch_dbs(dd, log=log)
self._send(200, {"ok": True,
"msg": "换库完成!现在点「启动造价审核.bat」即可查看测试数据。"})
except Exception as e:
self._send(200, {"ok": False, "msg": str(e)})
elif self.path == "/api/delete": elif self.path == "/api/delete":
ok, msg = start_sub("delete", {}) ok, msg = start_sub("delete", {})
self._send(200, {"ok": ok, "msg": msg}) self._send(200, {"ok": ok, "msg": msg})
@@ -314,6 +325,7 @@ history-test.db / session-test.db(首次自动从真实库克隆底子),<b
<div class="row"> <div class="row">
<button id="bRun" onclick="doRun()">开始生成</button> <button id="bRun" onclick="doRun()">开始生成</button>
<button class="gray" onclick="doStop()">停止(软暂停)</button> <button class="gray" onclick="doStop()">停止(软暂停)</button>
<button style="background:var(--ok)" onclick="doSwitch()">一键换库</button>
<button class="red" onclick="doDel()">删除全部测试数据</button> <button class="red" onclick="doDel()">删除全部测试数据</button>
<button class="gray" onclick="doAlign()">版本对齐</button> <button class="gray" onclick="doAlign()">版本对齐</button>
<button class="gray" onclick="loadStats()">刷新统计</button> <button class="gray" onclick="loadStats()">刷新统计</button>
@@ -343,6 +355,9 @@ async function doStop(){await post("/api/stop",{});}
async function doDel(){if(!confirm("确认真删除全部「测试」前缀的模拟数据?此操作不可恢复!"))return; async function doDel(){if(!confirm("确认真删除全部「测试」前缀的模拟数据?此操作不可恢复!"))return;
await post("/api/delete",{});poll();} await post("/api/delete",{});poll();}
async function doAlign(){const r=await post("/api/align",{});alert(r.msg||"完成");} async function doAlign(){const r=await post("/api/align",{});alert(r.msg||"完成");}
async function doSwitch(){
if(!confirm("一键换库:测试库换上(history-test.db→history.db),原库留底为 history-y.db / session-y.db。\n\n请先关闭造价审核软件,否则文件占用会失败。继续?"))return;
const r=await post("/api/switch",{});alert(r.msg||"完成");loadStats();}
async function saveDir(which){ async function saveDir(which){
const b={}; const b={};
if(which==="ref")b.ref_dir=document.getElementById("refdir").value; if(which==="ref")b.ref_dir=document.getElementById("refdir").value;