Merge pull request #3779 from rommapp/posthog-code/fix-simple-rom-eager-load

fix(backend): lightweight eager-load for /roms/{id}/simple
This commit is contained in:
Georges-Antoine Assi authored and GitHub committed 2026-07-16 20:37:45 -04:00
commit a171aa97bc
3 files changed
+83 -1

No files matched your search

+1 -1
View File
@@ -1083,7 +1083,7 @@ def get_rom_simple(
fetched on user-driven detail interactions (game details page, quick-
note dialog open, achievements panel)."""
rom = db_rom_handler.get_rom(id)
rom = db_rom_handler.get_rom_simple(id)
if not rom:
raise RomNotFoundInDatabaseException(id)
+56
View File
@@ -243,6 +243,50 @@ def with_details(func):
return wrapper
def with_simple_details(func):
"""Lightweight eager-load for the `SimpleRomSchema` (v2 gallery card).
Loads only the relationships `SimpleRomSchema` serializes (rom_users,
files, sibling_roms + their rom_users, notes) and the deferred file-count
columns, skipping the `saves` / `states` / `screenshots` / `collections`
arrays that `with_details` pulls for the detail view. Keeps per-card
hydration cheap on low-power hosts.
"""
@functools.wraps(func)
def wrapper(*args, **kwargs):
kwargs["query"] = select(Rom).options(
selectinload(Rom.platform),
selectinload(Rom.rom_users).options(noload(RomUser.rom)),
selectinload(Rom.metadatum).options(noload(RomMetadata.rom)),
selectinload(Rom.files).options(
joinedload(RomFile.rom).load_only(Rom.fs_path, Rom.fs_name),
selectinload(RomFile.track_meta),
),
selectinload(Rom.sibling_roms).options(
noload(Rom.platform),
noload(Rom.metadatum),
# Per-sibling is_main_sibling resolution needs each sibling's
# RomUser (relationship is `lazy="raise"`).
selectinload(Rom.rom_users).options(noload(RomUser.rom)),
load_only(
Rom.id,
Rom.name,
Rom.platform_id,
Rom.fs_name_no_tags,
Rom.fs_name_no_ext,
),
),
selectinload(Rom.notes),
undefer(Rom.multi_file),
undefer(Rom.top_level_file_count),
undefer(Rom.has_soundtrack),
)
return func(*args, **kwargs)
return wrapper
class DBRomsHandler(DBBaseHandler):
@begin_session
@with_details
@@ -268,6 +312,18 @@ class DBRomsHandler(DBBaseHandler):
) -> Rom | None:
return session.scalar(query.filter_by(id=id).limit(1))
@begin_session
@with_simple_details
def get_rom_simple(
self,
id: int,
*,
query: Query = None, # type: ignore
session: Session = None, # type: ignore
) -> Rom | None:
"""Get a rom by ID with only the loads `SimpleRomSchema` needs."""
return session.scalar(query.filter_by(id=id).limit(1))
@begin_session
@with_details
def get_roms_by_ids(
+26
View File
@@ -44,6 +44,32 @@ def test_get_rom(client: TestClient, access_token: str, rom: Rom):
assert body["id"] == rom.id
def test_get_rom_simple(client: TestClient, access_token: str, rom: Rom):
response = client.get(
f"/api/roms/{rom.id}/simple",
headers={"Authorization": f"Bearer {access_token}"},
)
assert response.status_code == status.HTTP_200_OK
body = response.json()
assert body["id"] == rom.id
# SimpleRomSchema stays lightweight: none of the detail-only arrays are
# present, so the endpoint must not eager-load them.
assert "user_saves" not in body
assert "user_states" not in body
assert "user_screenshots" not in body
assert "user_collections" not in body
assert "all_user_notes" not in body
def test_get_rom_simple_missing_returns_404(client: TestClient, access_token: str):
response = client.get(
"/api/roms/999999/simple",
headers={"Authorization": f"Bearer {access_token}"},
)
assert response.status_code == status.HTTP_404_NOT_FOUND
def test_download_multi_file_rom_content(
client: TestClient, access_token: str, multi_file_rom: Rom
):