|
| 1 | +"""Speed up gallery search and name ordering on the roms table |
| 2 | +
|
| 3 | +This migration adds the database structures the gallery relies on to search |
| 4 | +and sort large libraries without full table scans: |
| 5 | +
|
| 6 | +- Search indexes on roms.name and roms.fs_name, tailored per backend so the |
| 7 | + search query stays index-backed: a FULLTEXT index on MySQL/MariaDB, and |
| 8 | + pg_trgm GIN indexes on PostgreSQL (installing the pg_trgm extension first). |
| 9 | +- A plain idx_roms_name index on roms.name to accelerate name lookups and |
| 10 | + range scans. |
| 11 | +- A precomputed, indexed name_sort_key column for natural-sort ordering. |
| 12 | + Sorting by name previously ran a per-row regexp (strip leading articles, |
| 13 | + zero-pad numbers) that no index could cover, forcing a full sort on every |
| 14 | + page. The key is now stored on write and backfilled here, so ordering by |
| 15 | + name — including deep-offset pages — uses idx_roms_name_sort_key. |
| 16 | +
|
| 17 | +downgrade() drops every object created here in reverse order, leaving the |
| 18 | +pg_trgm extension in place since other objects may depend on it. |
| 19 | +
|
| 20 | +Revision ID: 0084_add_roms_search_index |
| 21 | +Revises: 0083_rom_category_soundtrack |
| 22 | +Create Date: 2026-06-16 00:00:00.000000 |
| 23 | +
|
| 24 | +""" |
| 25 | + |
| 26 | +import sqlalchemy as sa |
| 27 | +from alembic import op |
| 28 | + |
| 29 | +from models.rom import NAME_SORT_KEY_MAX_LENGTH, compute_name_sort_key |
| 30 | +from utils.database import is_mariadb, is_mysql, is_postgresql |
| 31 | + |
| 32 | +# revision identifiers, used by Alembic. |
| 33 | +revision = "0084_add_roms_search_index" |
| 34 | +down_revision = "0083_rom_category_soundtrack" |
| 35 | +branch_labels = None |
| 36 | +depends_on = None |
| 37 | + |
| 38 | +FULLTEXT_INDEX_NAME = "idx_roms_name_fs_name_fulltext" |
| 39 | +PG_NAME_INDEX = "idx_roms_name_trgm" |
| 40 | +PG_FS_NAME_INDEX = "idx_roms_fs_name_trgm" |
| 41 | + |
| 42 | +_BACKFILL_BATCH = 1000 |
| 43 | + |
| 44 | + |
| 45 | +def upgrade() -> None: |
| 46 | + bind = op.get_bind() |
| 47 | + |
| 48 | + # 1. DB-specific search indexes on roms.name and roms.fs_name. |
| 49 | + if is_mysql(bind) or is_mariadb(bind): |
| 50 | + op.execute( |
| 51 | + sa.text( |
| 52 | + f"CREATE FULLTEXT INDEX {FULLTEXT_INDEX_NAME} " |
| 53 | + "ON roms (name, fs_name)" |
| 54 | + ) |
| 55 | + ) |
| 56 | + elif is_postgresql(bind): |
| 57 | + # pg_trgm is a trusted extension since PostgreSQL 13, so a non-superuser |
| 58 | + # with CREATE on the database can install it. |
| 59 | + op.execute(sa.text("CREATE EXTENSION IF NOT EXISTS pg_trgm")) |
| 60 | + op.execute( |
| 61 | + sa.text( |
| 62 | + f"CREATE INDEX IF NOT EXISTS {PG_NAME_INDEX} " |
| 63 | + "ON roms USING gin (name gin_trgm_ops)" |
| 64 | + ) |
| 65 | + ) |
| 66 | + op.execute( |
| 67 | + sa.text( |
| 68 | + f"CREATE INDEX IF NOT EXISTS {PG_FS_NAME_INDEX} " |
| 69 | + "ON roms USING gin (fs_name gin_trgm_ops)" |
| 70 | + ) |
| 71 | + ) |
| 72 | + |
| 73 | + # 2. Plain index on roms.name. |
| 74 | + with op.batch_alter_table("roms", schema=None) as batch_op: |
| 75 | + batch_op.create_index( |
| 76 | + "idx_roms_name", |
| 77 | + ["name"], |
| 78 | + unique=False, |
| 79 | + if_not_exists=True, |
| 80 | + ) |
| 81 | + |
| 82 | + # 3. Precomputed name_sort_key column for natural-sort ordering. |
| 83 | + op.add_column( |
| 84 | + "roms", |
| 85 | + sa.Column( |
| 86 | + "name_sort_key", |
| 87 | + sa.String(length=NAME_SORT_KEY_MAX_LENGTH), |
| 88 | + nullable=True, |
| 89 | + ), |
| 90 | + if_not_exists=True, |
| 91 | + ) |
| 92 | + |
| 93 | + roms = sa.table( |
| 94 | + "roms", |
| 95 | + sa.column("id", sa.Integer), |
| 96 | + sa.column("name", sa.String), |
| 97 | + sa.column("name_sort_key", sa.String), |
| 98 | + ) |
| 99 | + |
| 100 | + result = bind.execute(sa.select(roms.c.id, roms.c.name)) |
| 101 | + update_stmt = ( |
| 102 | + roms.update() |
| 103 | + .where(roms.c.id == sa.bindparam("_id")) |
| 104 | + .values(name_sort_key=sa.bindparam("_key")) |
| 105 | + ) |
| 106 | + while True: |
| 107 | + batch = result.fetchmany(_BACKFILL_BATCH) |
| 108 | + if not batch: |
| 109 | + break |
| 110 | + bind.execute( |
| 111 | + update_stmt, |
| 112 | + [{"_id": row.id, "_key": compute_name_sort_key(row.name)} for row in batch], |
| 113 | + ) |
| 114 | + |
| 115 | + op.create_index( |
| 116 | + "idx_roms_name_sort_key", "roms", ["name_sort_key"], if_not_exists=True |
| 117 | + ) |
| 118 | + |
| 119 | + |
| 120 | +def downgrade() -> None: |
| 121 | + bind = op.get_bind() |
| 122 | + |
| 123 | + # 3. name_sort_key column and its index. |
| 124 | + op.drop_index("idx_roms_name_sort_key", table_name="roms", if_exists=True) |
| 125 | + op.drop_column("roms", "name_sort_key", if_exists=True) |
| 126 | + |
| 127 | + # 2. Plain index on roms.name. |
| 128 | + with op.batch_alter_table("roms", schema=None) as batch_op: |
| 129 | + batch_op.drop_index("idx_roms_name", if_exists=True) |
| 130 | + |
| 131 | + # 1. DB-specific search indexes. |
| 132 | + if is_mysql(bind) or is_mariadb(bind): |
| 133 | + op.execute(sa.text(f"DROP INDEX {FULLTEXT_INDEX_NAME} ON roms")) |
| 134 | + elif is_postgresql(bind): |
| 135 | + # Leave the pg_trgm extension in place; other objects may depend on it. |
| 136 | + op.execute(sa.text(f"DROP INDEX IF EXISTS {PG_FS_NAME_INDEX}")) |
| 137 | + op.execute(sa.text(f"DROP INDEX IF EXISTS {PG_NAME_INDEX}")) |
0 commit comments