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):
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):