From 5b8daab4573d9f72ef5715dbf6cfc992ba7b0fce Mon Sep 17 00:00:00 2001 From: showtan001 <240788545@qq.com> Date: Sun, 30 Aug 2026 21:50:57 +0800 Subject: [PATCH] Recover task metadata store schema on startup --- storage/task_metadata.py | 20 ++++++++++++++++++++ tests/test_task_metadata.py | 10 ++++++++++ 2 files changed, 30 insertions(+) create mode 100644 tests/test_task_metadata.py diff --git a/storage/task_metadata.py b/storage/task_metadata.py index 7891774..c4e0c2e 100644 --- a/storage/task_metadata.py +++ b/storage/task_metadata.py @@ -133,4 +133,24 @@ class TaskMetadataStore: def _connect(self) -> sqlite3.Connection: connection = sqlite3.connect(self.db_path) connection.row_factory = sqlite3.Row + # A previous interrupted startup can leave a zero-byte SQLite file + # behind before ``__init__`` reaches ``_ensure_schema``. Ensure the + # table exists on every connection so the console can recover without + # manual deletion or database repair. + connection.execute( + """ + create table if not exists tasks ( + id text primary key, + goal text not null, + device_id text not null, + status text not null, + created_at text not null, + updated_at text not null, + completed_at text, + failure_reason text, + source_task_id text, + source_attempt integer + ) + """ + ) return connection diff --git a/tests/test_task_metadata.py b/tests/test_task_metadata.py new file mode 100644 index 0000000..01ab964 --- /dev/null +++ b/tests/test_task_metadata.py @@ -0,0 +1,10 @@ +from storage.task_metadata import TaskMetadataStore + + +def test_store_recovers_from_preexisting_empty_database(tmp_path) -> None: + path = tmp_path / "task_progress.sqlite3" + path.touch() + + store = TaskMetadataStore(path) + + assert store.list_tasks() == []