fix(roms): drop a provider's old metadata when a manual rematch misses (#5841)

* fix(roms): drop a provider's old metadata when a manual rematch misses

Changing a provider id to one the provider cannot resolve kept the typed id
but left the previous game's metadata blob in place for every provider except
RetroAchievements. Apply the RetroAchievements rule to every provider: on a
miss the blob is cleared, unless raw metadata for it came with the same edit.

Fixes #5836

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>

* pr-ready: code-review

Strip string match ids so a padded Flashpoint id is not read as a rematch
that misses and wipes the stored metadata, and describe the miss case in
the _refetch_changed_matches docstring.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-authored-by: Georges-Antoine Assi <contact@me.gantoine.com>
This commit is contained in:
Alex Castillo González
2026-12-02 18:01:34 -05:00
committed by GitHub
parent 6aa1198634
commit 67a233d65d
2 changed files with 93 additions and 13 deletions

View File

@@ -2003,7 +2003,7 @@ _MEDIA_RAW_METADATA_FORM_KEYS: Final = frozenset(
def _parse_match_id(field: str, value: str | None) -> int | str | None:
if field in _STRING_MATCH_ID_FIELDS:
return value or None
return (value or "").strip() or None
if field in _SCENE_MATCH_ID_FIELDS:
return scene_id_or_none(value, field.removesuffix("_id"))
return int_or_none(value)
@@ -2026,18 +2026,12 @@ async def _refetch_ss(rom: Rom, ss_id: Any) -> Mapping[str, object]:
return exc.fallback
def _ra_miss(_rom: Rom) -> dict[str, Any]:
# The typed id is kept, so the previous game's achievements must not ride along.
return {"ra_metadata": {}}
@dataclass(frozen=True)
class _MatchRefetch:
"""How an edit that changes one provider's id pulls that provider's metadata."""
source: MetadataSource
fetch: Callable[[Rom, Any], Awaitable[Mapping[str, object]]]
on_miss: Callable[[Rom], dict[str, Any]] = lambda _rom: {}
@property
def id_field(self) -> str:
@@ -2058,7 +2052,6 @@ _MATCH_REFETCHES: Final = (
_MatchRefetch(
MetadataSource.RA,
lambda rom, i: meta_ra_handler.get_rom_by_id(rom, ra_id=i, ra_hash=rom.ra_hash),
on_miss=_ra_miss,
),
_MatchRefetch(
MetadataSource.MOBY, lambda _rom, i: meta_moby_handler.get_rom_by_id(i)
@@ -2094,7 +2087,7 @@ async def _refetch_changed_matches(
rom: Rom, cleaned_data: dict[str, Any]
) -> frozenset[str]:
"""Refetch each provider whose id the edit set or replaced, and drop the
metadata of each provider whose id it cleared.
metadata of each provider whose id it cleared or whose refetch missed.
Returns:
The id fields that were refetched.
@@ -2106,9 +2099,12 @@ async def _refetch_changed_matches(
new_id = cleaned_data[refetch.id_field]
if new_id:
fetched = await refetch.fetch(rom, new_id)
cleaned_data.update(
fetched if fetched.get(refetch.id_field) else refetch.on_miss(rom)
)
if fetched.get(refetch.id_field):
cleaned_data.update(fetched)
elif refetch.metadata_field:
# The typed id is kept, so the previous game's metadata must not
# ride along; raw metadata sent with the same edit still stands.
cleaned_data.setdefault(refetch.metadata_field, {})
refetched.add(refetch.id_field)
else:
cleaned_data[refetch.id_field] = None

View File

@@ -32,7 +32,11 @@ from handler.metadata.launchbox_handler.types import LaunchboxRom
from handler.metadata.moby_handler import MobyGamesHandler, MobyGamesRom
from handler.metadata.pouet_handler import PouetHandler
from handler.metadata.ra_handler import RAGameRom, RAHandler
from handler.metadata.ss_handler import SSHandler, SSRom
from handler.metadata.ss_handler import (
ScreenScraperExhaustedError,
SSHandler,
SSRom,
)
from handler.metadata.steam_handler import SteamHandler, SteamRom
from models.collection import Collection, SmartCollection
from models.permission import PermEntity
@@ -3360,6 +3364,62 @@ class TestUpdateRomProviderRefetch:
assert getattr(refreshed, id_field) is None
assert getattr(refreshed, metadata_field) == {}
@pytest.mark.parametrize(
("id_field", "handler", "form_value", "stored_id"), REFETCH_CASES
)
def test_a_missed_refetch_drops_the_previous_games_metadata(
self,
client: TestClient,
access_token: str,
rom: Rom,
id_field: str,
handler: type,
form_value: str,
stored_id: int | str,
):
"""The typed id is kept, but the old game's metadata must not ride along."""
metadata_field = _metadata_field(id_field)
previous_id = "fp-previous" if isinstance(stored_id, str) else 99
db_rom_handler.update_rom(
rom.id, {id_field: previous_id, metadata_field: {"marker": id_field}}
)
with patch.object(handler, "get_rom_by_id", return_value={}):
response = client.put(
f"/api/roms/{rom.id}",
headers={"Authorization": f"Bearer {access_token}"},
data={id_field: form_value},
)
assert response.status_code == status.HTTP_200_OK
refreshed = db_rom_handler.get_rom(rom.id)
assert refreshed is not None
assert getattr(refreshed, id_field) == stored_id
assert getattr(refreshed, metadata_field) == {}
def test_an_exhausted_screenscraper_drops_the_previous_games_metadata(
self,
client: TestClient,
access_token: str,
rom: Rom,
):
db_rom_handler.update_rom(rom.id, {"ss_id": 99, "ss_metadata": {"marker": 1}})
with patch.object(
SSHandler,
"get_rom_by_id",
side_effect=ScreenScraperExhaustedError(SSRom(ss_id=None)),
):
response = client.put(
f"/api/roms/{rom.id}",
headers={"Authorization": f"Bearer {access_token}"},
data={"ss_id": "14"},
)
assert response.status_code == status.HTTP_200_OK
refreshed = db_rom_handler.get_rom(rom.id)
assert refreshed is not None
assert refreshed.ss_id == 14
assert refreshed.ss_metadata == {}
@pytest.mark.parametrize(
("id_field", "handler", "form_value", "stored_id"), REFETCH_CASES
)
@@ -3383,6 +3443,30 @@ class TestUpdateRomProviderRefetch:
assert response.status_code == status.HTTP_200_OK
get_rom_by_id.assert_not_called()
def test_a_padded_string_id_is_the_stored_one(
self,
client: TestClient,
access_token: str,
rom: Rom,
):
db_rom_handler.update_rom(
rom.id,
{"flashpoint_id": "fp-game", "flashpoint_metadata": {"marker": 1}},
)
with patch.object(FlashpointHandler, "get_rom_by_id") as get_rom_by_id:
response = client.put(
f"/api/roms/{rom.id}",
headers={"Authorization": f"Bearer {access_token}"},
data={"flashpoint_id": " fp-game "},
)
assert response.status_code == status.HTTP_200_OK
get_rom_by_id.assert_not_called()
refreshed = db_rom_handler.get_rom(rom.id)
assert refreshed is not None
assert refreshed.flashpoint_id == "fp-game"
assert refreshed.flashpoint_metadata == {"marker": 1}
@patch.object(
IGDBHandler,
"get_rom_by_id",