Files
romm/backend/tests/test_migrations.py
Sam Dornan abd061f59a perf(backend): read the gallery sorts out of an index (#4647)
* perf(backend): read the nullable gallery sorts out of an index

Sorting the gallery by release date, rating or HowLongToBeat time placed
unset roms last with `ORDER BY <column> IS NULL, <column>`. The leading
term is an expression, so MariaDB and MySQL could not read the order out
of `idx_roms_<column>` and filesorted instead. PostgreSQL had the mirror
problem descending: `DESC NULLS LAST` does not match the index's own
order, so it fell back to a sequential scan.

Neither engine paid this once. v2 window fetches send no sidecars, so
each scroll window is its own `ORDER BY ... LIMIT 72 OFFSET n` over the
full filtered set, and `roms` carries a JSON metadata blob per provider
on every row.

Each column now carries a STORED `_unset` flag with an
`idx_roms_<column>_sort` pair the ascending sort leads with, and
PostgreSQL gets an `idx_roms_<column>_desc` for the descending one. Row
order is unchanged in both directions.

Measured on PostgreSQL 16 over 120k roms, first page:

  ascending    30.6 ms / 8109 buffers -> 0.074 ms / 13 buffers
  descending   33.5 ms / 8091 buffers -> 0.112 ms / 14 buffers

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NouZquWDxNBqxYyu2E2GF8

* fix(backend): rebuild a sort index MariaDB narrowed instead of dropped

Testing the previous commit against MariaDB rather than PostgreSQL alone
turned up a real bug in it.

The two engines leave a dropped generated column in different states:
PostgreSQL drops every index that reads it, while MariaDB and MySQL keep
a composite one over the columns that survive. `_restore_generated_indexes`
only created indexes missing by name, so on MariaDB the rebuild path in
`ensure_roms_columns` (a chain predating 0123) would leave
`idx_roms_<column>_sort` narrowed to the unset flag alone and the
ascending gallery sort walking half an index.

It now reconciles on the columns an index spans, not just its name, and
`_drop_index_sql` gives the two dialects their own DROP INDEX spelling.

Also corrects the flag columns' comments: MariaDB accepts no NOT NULL on
a generated column in either position, so they are nullable in the DDL on
both engines. `IS NULL` never evaluates to NULL, so no row carries one.

Verified on MariaDB 10.11 over 120k roms. The ascending page went from
`rows=116503, Using index; Using filesort` to `rows=72, Using index`,
134 ms to 6 ms, with the same plan once the rom_user join is added.
Migration, schema and flag values also checked under utf8mb3_unicode_ci,
the collation these expressions are most fragile under.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NouZquWDxNBqxYyu2E2GF8

* style(backend): give the sort-flag rationale one home

Greptile flagged the comment blocks in this PR against CLAUDE.md's
comment discipline. The length was a symptom: the same explanation of
why MariaDB cannot index the NULLS LAST emulation appeared in four
places. It now lives once, beside `SORTABLE_NULLABLE_ROM_COLUMNS`, and
the other sites say only what is local to them.

No behaviour change; the migration docstrings are left as they are,
matching the narrative style every recent revision uses.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NouZquWDxNBqxYyu2E2GF8

* fix(backend): drop the sort indexes with the rest of the catalog

Copilot pointed out that `ensure_roms_columns` installs the `_unset`
columns long before 0129 runs, so a chain that stops short and then
downgrades leaves them behind. Checking that turned up a real leak next
to it: 0108's downgrade, which the module documents as removing whatever
of the catalog such a chain still carries, was leaving two indexes.

A descending sort index reads a value column this module inherited
rather than added, so `drop_roms_columns` re-adds that column and the
index over it survives. `_drop_sort_indexes` now takes them explicitly.
PostgreSQL only, since no other engine has those indexes.

The flag columns themselves are not a leak: the catalog front-loads every
column it owns, so at 0127 the table already carries 0123's
`generated_keywords`, `generated_themes` and `generated_rating_count`
too. Verified on both engines that a 0128 to 0127 downgrade leaves the
table writable with the flags still computing, and that re-upgrading
restores the pair, narrowed index included.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NouZquWDxNBqxYyu2E2GF8

* style(backend): tighten the teardown docstrings and the test annotation

Two more from Greptile, both worth taking even though the reasoning
behind each was off.

`_drop_sort_indexes` and its test now carry a summary plus one line,
matching `drop_roms_columns` right above them, and the parametrized
metadata argument is typed instead of a bare `dict`.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NouZquWDxNBqxYyu2E2GF8

* perf(backend): index the remaining gallery sorts (#4644)

* perf(backend): span the gallery sort indexes through the id tiebreak

The gallery appends `id` to every ORDER BY, but `idx_roms_<column>_sort`
stopped at the value and `idx_roms_<column>_desc` at the value alone. On
PostgreSQL that leaves every tie to be sorted, and unset metadata is a single
tie covering the whole bucket: a scroll window inside it fell back to a Seq
Scan and an external merge sort. Both indexes now carry `id` in the sort's own
direction, which InnoDB was already appending for MariaDB and MySQL.

Measured on 120k roms, a window at offset 100000 (inside the unset bucket),
all three columns, both directions:

  PostgreSQL ascending   119 ms, external merge sort  ->  15 ms, index only
  PostgreSQL descending   92 ms, incremental sort     ->  13 ms, index only
  MariaDB, both          already index-only; unchanged

No sort node remains in any of the twelve plans. Also trims the docstrings and
comments this change touches to the repo's one-or-two-line rule.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NouZquWDxNBqxYyu2E2GF8

* test(backend): pin the reflected order of the descending sort indexes

The existing assertion read column names only, which a default ascending index
satisfies just as well, and `AUTOGENERATE_EXEMPT_INDEX_NAMES` keeps the drift
check away from these. Asserts the reflected `column_sorting` instead, and its
absence on the ascending pair.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NouZquWDxNBqxYyu2E2GF8

* fix(backend): declare the unset flags nullable, as their DDL is

`Mapped[bool]` inferred NOT NULL while both engines create the column nullable,
MariaDB because it takes no NOT NULL on a generated column. The four generated
columns beside them already match their DDL. The annotation stays `bool`, since
`IS NULL` never yields NULL.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NouZquWDxNBqxYyu2E2GF8

* fix(backend): renumber the gallery sort revisions after 0132

master gained its own 0129-0132 while this branch was open, and both
chains started from 0128_hltb_main_story_column, leaving alembic with
two heads (`upgrade head` fails with MultipleHeads).

0129_indexed_gallery_sorts -> 0133, now revising 0132_audit_events;
0130_gallery_sort_indexes -> 0134. No content change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VgcWNtJy6ucWRaibqNKNJC

* fix(backend): re-chain the renamed gallery sort revisions onto 0132

The previous commit renamed the files but left their revision IDs and
parent as they were. 0133 now revises 0132_audit_events, and both IDs
match their filenames.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VgcWNtJy6ucWRaibqNKNJC

---------

Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
2026-09-24 14:56:43 -05:00

601 lines
22 KiB
Python

"""Guard the models against drifting away from the migrated schema.
The test database is built from the migrations, so an index declared in one but
not the other goes unnoticed until autogenerate proposes dropping it.
"""
import importlib.util
import re
from pathlib import Path
from types import ModuleType
from typing import Any
import alembic.config
import pytest
import sqlalchemy as sa
from alembic.autogenerate import compare_metadata
from alembic.migration import MigrationContext
from alembic.operations import Operations
from sqlalchemy import DefaultClause, FetchedValue, Table, UniqueConstraint
from sqlalchemy.sql.schema import NULL_UNSPECIFIED
import models
from handler.database.base_handler import sync_engine
from models.base import BaseModel
from models.rom import FULL_PATH_HASH_LENGTH, Rom, compute_full_path_hash
from utils.database import (
AUTOGENERATE_EXEMPT_INDEX_NAMES,
POSTGRESQL_FK_INDEXES,
SORTABLE_NULLABLE_ROM_COLUMNS,
full_path_digest_sql,
has_column,
is_mariadb,
is_postgresql,
rom_desc_index_name,
rom_sort_index_name,
rom_unset_flag_column,
)
from utils.roms_columns import (
FULL_PATH_HASH_COLUMN,
HLTB_MAIN_STORY_COLUMN,
ROMS_METADATA_VIEW_COLUMNS,
STEAM_FED_COLUMNS,
STEAM_METADATA_COLUMN,
drop_roms_columns,
ensure_roms_columns,
has_server_default,
rebuild_generated_columns,
steam_fed_columns,
)
# `compare_metadata` yields flat tuples for schema-level diffs, and a list of
# tuples for column-level ones. Only these two name an index.
INDEX_DIFF_OPS = frozenset({"add_index", "remove_index"})
def _leading_columns(table: Table) -> set[str]:
"""Names of the columns a lookup on this table can start from."""
groups = [list(table.primary_key.columns)]
groups += [list(index.columns) for index in table.indexes]
groups += [
list(constraint.columns)
for constraint in table.constraints
if isinstance(constraint, UniqueConstraint)
]
return {columns[0].name for columns in groups if columns}
def test_no_index_drift_between_models_and_migrations():
"""Every migrated index is declared on its model, and vice versa.
A failure names the index: declare it in the model's `__table_args__`, or in
`AUTOGENERATE_EXEMPT_INDEX_NAMES` if it is dialect-specific.
"""
models.load_all_models()
with sync_engine.connect() as connection:
context = MigrationContext.configure(connection)
drift = [
f"{diff[0]}: {diff[1].name} on {diff[1].table.name}"
for diff in compare_metadata(context, BaseModel.metadata)
if not isinstance(diff, list)
and diff[0] in INDEX_DIFF_OPS
and diff[1].name not in AUTOGENERATE_EXEMPT_INDEX_NAMES
]
assert drift == []
def test_no_column_drift_between_models_and_migrations():
"""Every mapped column exists in the migrated schema with the same nullability."""
models.load_all_models()
with sync_engine.connect() as connection:
inspector = sa.inspect(connection)
# A view reflects every column as nullable, whatever the model says.
views = set(inspector.get_view_names())
drift = []
for table in BaseModel.metadata.sorted_tables:
if table.name in views:
continue
reflected = {
column["name"]: column["nullable"]
for column in inspector.get_columns(table.name)
}
for column in table.columns:
if column.name not in reflected:
drift.append(f"missing: {table.name}.{column.name}")
elif column.nullable != reflected[column.name]:
drift.append(
f"nullable: {table.name}.{column.name} "
f"model={column.nullable} db={reflected[column.name]}"
)
assert drift == []
def _is_database_filled(value: object) -> bool:
# `DefaultClause` is a literal default the ORM could write itself.
return isinstance(value, FetchedValue) and not isinstance(value, DefaultClause)
def test_database_filled_columns_declare_their_nullability():
"""Every database-filled column passes `nullable=`, or autogenerate never compares it."""
models.load_all_models()
undeclared = [
f"{table.name}.{column.name}"
for table in BaseModel.metadata.sorted_tables
for column in table.columns
if any(
_is_database_filled(value)
for value in (column.server_default, column.server_onupdate)
)
# The same private flag alembic's `_nullability_might_be_unset` reads.
and column._user_defined_nullable is NULL_UNSPECIFIED
]
assert undeclared == []
def test_postgresql_fk_indexes_cover_every_unindexed_foreign_key():
"""`POSTGRESQL_FK_INDEXES` holds exactly the foreign keys that need it.
MariaDB and MySQL index a foreign key implicitly unless its column already
leads an index; PostgreSQL does not.
"""
models.load_all_models()
expected = {
(table.name, f"ix_{table.name}_{column}", column)
for table in BaseModel.metadata.tables.values()
for constraint in table.foreign_key_constraints
for column in [c.name for c in constraint.columns]
if len(constraint.columns) == 1 and column not in _leading_columns(table)
}
assert expected == set(POSTGRESQL_FK_INDEXES)
@pytest.mark.parametrize(
"fs_path,fs_name",
[
("roms/nes", "Game (USA).zip"),
("roms/nes/Hacks & Tra'nslations", "Zelda [T-Eng].nes"),
("roms/nes/Ünïcøde", "Pokémon Édition Rouge.gb"),
("", ""),
(None, None),
],
)
def test_the_migrated_full_path_digest_matches_the_models(
fs_path: str | None, fs_name: str | None
):
"""0126 backfills `full_path_hash` in SQL; the app writes it from Python.
A mismatch would make every pre-existing rom look new to the unique index.
"""
with sync_engine.connect() as connection:
digest = connection.execute(
sa.text(
f"SELECT {full_path_digest_sql(connection)} FROM "
"(SELECT :fs_path AS fs_path, :fs_name AS fs_name) AS one_rom"
),
{"fs_path": fs_path, "fs_name": fs_name},
).scalar_one()
assert digest == compute_full_path_hash(fs_path, fs_name)
def _load_migration(filename: str) -> ModuleType:
"""Import a revision by file name, which no package path can reach."""
path = Path(__file__).parent.parent / "alembic" / "versions" / filename
spec = importlib.util.spec_from_file_location(path.stem, path)
assert spec and spec.loader
migration = importlib.util.module_from_spec(spec)
spec.loader.exec_module(migration)
return migration
# Columns by nullability and indexes by (columns, uniqueness).
TableSchema = tuple[
dict[str, bool], dict[str | None, tuple[tuple[str | None, ...], bool]]
]
def _schema_of(connection: sa.Connection, table: str) -> TableSchema:
inspector = sa.inspect(connection)
return (
{column["name"]: column["nullable"] for column in inspector.get_columns(table)},
{
index["name"]: (tuple(index["column_names"]), bool(index["unique"]))
for index in inspector.get_indexes(table)
},
)
def _replay(connection: sa.Connection, filename: str) -> None:
with Operations.context(MigrationContext.configure(connection)):
_load_migration(filename).upgrade()
# None of these is touched by a later revision, so replaying one over the
# migrated schema has to leave that schema exactly as it found it.
@pytest.mark.parametrize(
"filename,table",
[
("0111_physical_roms.py", "roms"),
("0120_container_adoptions.py", "streaming_container_adoptions"),
("0122_rom_similarity.py", "rom_similarity"),
("0123_recommendation_metadata.py", "roms"),
("0126_unique_rom_full_path.py", "roms"),
("0128_hltb_main_story_column.py", "roms"),
("0130_notifications.py", "notifications"),
("0131_notification_channels.py", "notification_channels"),
("0132_audit_events.py", "audit_events"),
],
)
def test_a_revision_replayed_over_the_migrated_schema_is_a_no_op(
filename: str, table: str
):
with sync_engine.begin() as connection:
before = _schema_of(connection, table)
_replay(connection, filename)
assert _schema_of(connection, table) == before
def test_the_rom_similarity_revision_fills_in_a_missing_index():
"""0122 meets its own table on a replay, with only some of its indexes.
Alembic issues those as their own statements after the table, so a run can
die between them.
"""
migration = _load_migration("0122_rom_similarity.py")
name, _ = migration.INDEXES[0]
with sync_engine.begin() as connection:
before = _schema_of(connection, migration.TABLE)
with Operations.context(MigrationContext.configure(connection)) as operations:
# The other one backs a foreign key, which MariaDB will not let go.
operations.drop_index(name, table_name=migration.TABLE)
migration.upgrade()
assert _schema_of(connection, migration.TABLE) == before
def test_the_memory_card_revision_replays_under_the_one_that_prunes_it():
"""0119 re-creates the index 0125 drops, so the pair has to replay together.
A real upgrade always runs 0125 after 0119; this pins that the second pass
still lands on the same schema.
"""
with sync_engine.begin() as connection:
before = _schema_of(connection, "memory_card_versions")
_replay(connection, "0119_memory_cards.py")
_replay(connection, "0125_drop_redundant_indexes.py")
assert _schema_of(connection, "memory_card_versions") == before
def test_the_full_path_hash_migration_resumes_an_interrupted_run(rom: Rom):
"""0126 finishes a run that died right after its ADD COLUMN.
That run leaves the column with no digest, no NOT NULL and neither index.
"""
migration = _load_migration("0126_unique_rom_full_path.py")
with sync_engine.begin() as connection:
with Operations.context(MigrationContext.configure(connection)) as operations:
migration.downgrade()
operations.add_column(
"roms",
sa.Column(
migration.COLUMN_NAME, sa.String(length=FULL_PATH_HASH_LENGTH)
),
)
migration.upgrade()
columns, indexes = _schema_of(connection, "roms")
digest = connection.execute(
sa.text(
f"SELECT {migration.COLUMN_NAME} FROM roms WHERE id = :rom_id"
), # nosec B608
{"rom_id": rom.id},
).scalar_one()
assert not columns[migration.COLUMN_NAME]
assert indexes[migration.UNIQUE_INDEX_NAME][1]
assert not indexes[migration.LOOKUP_INDEX_NAME][1]
assert digest == compute_full_path_hash(rom.fs_path, rom.fs_name)
def test_the_hltb_migration_resumes_an_interrupted_run():
"""0128 keeps the generated column it already added and builds its index."""
migration = _load_migration("0128_hltb_main_story_column.py")
with sync_engine.begin() as connection:
with Operations.context(MigrationContext.configure(connection)) as operations:
operations.drop_index(migration.INDEX_NAME, table_name="roms")
migration.upgrade()
_, indexes = _schema_of(connection, "roms")
assert migration.INDEX_NAME in indexes
def test_has_column_reflects_the_migrated_schema():
"""The guard every replayed column add is skipped by."""
with sync_engine.connect() as connection:
assert has_column(connection, "roms", "generated_publishers")
assert not has_column(connection, "roms", "no_such_column")
def _roms_alters(connection: sa.Connection) -> list[str]:
"""Record every ALTER TABLE roms the connection issues from here on."""
statements: list[str] = []
@sa.event.listens_for(connection, "before_cursor_execute")
def _record(_conn: Any, _cursor: Any, statement: str, *_args: Any) -> None:
if re.match(r"ALTER TABLE roms\b", statement.lstrip(), re.IGNORECASE):
statements.append(statement)
return statements
def _generation_expressions(connection: sa.Connection) -> dict[str, str]:
"""The expression behind every generated column on `roms`."""
return {
column["name"]: column["computed"]["sqltext"]
for column in sa.inspect(connection).get_columns("roms")
if column.get("computed")
}
def test_the_roms_columns_helper_leaves_a_migrated_table_alone():
"""Every revision calls it, so all but the first must cost nothing."""
with sync_engine.begin() as connection:
before = _schema_of(connection, "roms")
alters = _roms_alters(connection)
ensure_roms_columns(connection)
assert _schema_of(connection, "roms") == before
assert alters == []
def test_the_roms_columns_helper_adds_every_missing_column_at_once():
"""A stored and a generated column missing together cost one table copy."""
hltb = _load_migration("0128_hltb_main_story_column.py")
with sync_engine.begin() as connection:
before = _schema_of(connection, "roms")
with Operations.context(MigrationContext.configure(connection)) as operations:
operations.drop_index(hltb.INDEX_NAME, table_name="roms")
operations.drop_column("roms", "upc")
operations.drop_column("roms", HLTB_MAIN_STORY_COLUMN)
alters = _roms_alters(connection)
ensure_roms_columns(connection)
# 0128 owns the index, so its replay finishes the schema.
_replay(connection, "0128_hltb_main_story_column.py")
assert _schema_of(connection, "roms") == before
assert len(alters) == 1
def test_the_roms_columns_helper_rebuilds_a_narrowed_sort_index():
"""PostgreSQL drops a `_sort` index with its column; MariaDB narrows it."""
# `roms_metadata` does not project this one, so PostgreSQL lets it go
# without the view being dropped first.
column = HLTB_MAIN_STORY_COLUMN
spanned = (rom_unset_flag_column(column), column, "id")
with sync_engine.begin() as connection:
connection.execute(sa.text(f"ALTER TABLE roms DROP COLUMN {column}"))
assert _schema_of(connection, "roms")[1].get(rom_sort_index_name(column)) != (
spanned,
False,
)
ensure_roms_columns(connection)
# 0128 owns the value column's own index, so its replay finishes the schema.
_replay(connection, "0128_hltb_main_story_column.py")
assert _schema_of(connection, "roms")[1][rom_sort_index_name(column)] == (
spanned,
False,
)
def test_dropping_the_roms_columns_takes_the_sort_indexes_with_them():
"""A descending sort index reads an inherited column, so nothing else drops it."""
# Those indexes are PostgreSQL's alone, and only its DDL rolls back, which
# is what keeps this teardown out of the schema the other tests share.
with sync_engine.connect() as connection:
if not is_postgresql(connection):
pytest.skip("descending sort indexes are PostgreSQL-only")
transaction = connection.begin()
try:
descending = {
rom_desc_index_name(column) for column in SORTABLE_NULLABLE_ROM_COLUMNS
}
assert descending <= set(_schema_of(connection, "roms")[1])
drop_roms_columns(connection)
assert descending & set(_schema_of(connection, "roms")[1]) == set()
finally:
transaction.rollback()
def test_the_roms_columns_helper_redefines_a_column_that_predates_steam():
"""0123 taught the metadata chains to read Steam; an older table is rebuilt."""
with sync_engine.begin() as connection:
pg = is_postgresql(connection)
before = _schema_of(connection, "roms")
genres = next(
column
for column in steam_fed_columns(pg, with_steam=False)
if column.name == "generated_genres"
)
rebuild_generated_columns(
connection,
add=[genres],
drop=[],
view_columns=ROMS_METADATA_VIEW_COLUMNS,
)
assert (
STEAM_METADATA_COLUMN
not in _generation_expressions(connection)["generated_genres"]
)
alters = _roms_alters(connection)
ensure_roms_columns(connection)
assert _schema_of(connection, "roms") == before
assert len(alters) == 1
expressions = _generation_expressions(connection)
for column in STEAM_FED_COLUMNS:
assert STEAM_METADATA_COLUMN in expressions[column]
assert sa.inspect(connection).has_table("roms_metadata")
def _roms_schema(
connection: sa.Connection,
) -> tuple[TableSchema, dict[str, str], list[str]]:
"""`_schema_of` plus every generated expression and what the view projects."""
view = sa.inspect(connection).get_columns("roms_metadata")
return (
_schema_of(connection, "roms"),
_generation_expressions(connection),
[column["name"] for column in view],
)
def test_the_first_roms_columns_revision_downgrades_to_the_schema_it_found():
"""0108 adds every later revision's column, so alone it has to take them all back."""
migration = _load_migration("0126_unique_rom_full_path.py")
alembic.config.main(argv=["downgrade", "0107_roms_dedup_cover_index"])
try:
with sync_engine.connect() as connection:
before = _roms_schema(connection)
alembic.config.main(argv=["upgrade", "0108_roms_primary_region"])
# 0126 creates this before it is stamped, so a run that died right
# after leaves it for the downgrade to meet.
with sync_engine.begin() as connection:
with Operations.context(MigrationContext.configure(connection)) as ops:
ops.create_index(
migration.UNIQUE_INDEX_NAME,
"roms",
["platform_id", FULL_PATH_HASH_COLUMN],
unique=True,
)
alembic.config.main(argv=["downgrade", "0107_roms_dedup_cover_index"])
with sync_engine.connect() as connection:
after = _roms_schema(connection)
finally:
alembic.config.main(argv=["upgrade", "head"])
assert after == before
def test_the_roms_columns_helper_puts_back_a_view_a_run_lost():
"""A run that died between the ALTER and the CREATE VIEW replays to the view."""
with sync_engine.begin() as connection:
connection.execute(sa.text("DROP VIEW roms_metadata"))
alters = _roms_alters(connection)
ensure_roms_columns(connection)
assert sa.inspect(connection).has_table("roms_metadata")
assert alters == []
def test_the_roms_columns_helper_drops_a_digest_default_a_run_left_behind():
"""MariaDB fills the digest through a DEFAULT the run removes right after."""
with sync_engine.begin() as connection:
if not is_mariadb(connection):
pytest.skip("only MariaDB gives the column a default")
connection.execute(
sa.text(
f"ALTER TABLE roms ALTER COLUMN {FULL_PATH_HASH_COLUMN} SET DEFAULT ''"
)
)
assert has_server_default(connection, FULL_PATH_HASH_COLUMN)
alters = _roms_alters(connection)
ensure_roms_columns(connection)
assert not has_server_default(connection, FULL_PATH_HASH_COLUMN)
assert len(alters) == 1
def test_the_roms_columns_helper_fills_the_full_path_digest_where_it_can(rom: Rom):
"""MariaDB gets the digest in the same copy; the others leave it to 0126."""
migration = _load_migration("0126_unique_rom_full_path.py")
with sync_engine.begin() as connection:
before = _schema_of(connection, "roms")
with Operations.context(MigrationContext.configure(connection)) as operations:
operations.drop_index(migration.UNIQUE_INDEX_NAME, table_name="roms")
operations.drop_column("roms", FULL_PATH_HASH_COLUMN)
ensure_roms_columns(connection)
columns, _ = _schema_of(connection, "roms")
assert columns[FULL_PATH_HASH_COLUMN] is not is_mariadb(connection)
assert not has_server_default(connection, FULL_PATH_HASH_COLUMN)
if is_mariadb(connection):
digest = connection.execute(
sa.text(
f"SELECT {FULL_PATH_HASH_COLUMN} FROM roms WHERE id = :rom_id" # nosec B608
),
{"rom_id": rom.id},
).scalar_one()
assert digest == compute_full_path_hash(rom.fs_path, rom.fs_name)
migration.upgrade()
assert _schema_of(connection, "roms") == before
@pytest.mark.parametrize(
"drop_column",
[False, True],
ids=["died before the index", "died before the column"],
)
def test_the_state_disc_file_migration_resumes_an_interrupted_run(drop_column: bool):
"""0121 completes whatever a run that died partway left behind.
Nothing else in the schema references these, so the interrupted states can
be built for real: the foreign key and index missing, or all three.
"""
migration = _load_migration("0121_state_disc_file.py")
with sync_engine.begin() as connection:
with Operations.context(MigrationContext.configure(connection)) as operations:
# The foreign key goes first: MariaDB refuses to drop a column it
# still needs, and an index it still sits on.
operations.drop_constraint(
"fk_states_disc_file_id", "states", type_="foreignkey"
)
operations.drop_index("ix_states_disc_file_id", table_name="states")
if drop_column:
operations.drop_column("states", "disc_file_id")
migration.upgrade()
inspector = sa.inspect(connection)
columns = {column["name"] for column in inspector.get_columns("states")}
index = inspector.has_index("states", "ix_states_disc_file_id")
foreign_keys = {key["name"] for key in inspector.get_foreign_keys("states")}
assert "disc_file_id" in columns
assert index
assert "fk_states_disc_file_id" in foreign_keys