Skip to content

armgit / peach

Public Imported from GitHub
0

Peach: Local-first video metadata scraper and media manager.

fix: 更新异常直接退出并关闭恢复数据库连接

longmeidaoac1b0d5b65ff
diff --git a/scripts/build_app_entry.py b/scripts/build_app_entry.py
index b438a5e6c..554f1ea65 100644
--- a/scripts/build_app_entry.py
+++ b/scripts/build_app_entry.py
@@ -69,7 +69,12 @@ def main(argv: list[str] | None = None) -> int:
return apply(Path(argv[2]), int(argv[3]))
if wants_cli(argv):
_prepare_console()
- return cli_main(argv[1:])
+ try:
+ return cli_main(argv[1:])
+ except Exception:
+ import traceback
+ traceback.print_exc()
+ return 1
return tray_main()
diff --git a/src/peach/standalone_update.py b/src/peach/standalone_update.py
index af142b4d4..fe19cc91e 100644
--- a/src/peach/standalone_update.py
+++ b/src/peach/standalone_update.py
@@ -1,6 +1,7 @@
"""独立 Windows 包的持久更新事务:下载准备、用户重启、目录切换与回滚。"""
from __future__ import annotations
+from contextlib import closing
import hashlib
import json
import os
@@ -199,7 +200,7 @@ def restore_database(data: dict) -> None:
backup = STATE_DIR / "update-backups" / data["id"] / "ledger.db"
if not backup.is_file():
return
- with sqlite3.connect(backup.as_uri() + "?mode=ro", uri=True) as source, sqlite3.connect(DATABASE_PATH) as destination:
+ with closing(sqlite3.connect(backup.as_uri() + "?mode=ro", uri=True)) as source, closing(sqlite3.connect(DATABASE_PATH)) as destination:
source.backup(destination)
if destination.execute("PRAGMA integrity_check").fetchone()[0] != "ok":
raise RuntimeError("数据库恢复检查失败")
diff --git a/tests/test_cli.py b/tests/test_cli.py
index b6ecb5d74..549925d52 100644
--- a/tests/test_cli.py
+++ b/tests/test_cli.py
@@ -598,6 +598,14 @@ class PackagedEntryTests(unittest.TestCase):
self.assertTrue(self.entry.wants_cli(["peach.exe", "status"]))
self.assertTrue(self.entry.wants_cli(["peach.exe", "ledger-sync"]))
+ def test_packaged_command_failure_returns_without_a_native_error_dialog(self):
+ output = io.StringIO()
+ with mock.patch.object(self.entry, "cli_main", side_effect=RuntimeError("migration failed")), mock.patch.object(
+ self.entry, "_prepare_console"
+ ), mock.patch.object(self.entry.sys, "stderr", output):
+ self.assertEqual(self.entry.main(["peach.exe", "migrate", "upgrade", "--yes"]), 1)
+ self.assertIn("migration failed", output.getvalue())
+
if __name__ == "__main__":
unittest.main()
diff --git a/tests/test_standalone_update.py b/tests/test_standalone_update.py
index 887019430..e2dcec6d7 100644
--- a/tests/test_standalone_update.py
+++ b/tests/test_standalone_update.py
@@ -114,12 +114,12 @@ class StandaloneUpdateTests(unittest.TestCase):
self.assertTrue(self.state.is_dir())
def test_migration_failure_can_restore_the_sqlite_backup(self):
- with sqlite3.connect(update.DATABASE_PATH) as connection:
+ with update.closing(sqlite3.connect(update.DATABASE_PATH)) as connection, connection:
connection.execute("CREATE TABLE fixture(value TEXT)")
connection.execute("INSERT INTO fixture VALUES ('original')")
data = {"id":"migration-fixture"}
def failed_migration(*args, **kwargs):
- with sqlite3.connect(update.DATABASE_PATH) as connection:
+ with update.closing(sqlite3.connect(update.DATABASE_PATH)) as connection, connection:
connection.execute("UPDATE fixture SET value='changed'")
return Mock(returncode=1)
with patch("peach.migrations.plan",return_value=([],[object()])), patch.object(
@@ -127,5 +127,5 @@ class StandaloneUpdateTests(unittest.TestCase):
with self.assertRaises(RuntimeError):
update.migrate_database(self.root,data)
update.restore_database(data)
- with sqlite3.connect(update.DATABASE_PATH) as connection:
+ with update.closing(sqlite3.connect(update.DATABASE_PATH)) as connection:
self.assertEqual(connection.execute("SELECT value FROM fixture").fetchone()[0],"original")