From 8df91d1c996ac29cddea1485f42c6e7d0aceeaeb Mon Sep 17 00:00:00 2001 From: Georges-Antoine Assi Date: Sun, 19 Jul 2026 21:56:00 -0400 Subject: [PATCH] fix: don't re-stamp updated_at for unchanged ROMs during scans bulk_mark_present issued an unconditional UPDATE setting missing_from_fs=False on every matched row, including rows already present. Since Rom.updated_at has onupdate=utc_now, every scan re-stamped all skipped/unchanged ROMs of a platform, defeating updated_after-based incremental consumers and busting every cover URL (?ts={updated_at}) on each scan. Mirror the missing-side guard (mark_missing_roms) by restricting the UPDATE to rows that actually flip via Rom.missing_from_fs.is_(True), so an unchanged platform issues no updates. Fixes #3836 Co-Authored-By: Claude Opus 4.8 (1M context) --- backend/handler/database/roms_handler.py | 8 +++- backend/tests/handler/test_db_handler.py | 47 ++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 1 deletion(-) diff --git a/backend/handler/database/roms_handler.py b/backend/handler/database/roms_handler.py index 772b769fb..cb0243d79 100644 --- a/backend/handler/database/roms_handler.py +++ b/backend/handler/database/roms_handler.py @@ -1533,7 +1533,12 @@ class DBRomsHandler(DBBaseHandler): rom_ids: list[int], session: Session = None, # type: ignore ) -> None: - """Bulk set missing_from_fs=False for a list of ROM IDs.""" + """Bulk set missing_from_fs=False for a list of ROM IDs. + + Only rows that actually flip are written, so a re-scan of an + unchanged platform issues no updates and leaves `updated_at` + untouched, keeping it a usable incremental signal. + """ if not rom_ids: return @@ -1545,6 +1550,7 @@ class DBRomsHandler(DBBaseHandler): and_( Rom.platform_id == platform_id, Rom.id.in_(chunk), + Rom.missing_from_fs.is_(True), ) ) .values(missing_from_fs=False) diff --git a/backend/tests/handler/test_db_handler.py b/backend/tests/handler/test_db_handler.py index 29f4247a9..e18d062e9 100644 --- a/backend/tests/handler/test_db_handler.py +++ b/backend/tests/handler/test_db_handler.py @@ -492,6 +492,53 @@ def test_bulk_mark_present(platform: Platform): assert updated.missing_from_fs is True +def test_bulk_mark_present_skips_already_present(platform: Platform): + """bulk_mark_present leaves updated_at untouched for already-present ROMs. + + Regression: an unchanged (already present) ROM must not be re-stamped on + each scan, so `updated_after`-based incremental consumers stay usable. + """ + present = db_rom_handler.add_rom( + Rom( + platform_id=platform.id, + name="rom_present", + slug="rom-present", + fs_name="rom_present.zip", + fs_name_no_tags="rom_present", + fs_name_no_ext="rom_present", + fs_extension="zip", + fs_path=f"{platform.slug}/roms", + missing_from_fs=False, + ) + ) + missing = db_rom_handler.add_rom( + Rom( + platform_id=platform.id, + name="rom_missing", + slug="rom-missing", + fs_name="rom_missing.zip", + fs_name_no_tags="rom_missing", + fs_name_no_ext="rom_missing", + fs_extension="zip", + fs_path=f"{platform.slug}/roms", + missing_from_fs=True, + ) + ) + + present_updated_at = db_rom_handler.get_rom(present.id).updated_at + + db_rom_handler.bulk_mark_present(platform.id, [present.id, missing.id]) + + # Already-present ROM is not re-stamped. + present_after = db_rom_handler.get_rom(present.id) + assert present_after.missing_from_fs is False + assert present_after.updated_at == present_updated_at + + # Actually-missing ROM is flipped to present. + missing_after = db_rom_handler.get_rom(missing.id) + assert missing_after.missing_from_fs is False + + def test_bulk_mark_present_empty_list(platform: Platform): """bulk_mark_present with an empty list is a no-op.""" rom = db_rom_handler.add_rom(