From 5be899bf1f05b3e52b5274fe762d0ec94ee94652 Mon Sep 17 00:00:00 2001 From: lrnselfreliance <33263708+lrnselfreliance@users.noreply.github.com> Date: Mon, 2 Nov 2026 11:10:48 -0700 Subject: [PATCH] Saving zims.yaml keeps the entries of Zims the database does not have yet (#935) A rebuilt database imports zims.yaml before the refresh has modeled a single Zim. That import has no database version to compare with, so it saves the config afterward, and the save wrote only the Zims in the database: none. The file's choices were erased and every Zim was then modeled with the default. The dump now starts from the file's entries and overlays the database's, so a choice survives until its Zim is modeled again, and a Zim in the database still decides its own line. Co-authored-by: Claude Fable 5.1 --- modules/zim/lib.py | 7 +++++-- modules/zim/test/test_config.py | 29 +++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/modules/zim/lib.py b/modules/zim/lib.py index 180dfbfb..2d42edd2 100644 --- a/modules/zim/lib.py +++ b/modules/zim/lib.py @@ -843,11 +843,14 @@ class ZimsConfig(ConfigFile): raise def dump_config(self, file: pathlib.Path = None, send_events=False, overwrite=False): - """Save every Zim's settings. Of several versions of one Zim, the newest decides.""" + """Save every Zim's settings. Of several versions of one Zim, the newest decides. + + The file's entries for Zims the database does not have are kept: a rebuilt database imports this config + (and saves it) before the refresh has modeled a single Zim, and the choices must survive until then.""" with get_db_session() as session: version = self.db_version_for_dump(session) + auto_search = self.auto_search # Sorted by path, so the newest version of a Zim (its date sorts last) wins. - auto_search = dict() for zim in session.query(Zim).order_by(Zim.path): auto_search[zim_config_name(zim.path)] = bool(zim.auto_search) self._config['auto_search'] = dict(sorted(auto_search.items())) diff --git a/modules/zim/test/test_config.py b/modules/zim/test/test_config.py index 1eb915f0..7b55d391 100644 --- a/modules/zim/test/test_config.py +++ b/modules/zim/test/test_config.py @@ -97,3 +97,32 @@ async def test_older_file_saves_the_database(test_session, test_directory, async data = config.read_config_file() assert data['auto_search'] == {'wikipedia_en_all_maxi': False} assert data['version'] > saved_version + + +@pytest.mark.asyncio +async def test_dump_keeps_zims_the_database_does_not_have(test_session, test_directory, async_client, + test_wrolpi_config, zim_path_factory): + """A rebuilt database imports zims.yaml before any Zim is modeled, then saves the config (an import without a + known version does). That save must keep the file's choices for Zims not yet in the database, or the refresh + that follows models every Zim with the default.""" + config = lib.get_zims_config() + config.write_config_data(dict(version=3, auto_search={'wikiversity_en_all_maxi': False, 'ham': True}), + config.get_file()) + assert test_session.query(Zim).count() == 0, 'The database is empty, as after a rebuild' + + config.import_config() + config.dump_config() + assert config.read_config_file()['auto_search'] == {'ham': True, 'wikiversity_en_all_maxi': False}, \ + 'The dump kept the file entries for Zims the database does not have' + + # The refresh models the Zims afterward; the choice is applied. + file_group = FileGroup.from_paths(test_session, zim_path_factory('wikiversity_en_all_maxi_2026-05.zim')) + zim = lib.model_zim(file_group, test_session) + test_session.commit() + assert zim.auto_search is False + + # A Zim in the database overrides the file's stale value for its name. + zim.auto_search = True + test_session.commit() + config.dump_config() + assert config.read_config_file()['auto_search'] == {'ham': True, 'wikiversity_en_all_maxi': True}