mirror of
https://github.com/rommapp/romm.git
synced 2026-12-03 10:33:22 +01:00
* Search on search_titles and drop generated_search_aliases The gallery filter matched provider aliases through a generated column built from the JSON text, so escaped characters (MariaDB's \uXXXX storage, tabs, quotes) were out of its reach, and ranking already reads the folded titles in `search_titles`. Point the MariaDB FULLTEXT index and the PostgreSQL trigram index at `search_titles` and drop the generated column. Migration 0147, not yet released, makes the swap; its downgrade rebuilds 0146's column and index from a frozen copy of their SQL. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_015iCApu4gYrGBWDX8JD9JH6 * Rebuild roms after the search titles backfill on MariaDB Filling `search_titles` grows every row in place, which leaves InnoDB's pages split and slowed the FULLTEXT search by 10 to 25 ms on 40,000 ROMs. One table rebuild after a backfill that filled rows restores it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_015iCApu4gYrGBWDX8JD9JH6 * pr-ready code review: describe the alias search test case as it stands Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_015iCApu4gYrGBWDX8JD9JH6 * pr-ready simplify: read the legacy column name off the 0147 migration Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_015iCApu4gYrGBWDX8JD9JH6 * pr-ready review-polish: describe the rebuild as the code does it Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_015iCApu4gYrGBWDX8JD9JH6 * Freeze 0147's provider list and always rebuild roms on MariaDB - The backfill and the downgrade's copy of 0146's column read a frozen provider list instead of `ALTERNATIVE_NAME_SOURCES`, so a later provider can't change what this revision selects or rebuilds. - The table rebuild runs on every MariaDB/MySQL upgrade: a run that stopped after the fill but before the rebuild left nothing to fill on retry. - Two comments describe why, not what the migration replaces. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_015iCApu4gYrGBWDX8JD9JH6 * Fold 0146 and 0147 into one search titles revision Neither revision has shipped, so `0146_roms_search_titles` takes the roms table from 0145 straight to `search_titles` and its search index, and `generated_search_aliases` never exists. The search indexes are dropped first, so the column is added and filled with no index to maintain, and the index is built once over the filled rows. Databases on a nightly that ran either old revision need resetting to 0145 before upgrading. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_015iCApu4gYrGBWDX8JD9JH6 * Keep the search titles migration under 0146's revision ID The folded revision now rewrites 0146_roms_search_aliases in place rather than adding a new ID. A nightly database that already ran the old 0146 or 0147 still needs resetting to 0145 before upgrading. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_015iCApu4gYrGBWDX8JD9JH6 * Say why 0146 adds search_titles ahead of ensure_roms_columns Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_015iCApu4gYrGBWDX8JD9JH6 --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
168 lines
4.8 KiB
Python
168 lines
4.8 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_titles(rom: Rom) -> str | None:
|
|
with session_factory() as session:
|
|
return session.scalar(select(Rom.search_titles).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_stores_only_its_name(platform: Platform):
|
|
rom = _add_rom(
|
|
platform,
|
|
"Chrono Trigger",
|
|
igdb_metadata={"alternative_names": []},
|
|
moby_metadata={"alternate_titles": "not a list"},
|
|
)
|
|
|
|
assert _stored_titles(rom) == "\x1fchrono trigger\x1f"
|
|
|
|
|
|
def test_the_titles_are_stored_folded(platform: Platform):
|
|
rom = _add_rom(
|
|
platform,
|
|
"Final Fantasy IX",
|
|
igdb_metadata={"alternative_names": ["FF9", "Final Fantasy 9"]},
|
|
ss_metadata={"alternative_names": ["FFIX"]},
|
|
)
|
|
|
|
assert _stored_titles(rom) == (
|
|
"\x1ffinal fantasy ix\x1fff9\x1ffinal fantasy 9\x1fffix\x1f"
|
|
)
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("alias", "term"),
|
|
[
|
|
# JSON stores these characters escaped, which the titles must not keep.
|
|
("ŌKAMI Den", "ōkami den"),
|
|
("Chrono\tTrigger", "chrono trigger"),
|
|
('Say "Cheese"', 'say "cheese"'),
|
|
],
|
|
)
|
|
def test_an_alias_with_an_escaped_character_finds_the_rom(
|
|
platform: Platform, unrelated: Rom, alias: str, term: str
|
|
):
|
|
rom = _add_rom(platform, "Name", igdb_metadata={"alternative_names": [alias]})
|
|
|
|
assert _search_ids(term) == [rom.id]
|
|
|
|
|
|
@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_titles_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.search_titles)" in compile_sql(
|
|
query, MARIADB_DIALECT
|
|
)
|
|
|
|
|
|
def test_the_postgresql_search_matches_the_titles_column():
|
|
query = db_rom_handler._filter_by_search_term(select(Rom.id), "ff9")
|
|
|
|
assert "roms.search_titles ILIKE" in compile_sql(query, POSTGRESQL_DIALECT)
|