From 6e14de60ca4e6ac75227545b2623f59ee174d4bd Mon Sep 17 00:00:00 2001 From: Bob Date: Sat, 10 Oct 2026 13:16:12 +0000 Subject: [PATCH 1/2] docs(faq): how to recover a malformed SQLite database Git-Session-Id: a51c23a1-c0bb-4da7-865c-9d9ec3107471 --- src/faq.rst | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/faq.rst b/src/faq.rst index 89c7054..aa81192 100644 --- a/src/faq.rst +++ b/src/faq.rst @@ -170,6 +170,35 @@ Once you identify the process, stop it and start ActivityWatch again. instance is already listening on the port and prints a clearer message instead of a generic ``Address already in use`` error. +.. _recovering-a-corrupted-database: + +The server fails to start: "database disk image is malformed" +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +The SQLite database was damaged, usually by a crash or power loss mid-write. You can usually rebuild it with the ``sqlite3`` command-line tool: + +1. Quit ActivityWatch completely (including the tray icon), so nothing writes to the database. +2. Open the :ref:`data directory ` and back up ``sqlite.db`` together with any ``sqlite.db-wal`` and ``sqlite.db-shm`` files next to it. +3. Note the schema version of the original database:: + + sqlite3 sqlite.db 'PRAGMA user_version;' + +4. Rebuild it into a new file:: + + sqlite3 sqlite.db .recover | sqlite3 restored.db + + ``.recover`` salvages more from a badly damaged file than ``.dump``. Some ``sqlite3`` builds lack it and print ``no such table: sqlite_dbpage``; in that case delete ``restored.db`` and use ``sqlite3 sqlite.db .dump | sqlite3 restored.db`` instead. + +5. ``.dump`` does not copy the schema version. Check it on the new file with ``sqlite3 restored.db 'PRAGMA user_version;'``, and if it prints ``0``, set it to the number from step 3:: + + sqlite3 restored.db 'PRAGMA user_version = N;' + + Without this, aw-server-rust releases before the fix in `aw-server-rust#776 `_ re-run every migration on startup and crash with a duplicate-column error. Newer releases infer the version and log a warning, but setting it is harmless. + +6. Move the damaged ``sqlite.db`` (and its ``-wal``/``-shm`` files) out of the way, rename ``restored.db`` to ``sqlite.db``, and start ActivityWatch again. + +Events that sat on damaged pages are lost; everything else comes back. Background and reports: `aw-server#89 `_. + .. What happens when my computer is off or asleep? ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ From 79cdae0975a858e9f09f9540753ada7e79675134 Mon Sep 17 00:00:00 2001 From: Bob Date: Fri, 27 Nov 2026 14:20:52 +0000 Subject: [PATCH 2/2] docs(faq): checkpoint the WAL before .recover so recent events are not dropped Git-Session-Id: 2e1756a6-47e6-480b-9da0-363aac57d2ba --- src/faq.rst | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/faq.rst b/src/faq.rst index aa81192..c53aeeb 100644 --- a/src/faq.rst +++ b/src/faq.rst @@ -183,10 +183,13 @@ The SQLite database was damaged, usually by a crash or power loss mid-write. You sqlite3 sqlite.db 'PRAGMA user_version;' -4. Rebuild it into a new file:: +4. Reclaim anything still sitting in the write-ahead log, then rebuild into a new file:: + sqlite3 sqlite.db 'PRAGMA wal_checkpoint(TRUNCATE);' sqlite3 sqlite.db .recover | sqlite3 restored.db + ``.recover`` reads only the main database file and does not apply uncheckpointed frames from ``sqlite.db-wal``. Without the checkpoint, heartbeats committed just before the crash but never checkpointed would be silently dropped. If the checkpoint itself errors because the file is malformed, those WAL-only events cannot be recovered; the rest of the file still can. + ``.recover`` salvages more from a badly damaged file than ``.dump``. Some ``sqlite3`` builds lack it and print ``no such table: sqlite_dbpage``; in that case delete ``restored.db`` and use ``sqlite3 sqlite.db .dump | sqlite3 restored.db`` instead. 5. ``.dump`` does not copy the schema version. Check it on the new file with ``sqlite3 restored.db 'PRAGMA user_version;'``, and if it prints ``0``, set it to the number from step 3::