mirror of
https://github.com/rommapp/romm.git
synced 2026-11-28 17:48:31 +01:00
The roms_metadata VIEW re-derived its facet columns by JSON-parsing up to eight raw provider-metadata blobs on every roms row, on every query, making galleries, search, and sorts CPU-bound on large libraries. Move the per-row derivation into STORED generated columns on roms (rm_*), maintained automatically by the database engine (no application hook, no background job). roms_metadata is redefined as a thin projection over them, so the RomMetadata model, every handler query, and the virtual_collections view are unchanged and inherit the speedup. Indexes on the release-date, rating, and player-count columns back the corresponding sorts and filters. On a 75k-row MariaDB library: release-date sort 1.15s -> 0.05s, facet-value scan 3.35s -> 0.18s, player-count filter 0.54s -> 0.04s; virtual_collections 4.2s -> 1.3s. Generated columns match the old view byte-for-byte across all rows except the quoted-string dates the old view silently truncated to 0. Cross-engine correctness: - MariaDB JSON_UNQUOTE before every numeric CAST avoids strict-mode INSERT truncation on quoted values. - PostgreSQL to_timestamp()/jsonb_build_array() are only STABLE and illegal in generated columns; the gamelist date and age-ratings derivations run through IMMUTABLE wrapper functions. Gamelist dates are parsed as UTC on both engines so the stored value is time-zone independent. Fixes #3768. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
78 lines
2.8 KiB
Python
78 lines
2.8 KiB
Python
"""End-to-end checks for the roms_metadata derivations.
|
|
|
|
roms_metadata is a view over STORED generated columns on ``roms`` (migration
|
|
0098). These tests write raw provider-metadata blobs and assert the values the
|
|
engine derives and exposes through ``Rom.metadatum``, covering the facet
|
|
columns, the rating average, the gamelist date wrapper, and the quoted-string
|
|
date that the old view silently truncated to ``0``.
|
|
"""
|
|
|
|
from handler.database import db_rom_handler
|
|
from models.rom import Rom
|
|
|
|
|
|
def _reload(rom: Rom) -> Rom:
|
|
reloaded = db_rom_handler.get_rom(rom.id)
|
|
assert reloaded is not None
|
|
return reloaded
|
|
|
|
|
|
class TestGeneratedMetadata:
|
|
def test_igdb_derivations(self, rom: Rom):
|
|
db_rom_handler.update_rom(
|
|
rom.id,
|
|
{
|
|
"igdb_metadata": {
|
|
"genres": ["Action", "RPG"],
|
|
"franchises": ["Zelda"],
|
|
"game_modes": ["Single player"],
|
|
"total_rating": "80",
|
|
"first_release_date": "1569369600",
|
|
"age_ratings": [{"rating": "E"}, {"rating": "T"}],
|
|
"player_count": "4",
|
|
},
|
|
},
|
|
)
|
|
|
|
meta = _reload(rom).metadatum
|
|
assert meta.genres == ["Action", "RPG"]
|
|
assert meta.franchises == ["Zelda"]
|
|
assert meta.game_modes == ["Single player"]
|
|
assert meta.age_ratings == ["E", "T"]
|
|
assert meta.player_count == "4"
|
|
assert meta.average_rating == 80.0
|
|
# Quoted-string epoch seconds -> ms. The old view truncated this to 0.
|
|
assert meta.first_release_date == 1569369600000
|
|
|
|
def test_rating_average_across_providers(self, rom: Rom):
|
|
db_rom_handler.update_rom(
|
|
rom.id,
|
|
{
|
|
"igdb_metadata": {"total_rating": "84"},
|
|
"ss_metadata": {"ss_score": "8"}, # * 10 -> 80
|
|
},
|
|
)
|
|
|
|
meta = _reload(rom).metadatum
|
|
assert meta.average_rating == 82.0
|
|
|
|
def test_precedence_falls_through_to_gamelist_date(self, rom: Rom):
|
|
# No earlier provider supplies first_release_date, so the gamelist
|
|
# branch (parsed through the immutable UTC wrapper) wins.
|
|
db_rom_handler.update_rom(
|
|
rom.id,
|
|
{"gamelist_metadata": {"first_release_date": "20200101T000000"}},
|
|
)
|
|
|
|
meta = _reload(rom).metadatum
|
|
assert meta.first_release_date == 1577836800000 # 2020-01-01T00:00:00Z
|
|
|
|
def test_empty_metadata_yields_defaults(self, rom: Rom):
|
|
meta = _reload(rom).metadatum
|
|
assert meta.genres == []
|
|
assert meta.franchises == []
|
|
assert meta.age_ratings == []
|
|
assert meta.player_count == "1"
|
|
assert meta.first_release_date is None
|
|
assert meta.average_rating is None
|