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(