fix(4.0): 配置迁移失败时把备份移回原文件,重启循环不再堆积 .v3.bak.N

This commit is contained in:
Estrella Pan
2026-10-08 16:20:06 +02:00
parent fb77bd3e10
commit 5426bec0c3
2 changed files with 7 additions and 6 deletions

View File

@@ -11,7 +11,7 @@
→ ``webhook_url``(渠道实现不再读旧字段)
在 ``Settings`` 读写配置文件之前运行。改写前把原文件备份为 ``<文件名>.v3.bak``;
失败时从备份恢复并抛出 :class:`ConfigMigrationError`,拒绝启动。
失败时从备份恢复(不留下备份)并抛出 :class:`ConfigMigrationError`,拒绝启动。
本模块不能 import ``module.conf``:它在 ``settings`` 构造之前被调用。
"""
@@ -147,11 +147,12 @@ def migrate_v3_config(path: Path) -> bool:
)
os.replace(tmp, path)
except Exception as e:
shutil.copy2(backup, path)
# 移回而不是复制:备份与原文件相同,留着会让每次失败的重启多一份
os.replace(backup, path)
message = (
f"config migration to 4.0 failed at {_failed_field(e)}; "
f"{path.name} was restored from {backup.name}. Fix the field and restart. "
f"配置迁移失败,已从备份恢复 {path.name},请修正该字段后重新启动。"
f"{path.name} was left unchanged. Fix the field and restart. "
f"配置迁移失败,{path.name} 未改动,请修正该字段后重新启动。"
)
logger.critical(message)
raise ConfigMigrationError(message) from e

View File

@@ -91,8 +91,8 @@ def test_migrate_v3_config_invalid_field_restores_and_refuses(
migrate_v3_config(v3_config)
assert v3_config.read_text() == original
assert v3_config.with_name("config.json.v3.bak").read_text() == original
assert not v3_config.with_name("config.json.tmp").exists()
# 每次失败的启动都不留下新的备份(容器重启循环不会堆积 .v3.bak.N)
assert not list(v3_config.parent.glob("config.json.*"))
@pytest.mark.parametrize(