fix(scan): reassociate a renamed non-hashable ROM by its title id

Reassociation matched a moved or renamed file to its existing entry by
CRC, MD5 and SHA1, and deliberately refused a partial set. Switch and
Switch 2 are never hashed, so nothing could ever match them: a renamed
file created a duplicate and stranded the original entry, holding the
user's collections and notes, as missing from the filesystem. That is
the one platform title-id embedding renames. The binary title id is the
identity those platforms do carry, so it serves when hashes cannot,
under the same single-unambiguous-match rule.
This commit is contained in:
nendo
2026-08-27 13:29:30 +09:00
parent 5425a227ab
commit 64779f8223
4 changed files with 113 additions and 9 deletions

View File

@@ -480,6 +480,7 @@ async def _identify_rom(
crc_hash=parsed_rom_files.crc_hash,
md5_hash=parsed_rom_files.md5_hash,
sha1_hash=parsed_rom_files.sha1_hash,
title_id=parsed_rom_files.title_id,
)
if missing_match is not None:
# Move the existing entry onto the new file, clearing its missing state.

View File

@@ -2894,17 +2894,37 @@ class DBRomsHandler(DBBaseHandler):
crc_hash: str | None = None,
md5_hash: str | None = None,
sha1_hash: str | None = None,
title_id: str | None = None,
session: Session = None, # type: ignore
) -> Rom | None:
"""Find a ROM marked missing on a platform whose hashes match the file.
"""Find a ROM marked missing on a platform that identifies the file.
Used during scanning to reassociate a renamed or moved file with its
existing entry (preserving collections, notes, and assets) instead of
creating a duplicate. Requires the CRC, MD5, and SHA1 hashes to all
match. Any missing hash yields no match, so non-hashable platforms and
pre-hash entries safely fall back to creating a new entry.
creating a duplicate. All three hashes must match; any missing hash
falls through to the binary title id, which is what non-hashable
platforms like Switch carry instead. With neither, a new entry is
created.
Args:
platform_id: Platform whose missing entries are searched.
crc_hash: CRC hash of the scanned file, if hashed.
md5_hash: MD5 hash of the scanned file, if hashed.
sha1_hash: SHA1 hash of the scanned file, if hashed.
title_id: Binary title id of the scanned file, if extracted.
Returns:
The single matching ROM, or None when there is no unambiguous one.
"""
if not (crc_hash and md5_hash and sha1_hash):
identity: tuple[ColumnElement[bool], ...]
if crc_hash and md5_hash and sha1_hash:
identity = (
Rom.crc_hash == crc_hash,
Rom.md5_hash == md5_hash,
Rom.sha1_hash == sha1_hash,
)
elif title_id:
identity = (Rom.title_id == title_id,)
else:
return None
matches = session.scalars(
@@ -2913,9 +2933,7 @@ class DBRomsHandler(DBBaseHandler):
and_(
Rom.platform_id == platform_id,
Rom.missing_from_fs.is_(True),
Rom.crc_hash == crc_hash,
Rom.md5_hash == md5_hash,
Rom.sha1_hash == sha1_hash,
*identity,
)
)
.limit(2)

View File

@@ -822,6 +822,7 @@ class TestIdentifyRomReassociation:
crc_hash="crc",
md5_hash="md5",
sha1_hash="sha1",
title_id=None,
)
db.update_rom.assert_called_once()
rom_id, data = db.update_rom.call_args.args
@@ -831,6 +832,41 @@ class TestIdentifyRomReassociation:
# No brand-new row is inserted; add_rom only persists the scan result.
assert db.add_rom.call_count == 1
async def test_title_id_is_offered_when_the_platform_is_not_hashed(
self, patched, mocker
):
"""Switch files carry no hashes, so the title id is the only identity.
Without it a renamed Switch file lands as a duplicate and strands the
original entry, along with its collections and notes, as missing.
"""
db = patched
db.get_matching_missing_rom.return_value = None
mocker.patch.object(
scan_module.fs_rom_handler,
"get_rom_files",
AsyncMock(
return_value=ParsedRomFiles(
rom_files=[],
crc_hash="",
md5_hash="",
sha1_hash="",
ra_hash="",
title_id="0100ABCD12340000",
)
),
)
await self._run(db)
db.get_matching_missing_rom.assert_called_once_with(
platform_id=1,
crc_hash="",
md5_hash="",
sha1_hash="",
title_id="0100ABCD12340000",
)
async def test_files_are_reconciled_in_place(self, patched):
db = patched
db.get_matching_missing_rom.return_value = None

View File

@@ -1049,7 +1049,7 @@ def test_get_matching_missing_rom_ambiguous_match_returns_none(platform: Platfor
def test_get_matching_missing_rom_requires_all_three_hashes(platform: Platform):
"""A match needs all three hashes; omitting one yields no match."""
"""A partial hash set yields no match when there is no title id either."""
_add_missing_rom(
platform, "renamed", crc_hash="aabbccdd", md5_hash="md5val", sha1_hash="sha1val"
)
@@ -1060,6 +1060,55 @@ def test_get_matching_missing_rom_requires_all_three_hashes(platform: Platform):
assert match is None
def test_get_matching_missing_rom_matches_title_id_without_hashes(platform: Platform):
"""A non-hashable platform reassociates on the binary title id.
Switch ROMs are never hashed, so a renamed file would otherwise be
unrecoverable and land as a duplicate entry.
"""
missing = _add_missing_rom(platform, "renamed", title_id="0100ABCD12340000")
match = db_rom_handler.get_matching_missing_rom(
platform_id=platform.id, title_id="0100ABCD12340000"
)
assert match is not None
assert match.id == missing.id
def test_get_matching_missing_rom_hashes_take_precedence_over_title_id(
platform: Platform,
):
"""A hashed file is matched on its hashes, not on a shared title id."""
_add_missing_rom(
platform,
"other",
crc_hash="aabbccdd",
md5_hash="md5val",
sha1_hash="sha1val",
title_id="0100ABCD12340000",
)
match = db_rom_handler.get_matching_missing_rom(
platform_id=platform.id,
crc_hash="different",
md5_hash="different",
sha1_hash="different",
title_id="0100ABCD12340000",
)
assert match is None
def test_get_matching_missing_rom_ambiguous_title_id_returns_none(platform: Platform):
"""Two missing entries sharing a title id leave the choice to a new entry."""
_add_missing_rom(platform, "dup_a", title_id="0100ABCD12340000")
_add_missing_rom(platform, "dup_b", title_id="0100ABCD12340000")
match = db_rom_handler.get_matching_missing_rom(
platform_id=platform.id, title_id="0100ABCD12340000"
)
assert match is None
def test_get_matching_missing_rom_ignores_present_roms(platform: Platform):
"""Only ROMs marked missing are eligible for reassociation."""
db_rom_handler.add_rom(