mirror of
https://github.com/rommapp/romm.git
synced 2026-11-29 18:11:16 +01:00
* Search the gallery by providers' alternative titles A search for "FF9" or "Final Fantasy 9" did not find "Final Fantasy IX": the gallery search only matched roms.name and roms.fs_name, while the alternative titles IGDB, MobyGames and ScreenScraper report lived in the raw metadata blobs where no index reaches. Add generated_search_aliases, a STORED generated column the engine fills from those blobs, so every write path (scans, metadata refreshes, the bulk update_rom, raw metadata edits) keeps it current with no backfill. Migration 0145 rebuilds the MariaDB/MySQL FULLTEXT index over (name, fs_name, aliases) and adds a pg_trgm GIN index on PostgreSQL, and the search matches the column on both paths. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01A62FTdVyav4wK5Nkj55MGy * Compare the alias JSON type under the table's collation On a utf8mb3_unicode_ci table, JSON_TYPE's result and the 'ARRAY' literal carry different collations, so creating the generated column failed with an illegal mix. CAST re-derives it, as _maria_text does. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01A62FTdVyav4wK5Nkj55MGy * pr-ready step 2 (code-review): defer the alias column, share search columns Loading a ROM no longer fetches generated_search_aliases, which only the gallery search reads. The FULLTEXT match and the ILIKE fallback both take their columns from one ROM_SEARCH_COLUMNS tuple. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01A62FTdVyav4wK5Nkj55MGy * pr-ready step 3 (simplify): register the search indexes, share the CAST Both search indexes join the generated-column index registry, so any later rebuild of the alias column puts them back instead of leaving MariaDB's FULLTEXT index narrowed (and every MATCH failing) or the PostgreSQL trgm index gone. 0145 shrinks to ensure_roms_columns plus retiring 0084's index, and its downgrade guards DROP COLUMN with has_column, since MySQL has no DROP COLUMN IF EXISTS. _maria_char carries the collation CAST that _maria_text and the alias expression share, the alias expression extracts each array twice instead of three times, and the dialect test runs the handler's own _fulltext_match. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01A62FTdVyav4wK5Nkj55MGy * Store the search aliases as plain titles The alias column held each provider's array as JSON text, so a short ILIKE search for '"', ',' or '[' matched every ROM with any alias. Both engines print an array as ["a", "b"] however it was stored, so the expression strips the brackets and quotes and keeps the titles separated by spaces, which search terms can never match on their own. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01A62FTdVyav4wK5Nkj55MGy * Shorten the alias-search docstrings to one sentence The migration and test-module docstrings explained the design in several paragraphs; AGENTS.md asks for one sentence. The design notes live in the PR description. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01A62FTdVyav4wK5Nkj55MGy --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: Georges-Antoine Assi <contact@me.gantoine.com>
154 lines
4.3 KiB
Python
154 lines
4.3 KiB
Python
"""Searching the gallery by a provider's alternative titles."""
|
|
|
|
from typing import Any
|
|
|
|
import pytest
|
|
from sqlalchemy import select
|
|
from tests.conftest import session as session_factory
|
|
from tests.sql_dialects import MARIADB_DIALECT, POSTGRESQL_DIALECT, compile_sql
|
|
|
|
from handler.database import db_rom_handler
|
|
from models.platform import Platform
|
|
from models.rom import Rom
|
|
|
|
|
|
def _add_rom(platform: Platform, name: str, **metadata: dict[str, Any]) -> Rom:
|
|
fs_name = f"{name.replace(' ', '_')}.zip"
|
|
return db_rom_handler.add_rom(
|
|
Rom(
|
|
platform_id=platform.id,
|
|
name=name,
|
|
slug=name.lower().replace(" ", "-"),
|
|
fs_name=fs_name,
|
|
fs_name_no_tags=fs_name.removesuffix(".zip"),
|
|
fs_name_no_ext=fs_name.removesuffix(".zip"),
|
|
fs_extension="zip",
|
|
fs_path=f"{platform.slug}/roms",
|
|
**metadata,
|
|
)
|
|
)
|
|
|
|
|
|
def _search_ids(term: str) -> list[int]:
|
|
return [r.id for r in db_rom_handler.get_roms_scalar(search_term=term)]
|
|
|
|
|
|
def _stored_aliases(rom: Rom) -> str | None:
|
|
with session_factory() as session:
|
|
return session.scalar(
|
|
select(Rom.generated_search_aliases).where(Rom.id == rom.id)
|
|
)
|
|
|
|
|
|
@pytest.fixture
|
|
def ff9(platform: Platform) -> Rom:
|
|
return _add_rom(
|
|
platform,
|
|
"Final Fantasy IX",
|
|
igdb_metadata={"alternative_names": ["FF9", "Final Fantasy 9"]},
|
|
)
|
|
|
|
|
|
@pytest.fixture
|
|
def unrelated(platform: Platform) -> Rom:
|
|
return _add_rom(platform, "Chrono Cross")
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"term",
|
|
[
|
|
# Every word long enough for the FULLTEXT index on MariaDB and MySQL.
|
|
"ff9",
|
|
"FF9",
|
|
# "9" is too short for it, so this one takes the ILIKE fallback.
|
|
"final fantasy 9",
|
|
],
|
|
)
|
|
def test_an_alternative_title_finds_the_rom(ff9: Rom, unrelated: Rom, term: str):
|
|
assert _search_ids(term) == [ff9.id]
|
|
|
|
|
|
def test_the_name_still_finds_the_rom(ff9: Rom, unrelated: Rom):
|
|
assert _search_ids("final fantasy ix") == [ff9.id]
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("column", "key"),
|
|
[
|
|
("igdb_metadata", "alternative_names"),
|
|
("moby_metadata", "alternate_titles"),
|
|
("ss_metadata", "alternative_names"),
|
|
],
|
|
)
|
|
def test_each_provider_contributes_its_titles(
|
|
platform: Platform, unrelated: Rom, column: str, key: str
|
|
):
|
|
rom = _add_rom(platform, "Seiken Densetsu", **{column: {key: ["Secret of Mana"]}})
|
|
|
|
assert _search_ids("secret of mana") == [rom.id]
|
|
|
|
|
|
def test_titles_from_every_provider_are_searchable_together(platform: Platform):
|
|
rom = _add_rom(
|
|
platform,
|
|
"Final Fantasy IX",
|
|
igdb_metadata={"alternative_names": ["FF9"]},
|
|
ss_metadata={"alternative_names": ["FFIX"]},
|
|
)
|
|
|
|
assert _search_ids("ff9") == [rom.id]
|
|
assert _search_ids("ffix") == [rom.id]
|
|
|
|
|
|
def test_a_rom_without_titles_has_no_aliases(platform: Platform):
|
|
rom = _add_rom(
|
|
platform,
|
|
"Chrono Trigger",
|
|
igdb_metadata={"alternative_names": []},
|
|
moby_metadata={"alternate_titles": "not a list"},
|
|
)
|
|
|
|
assert _stored_aliases(rom) is None
|
|
|
|
|
|
def test_the_aliases_are_stored_as_plain_titles(platform: Platform):
|
|
rom = _add_rom(
|
|
platform,
|
|
"Final Fantasy IX",
|
|
igdb_metadata={"alternative_names": ["FF9", "Final Fantasy 9"]},
|
|
ss_metadata={"alternative_names": ["FFIX"]},
|
|
)
|
|
|
|
assert _stored_aliases(rom) == "FF9 Final Fantasy 9 FFIX"
|
|
|
|
|
|
@pytest.mark.parametrize("term", ['"', ",", "["])
|
|
def test_json_punctuation_matches_nothing(ff9: Rom, term: str):
|
|
assert _search_ids(term) == []
|
|
|
|
|
|
def test_updating_the_metadata_refreshes_the_aliases(ff9: Rom):
|
|
db_rom_handler.update_rom(
|
|
ff9.id, {"igdb_metadata": {"alternative_names": ["Final Fantasy Nine"]}}
|
|
)
|
|
|
|
assert _search_ids("nine") == [ff9.id]
|
|
assert _search_ids("ff9") == []
|
|
|
|
|
|
def test_the_mariadb_search_matches_the_alias_column_in_one_fulltext_index():
|
|
query = db_rom_handler._filter_by_search_term(select(Rom.id), "ff9")
|
|
|
|
assert (
|
|
"MATCH (roms.name, roms.fs_name, roms.generated_search_aliases)"
|
|
in compile_sql(query, MARIADB_DIALECT)
|
|
)
|
|
|
|
|
|
def test_the_postgresql_search_matches_the_alias_column():
|
|
query = db_rom_handler._filter_by_search_term(select(Rom.id), "ff9")
|
|
|
|
assert "roms.generated_search_aliases ILIKE" in compile_sql(
|
|
query, POSTGRESQL_DIALECT
|
|
)
|