diff --git a/backend/endpoints/sockets/scan.py b/backend/endpoints/sockets/scan.py index d4bc3d0a0..07443dab8 100644 --- a/backend/endpoints/sockets/scan.py +++ b/backend/endpoints/sockets/scan.py @@ -1420,10 +1420,23 @@ async def scan_platforms( log.info(f"{emoji.EMOJI_CHECK_MARK} Scan completed") + # The scan may have added platforms. + db_platforms = db_platform_handler.get_platforms() + db_platforms_by_slug = {p.fs_slug: p for p in db_platforms} + # Repair any group two scan workers rewrote at once. This runs first # because the cache drop and the smart collection refresh read the groups. try: - db_rom_handler.rebuild_sibling_groups() + if roms_ids: + db_rom_handler.regroup_siblings(scanned_rom_ids) + else: + db_rom_handler.rebuild_sibling_groups( + [ + db_platforms_by_slug[slug].id + for slug in platforms_to_scan + if slug in db_platforms_by_slug + ] + ) except Exception as e: log.error(f"Couldn't rebuild the sibling groups: {e}") @@ -1459,10 +1472,6 @@ async def scan_platforms( # Export metadata files if enabled in config config = cm.get_config() - # Update the list of platforms after the scan to ensure we have the latest data - db_platforms = db_platform_handler.get_platforms() - db_platforms_by_slug = {p.fs_slug: p for p in db_platforms} - # Export only what this scan touched: a new-platforms scan skips every # platform already in the database, so rewriting those is wasted work. if config.GAMELIST_AUTO_EXPORT_ON_SCAN: diff --git a/backend/handler/database/roms_handler.py b/backend/handler/database/roms_handler.py index 91e8ed716..896e575e7 100644 --- a/backend/handler/database/roms_handler.py +++ b/backend/handler/database/roms_handler.py @@ -899,8 +899,14 @@ class DBRomsHandler(DBBaseHandler): session: Session = INJECTED_SESSION, ) -> Rom: rom = session.merge(rom) + state = sa_inspect(rom) + identity_changed = state.pending or any( + state.attrs[column].history.has_changes() + for column in IDENTITY_KEY_SOURCE_COLUMNS + ) session.flush() - sibling_groups.regroup(session, [rom.id]) + if identity_changed: + sibling_groups.regroup(session, [rom.id]) return session.scalars(query.filter_by(id=rom.id).limit(1)).one() @@ -4107,11 +4113,24 @@ class DBRomsHandler(DBBaseHandler): @begin_session def rebuild_sibling_groups( self, + platform_ids: Iterable[int] | None = None, *, session: Session = INJECTED_SESSION, ) -> None: - """Recompute every sibling group, repairing any a concurrent write raced.""" - sibling_groups.rebuild(session) + """Recompute the sibling groups on `platform_ids`, or on every platform when None.""" + sibling_groups.rebuild( + session, None if platform_ids is None else set(platform_ids) + ) + + @begin_session + def regroup_siblings( + self, + rom_ids: Iterable[int], + *, + session: Session = INJECTED_SESSION, + ) -> None: + """Recompute the sibling groups `rom_ids` belong to.""" + sibling_groups.regroup(session, set(rom_ids)) @staticmethod def invalidate_filter_values_cache() -> None: diff --git a/backend/handler/database/sibling_groups.py b/backend/handler/database/sibling_groups.py index e58604abb..f8a097890 100644 --- a/backend/handler/database/sibling_groups.py +++ b/backend/handler/database/sibling_groups.py @@ -89,10 +89,18 @@ def regroup(session: Session, rom_ids: Collection[int]) -> None: _write(session, sibling_groups(key_rows), scope=visited) -def rebuild(session: Session) -> None: - """Recompute every group from scratch.""" - rows = session.execute(select(RomIdentityKey.rom_id, *_KEY_COLUMNS)).all() - _write(session, sibling_groups(_as_key_rows(rows)), scope=None) +def rebuild(session: Session, platform_ids: Collection[int] | None = None) -> None: + """Recompute the groups on `platform_ids` (every platform when None) from scratch.""" + query = select(RomIdentityKey.rom_id, *_KEY_COLUMNS) + scope: Collection[int] | None = None + if platform_ids is not None: + # Keys carry their platform, so no group spans two platforms. + query = query.where(RomIdentityKey.platform_id.in_(platform_ids)) + scope = session.scalars( + select(Rom.id).where(Rom.platform_id.in_(platform_ids)) + ).all() + rows = session.execute(query).all() + _write(session, sibling_groups(_as_key_rows(rows)), scope=scope) def _key_rows( diff --git a/backend/tests/handler/database/test_rom_sibling_groups.py b/backend/tests/handler/database/test_rom_sibling_groups.py index 0add488b2..6c228ee16 100644 --- a/backend/tests/handler/database/test_rom_sibling_groups.py +++ b/backend/tests/handler/database/test_rom_sibling_groups.py @@ -127,6 +127,32 @@ class TestSiblingGroups: assert _groups() == {first.id: first.id, bridge.id: first.id, last.id: first.id} + def test_a_platform_rebuild_leaves_other_platforms_alone( + self, platform: Platform, other_platform: Platform + ): + first, bridge, last = _chain(platform) + usa = make_rom(other_platform, "Other (USA)", igdb_id=10) + japan = make_rom(other_platform, "Other (Japan)", igdb_id=10) + with sync_session.begin() as session: + session.execute(delete(RomSiblingGroup)) + + db_rom_handler.rebuild_sibling_groups([platform.id]) + + assert _groups() == {first.id: first.id, bridge.id: first.id, last.id: first.id} + assert usa.id not in _groups() and japan.id not in _groups() + + def test_re_adding_a_rom_regroups_only_when_its_ids_change( + self, platform: Platform, mocker + ): + rom = make_rom(platform, "Readd (USA)", igdb_id=70) + regroup = mocker.spy(sibling_groups, "regroup") + + make_rom(platform, "Readd (USA) Rev 1", id=rom.id, igdb_id=70) + assert regroup.call_count == 0 + + make_rom(platform, "Readd (USA)", id=rom.id, igdb_id=71) + assert regroup.call_count == 1 + def test_two_writers_grouping_one_rom_both_commit(self, platform: Platform): """The second writer reads before the first commits, then meets its row.""" existing = make_rom(platform, "Raced (USA)", igdb_id=90)