From ce7fb96faf2ca22e4bba6311bd18b42c24faa998 Mon Sep 17 00:00:00 2001 From: Christin Date: Wed, 18 Nov 2026 14:11:29 +0100 Subject: [PATCH] fix(database): reopen SQLite at the current data folder SQLiteDatabase resolved its file once, in __init__. open() then created the directory of the current storage_path but connected to the old file. Database keeps the backend while the provider stays the same, so after a data folder change and a reopen, EOS kept reading and writing the database of the old folder. LMDB resolves its directory on every open and was not affected. Tests showed it as order dependence: test_count_records[SQLite] found the five records of test_load_records_with_range[SQLite] when both ran in a row. Under pytest-xdist, ten SQLite tests failed this way. open() now resolves the file from the current storage_path, like LMDB. Refs #1553 Co-Authored-By: Claude Fable 5.2 Signed-off-by: SchaafAlexander <86607129+SchaafAlexander@users.noreply.github.com> Assisted-by: Claude Code (claude-fable-5-2) --- src/akkudoktoreos/core/database.py | 3 +++ tests/test_database.py | 23 +++++++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/src/akkudoktoreos/core/database.py b/src/akkudoktoreos/core/database.py index 1650d96c..9c767eb7 100644 --- a/src/akkudoktoreos/core/database.py +++ b/src/akkudoktoreos/core/database.py @@ -688,6 +688,9 @@ class SQLiteDatabase(DatabaseBackendABC): if self.is_open: return + # Resolve the file on every open, like LMDB does with its directory: the + # data folder may have changed since this backend was created. + self.db_file = self.storage_path / "data.db" self.storage_path.mkdir(parents=True, exist_ok=True) self.conn = sqlite3.connect( diff --git a/tests/test_database.py b/tests/test_database.py index a65d2909..853d12fe 100644 --- a/tests/test_database.py +++ b/tests/test_database.py @@ -600,6 +600,29 @@ class TestSQLiteDatabase: await db.close() + @pytest.mark.asyncio + async def test_sqlite_reopen_uses_current_data_folder(self, config_eos, tmp_path): + """A reopen after a data folder change must not reconnect to the old file.""" + config_eos.database.provider = "SQLite" + db = get_database() + await db.open() + await db.save_records([(b"2024-01-01T00:00:00+00:00", b"old")]) + backend = db._db + assert isinstance(backend, SQLiteDatabase) + old_file = backend.db_file + await db.close() + + config_eos.general.data_folder_path = tmp_path / "other" + await db.open() + + # The provider did not change, so the same backend object is opened again. + assert db._db is backend + assert backend.db_file == tmp_path / "other" / "db" / "sqlitedatabase" / "data.db" + assert backend.db_file != old_file + assert await db.count_records() == 0 + + await db.close() + @pytest.mark.asyncio async def test_sqlite_namespace_isolation(self, config_eos): """Records in different namespaces must not interfere."""