fix(converto): backfill rom-converto metadata on unchanged files

Addresses Greptile's review on #5002: quick scans reused unchanged file
rows without queuing them for rom-converto, so files scanned before the
upgrade or before scan_metadata was enabled never got the new columns.
A converto_read_at marker on rom_files now records the last successful
read, and unchanged rows without one are queued on the next scan.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Georges-Antoine Assi
2026-10-02 09:28:22 -04:00
parent f7cb635b16
commit 90947dda64
6 changed files with 49 additions and 3 deletions

View File

@@ -36,6 +36,7 @@ def _columns() -> list[sa.Column]:
sa.Column("compression", sa.String(length=255), nullable=True),
sa.Column("file_format", sa.String(length=255), nullable=True),
sa.Column("uncompressed_size_bytes", sa.BigInteger(), nullable=True),
sa.Column("converto_read_at", sa.TIMESTAMP(timezone=True), nullable=True),
sa.Column("icon_path", sa.String(length=1024), nullable=True),
sa.Column("banner_path", sa.String(length=1024), nullable=True),
sa.Column("background_path", sa.String(length=1024), nullable=True),

View File

@@ -329,6 +329,9 @@ ROM_FILTERS_CACHE_VERSION_KEY = "filter_values:ver"
ROM_FILTERS_CACHE_TTL = 60 * 60 * 24 * 7 # 7 days
ROM_FILTERS_CACHE_SCHEMA_VERSION = get_version().replace(".", "_")
# What a scan carries over from a file's previous rom-converto read.
ROM_FILE_CONVERTO_KEPT_COLUMNS = (*ROM_FILE_CONVERTO_COLUMNS, "converto_read_at")
# Columns copied from a scanned (transient) RomFile onto its database row.
ROM_FILE_SCANNED_COLUMNS = (
"file_name",
@@ -340,7 +343,7 @@ ROM_FILE_SCANNED_COLUMNS = (
"sha1_hash",
"ra_hash",
"chd_sha1_hash",
*ROM_FILE_CONVERTO_COLUMNS,
*ROM_FILE_CONVERTO_KEPT_COLUMNS,
"archive_members",
"category",
)
@@ -2643,7 +2646,7 @@ class DBRomsHandler(DBBaseHandler):
row,
ROM_FILE_SCANNED_COLUMNS,
RomFile,
keep_when_unset=ROM_FILE_CONVERTO_COLUMNS if keep_converto else (),
keep_when_unset=ROM_FILE_CONVERTO_KEPT_COLUMNS if keep_converto else (),
)
if row.missing_from_fs:

View File

@@ -8,6 +8,7 @@ import struct
import zlib
from collections.abc import Mapping, Sequence
from dataclasses import dataclass, field, replace
from datetime import datetime, timezone
from pathlib import Path
from typing import Any, Final, NotRequired, TypedDict
@@ -692,12 +693,14 @@ class FSRomsHandler(FSHandler):
return {}
reader = info_batch or rom_converto_service
infos = await reader.read_infos([p for p, _ in sources])
read_at = datetime.now(timezone.utc)
images: dict[RomFile, RomConvertoImages] = {}
for path, rom_file in sources:
info = infos.get(path)
if info is None:
continue
images[rom_file] = info.images
rom_file.converto_read_at = read_at
for column in ROM_FILE_CONVERTO_COLUMNS:
setattr(rom_file, column, getattr(info, column))
rom_file.regions = normalize_provider_regions(info.regions) or None
@@ -873,6 +876,8 @@ class FSRomsHandler(FSHandler):
row.category = self._folder_category(rom, rel_dir)
rom_files.append(row)
_record_title_id_source(abs_file_path, row)
if row.converto_read_at is None:
_record_converto_source(abs_file_path, row)
continue
if hashable_platform:
@@ -935,6 +940,8 @@ class FSRomsHandler(FSHandler):
rom_files.append(flat_row)
top_level_changed = False
_record_title_id_source(rom_dir, flat_row)
if flat_row.converto_read_at is None:
_record_converto_source(rom_dir, flat_row)
elif hashable_platform and rom_ext in ARCHIVE_READERS:
# Multi-file archive: compute a composite hash across all
# internal entries (in ASCII path order) for hash-database

View File

@@ -444,6 +444,10 @@ class RomFile(BaseModel):
uncompressed_size_bytes: Mapped[int | None] = mapped_column(
BigInteger, default=None
)
# When rom-converto last recognized the file; NULL queues it on the next scan.
converto_read_at: Mapped[datetime | None] = mapped_column(
TIMESTAMP(timezone=True), default=None
)
# Where the images rom-converto read from the file are stored, relative
# to RESOURCES_BASE_PATH. Written after the scan, never by it.
icon_path: Mapped[str | None] = mapped_column(String(length=1024), default=None)

View File

@@ -7,6 +7,7 @@ the columns derived from `name` / `fs_name` / `fs_path` in sync explicitly.
import re
import struct
from collections.abc import Iterator
from datetime import datetime, timezone
from typing import Any
from unittest.mock import MagicMock
@@ -458,6 +459,7 @@ class TestSyncRomFiles:
scanned.last_modified = 1000.0
for column, value in metadata.items():
setattr(scanned, column, value)
scanned.converto_read_at = datetime(2026, 1, 1, tzinfo=timezone.utc)
(second,) = _sync(rom, [scanned])
@@ -481,6 +483,7 @@ class TestSyncRomFiles:
if sigil_title_id is not None:
expected["title_id"] = sigil_title_id
assert {column: getattr(stored, column) for column in metadata} == expected
assert (stored.converto_read_at is not None) == keep_metadata
@pytest.mark.parametrize(
("stored_mtime", "scanned_mtime", "matching_hashes"),

View File

@@ -447,7 +447,8 @@ class TestGetRomFilesWithConverto:
@pytest.mark.asyncio
async def test_unchanged_rows_are_not_reread(self, scan_env, sigil_reads, mocker):
read_infos = mocker.AsyncMock(return_value={})
discs = [scan_env.rom_dir / f"Game (Disc {n}).iso" for n in (1, 2)]
read_infos = mocker.AsyncMock(return_value={disc: _info() for disc in discs})
mocker.patch.object(rom_converto_service, "read_infos", read_infos)
first = await scan_env.handler.get_rom_files(
scan_env.rom, calculate_hashes=False
@@ -465,6 +466,33 @@ class TestGetRomFilesWithConverto:
"readme.nfo",
}
@pytest.mark.asyncio
async def test_unchanged_rows_rom_converto_never_read_are_backfilled(
self, scan_env, sigil_reads, mocker
):
disc1 = scan_env.rom_dir / "Game (Disc 1).iso"
read_infos = mocker.AsyncMock(return_value={})
mocker.patch.object(rom_converto_service, "read_infos", read_infos)
first = await scan_env.handler.get_rom_files(
scan_env.rom, calculate_hashes=False
)
read_infos.reset_mock()
read_infos.return_value = {disc1: _info(title="PaRappa the Rapper")}
rescan = await scan_env.handler.get_rom_files(
scan_env.rom, calculate_hashes=False, existing_files=first.rom_files
)
read_infos.assert_awaited_once()
assert set(read_infos.await_args.args[0]) == {
disc1,
scan_env.rom_dir / "Game (Disc 2).iso",
}
by_name = {f.file_name: f for f in rescan.rom_files}
assert by_name["Game (Disc 1).iso"].title == "PaRappa the Rapper"
assert by_name["Game (Disc 1).iso"].converto_read_at is not None
assert by_name["Game (Disc 2).iso"].converto_read_at is None
@pytest.mark.parametrize(
("service_state", "expected"),