Files
romm/backend/models/rom.py
Ryan Farthing c6b5f0ae11 fix(platforms): count only the roms the caller can see in rom_count and fs_size_bytes (#5313)
* fix(platforms): count only the roms the caller can see in rom_count and fs_size_bytes

Platform.rom_count and fs_size_bytes are column properties over every rom
row on the platform, and the platform endpoints only filter hidden
platforms. So the platform header's "In library" count and "On disk" size
included roms hidden from the caller (directly, or by an age limit), while
the gallery left them out. Server Stats was off the same way: its
per-platform metadata coverage divides /api/stats' visibility-filtered
match counts by the unfiltered rom_count, so a restricted user saw
coverage too low and platform counts and sizes too high.

For a restricted caller, the list, detail and update endpoints now subtract
the totals of the roms RomVisibilityFilter hides
(DBStatsHandler.get_hidden_rom_totals_by_platform, next to the other
per-platform aggregates). Direct hides are matched by primary key, so a
few hidden roms cost a few lookups rather than a pass over every rom on
the platform. Admins and other unrestricted callers keep the model's
values and skip the query.

On a generated library of 50 platforms and 100k roms (MariaDB), the
platform list takes ~44 ms; this adds ~4 ms with 100 roms hidden, ~44 ms
with 5,000, and ~60 ms with an age limit (min_age isn't indexed, so that
case scans the platforms' roms).

Fixes #5312

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(platforms): read the hidden roms' totals in the platform query

Addresses Greptile's review of #5313:

- Counts can become negative: the hidden totals came from a second query,
  so a rom added between the two could be subtracted without having been
  counted. They now come from a grouped subquery outer-joined into the
  statement that computes the full totals, so both read one snapshot (on
  PostgreSQL's READ COMMITTED too, which a shared transaction wouldn't
  give).
- Counting rules live in endpoints: the endpoint helper is gone.
  get_platform and get_platforms take the caller's RomVisibilityFilter
  (unrestricted by default) and return platforms with the visible totals;
  DBStatsHandler.get_hidden_rom_totals_by_platform is removed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(platforms): code-review fixes for visible platform totals

- Re-read rows with populate_existing so a shared session never subtracts twice
- Narrow the hidden-totals subquery to the one platform get_platform reads
- Re-read after update only for restricted callers, and 404 instead of leaking full totals
- Test the update endpoint's restricted totals and the shared-session read

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* refactor(platforms): simplify pass on visible platform totals

Gate the row clause in one condition with a why comment, fold the MariaDB
narrowing reason into the docstring, and drop restating comments.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(platforms): count only visible roms in /supported and RetroArch listings

- Move the hidden-ROM predicate into RomVisibilityFilter.hidden_on_visible_platforms_clause,
  with the age rule spelled as ranges an index on min_age can serve
- get_platforms reads hidden platforms from the visibility filter instead of a
  second hidden_platform_ids argument
- /api/platforms/supported lists a hidden platform as unmatched and counts only
  the caller's ROMs on the rest
- RetroArch WebDAV skips a platform whose ROMs are all hidden from the caller

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* perf(roms): index platform_id, min_age, fs_size_bytes for age-limited totals

On a 73k-ROM copy of a real library (MariaDB 13), GET /api/platforms for an
age-limited user went from 208 ms to 55 ms: the hidden-totals aggregate is now
an index-only scan instead of a full scan of the wide roms rows.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Georges-Antoine Assi <contact@me.gantoine.com>
2026-10-29 10:48:04 -04:00

1628 lines
60 KiB
Python

from __future__ import annotations
import enum
import hashlib
import re
import sys
from collections.abc import Iterable, Mapping, Sequence
from dataclasses import dataclass
from datetime import datetime
from functools import cached_property
from typing import TYPE_CHECKING, Any, Final, NamedTuple, TypedDict
from sqlalchemy import (
TIMESTAMP,
BigInteger,
Boolean,
Enum,
FetchedValue,
Float,
ForeignKey,
Index,
Integer,
SmallInteger,
String,
Text,
UniqueConstraint,
and_,
event,
func,
inspect,
or_,
select,
)
from sqlalchemy.orm import (
Mapped,
column_property,
declared_attr,
mapped_column,
relationship,
validates,
)
from sqlalchemy.orm.attributes import InstrumentedAttribute, set_committed_value
from sqlalchemy.sql.elements import ColumnElement
from config import FRONTEND_RESOURCES_PATH
from models.base import (
FILE_EXTENSION_MAX_LENGTH,
FILE_NAME_MAX_LENGTH,
FILE_PATH_MAX_LENGTH,
BaseModel,
compute_file_name_parts,
)
from utils import valid_youtube_id
from utils.age_ratings import MIN_AGE_SOURCE_COLUMNS, compute_min_age
from utils.database import CustomJSON, is_non_blank
# Max length of the precomputed natural-sort key column.
NAME_SORT_KEY_MAX_LENGTH = 500
# Length of the sha256 hex digest stored in `Rom.full_path_hash`.
FULL_PATH_HASH_LENGTH = 64
# Max length for free-text audio tag columns (title/artist/album).
AUDIO_TAG_MAX_LENGTH = 512
# Max length for the binary identity columns (title id and save target).
TITLE_ID_MAX_LENGTH = 100
# Every provider's metadata column; `manual_metadata` holds the user's own overrides.
PROVIDER_METADATA_COLUMNS: Final = (
"igdb_metadata",
"moby_metadata",
"ss_metadata",
"ra_metadata",
"launchbox_metadata",
"hasheous_metadata",
"flashpoint_metadata",
"hltb_metadata",
"demozoo_metadata",
"pouet_metadata",
"csdb_metadata",
"steam_metadata",
"gamelist_metadata",
)
# (metadata column, key) of each provider's alternative titles.
PROVIDER_ALTERNATIVE_NAME_SOURCES = (
("igdb_metadata", "alternative_names"),
("moby_metadata", "alternate_titles"),
("ss_metadata", "alternative_names"),
)
MANUAL_ALTERNATIVE_NAME_SOURCE = ("manual_metadata", "alternative_names")
ALTERNATIVE_NAME_SOURCES = (
MANUAL_ALTERNATIVE_NAME_SOURCE,
*PROVIDER_ALTERNATIVE_NAME_SOURCES,
)
# Limits on `RomUser.pinned_media`, a list of keys like `file:12` naming the
# media shown on the user's overview.
PINNED_MEDIA_MAX_ITEMS = 100
PINNED_MEDIA_KEY_MAX_LENGTH = 1024
PINNED_MEDIA_KEY_PATTERN = r"^(scraped|file|screenshot|artwork):\S"
# Articles ignored when sorting or bucketing a title, across the languages
# No-Intro and LaunchBox name games in. Both patterns built from this are
# anchored on the right, so "la" preceding "las" costs nothing.
ARTICLES = (
"the",
"a",
"an",
"le",
"la",
"les",
"el",
"los",
"las",
"il",
"lo",
"gli",
"der",
"die",
"das",
"het",
)
ARTICLE_PREFIX_RE = re.compile(rf"^({'|'.join(ARTICLES)})\s+")
DIGIT_RUN_RE = re.compile(r"\d+")
def compute_name_sort_key(name: str | None) -> str:
"""Precompute the natural-sort key stored in `Rom.name_sort_key`"""
value = (name or "").lower()
value = ARTICLE_PREFIX_RE.sub("", value).strip()
value = DIGIT_RUN_RE.sub(lambda m: m.group(0).zfill(12), value)
return value[:NAME_SORT_KEY_MAX_LENGTH]
def compute_full_path_hash(fs_path: str | None, fs_name: str | None) -> str:
"""Precompute the digest stored in `Rom.full_path_hash`"""
return hashlib.sha256(
f"{fs_path or ''}/{fs_name or ''}".encode(), usedforsecurity=False
).hexdigest()
# Closes every title in `Rom.search_titles`; a control character no title holds.
SEARCH_TITLE_SEPARATOR = "\x1f"
# In characters: a utf8mb4 TEXT holds 65535 bytes, and a longer value would
# fail the INSERT under strict mode.
SEARCH_TEXT_MAX_LENGTH = 16000
def fold_search_title(title: str) -> str:
"""`title` as search ranking compares it: lowercased, whitespace collapsed."""
return " ".join(title.replace(SEARCH_TITLE_SEPARATOR, " ").split()).lower()
def source_titles(blob: Any, key: str) -> list[str]:
"""The string titles under `key`, ignoring a malformed blob or entry."""
names = blob.get(key) if isinstance(blob, dict) else None
if not isinstance(names, list):
return []
return [title for title in names if isinstance(title, str)]
def resolve_alternative_names(metadata: Mapping[str, Any]) -> list[str]:
"""The hand-set titles, or every provider's when none are set, each trimmed and
kept once however it is cased or spaced.
Args:
metadata: Each `ALTERNATIVE_NAME_SOURCES` column's value, by column name.
"""
manual_column, manual_key = MANUAL_ALTERNATIVE_NAME_SOURCE
titles = source_titles(metadata.get(manual_column), manual_key) or [
title
for column, key in PROVIDER_ALTERNATIVE_NAME_SOURCES
for title in source_titles(metadata.get(column), key)
]
unique: dict[str, str] = {}
for title in titles:
unique.setdefault(fold_search_title(title), " ".join(title.split()))
return [title for folded, title in unique.items() if folded]
def compute_search_titles(name: str | None, metadata: Mapping[str, Any]) -> str:
"""Precompute `Rom.search_titles`: the folded name, then each folded alias.
Args:
metadata: Each `ALTERNATIVE_NAME_SOURCES` column's value, by column name.
"""
sep = SEARCH_TITLE_SEPARATOR
aliases = resolve_alternative_names(metadata)
folded_name = fold_search_title(name or "")
value = sep + folded_name + sep
for alias in dict.fromkeys(fold_search_title(title) for title in aliases):
if not alias or alias == folded_name:
continue
if len(value) + len(alias) + len(sep) > SEARCH_TEXT_MAX_LENGTH:
continue
value += alias + sep
return value
def _ra_achievement_sort_key(achievement: dict[str, Any]) -> tuple[int, int]:
"""Orders achievements by RetroAchievements' "Display Order", ties by id."""
order = achievement.get("display_order")
ra_id = achievement.get("ra_id")
return (
order if isinstance(order, int) else sys.maxsize,
ra_id if isinstance(ra_id, int) else sys.maxsize,
)
if TYPE_CHECKING:
from models.assets import Save, Screenshot, State
from models.collection import Collection
from models.platform import Platform
from models.recommendation import RomSimilarity
from models.user import User
class RomFileCategory(enum.StrEnum):
GAME = "game"
DLC = "dlc"
HACK = "hack"
MANUAL = "manual"
WALKTHROUGH = "walkthrough"
PATCH = "patch"
UPDATE = "update"
MOD = "mod"
DEMO = "demo"
TRANSLATION = "translation"
PROTOTYPE = "prototype"
CHEAT = "cheat"
SOUNDTRACK = "soundtrack"
SCREENSHOT = "screenshot"
class SaveTargetLayout(enum.StrEnum):
FOLDER_EXACT = "folder-exact"
FOLDER_PREFIX = "folder-prefix"
FILE_EXACT = "file-exact"
FILE_PREFIX = "file-prefix"
FOLDER_SPLIT = "folder-split"
@dataclass(frozen=True)
class RomIdentity:
"""A ROM's platform-native identity, as read from its binary.
One home for the triple that travels from the scan through to the `Rom`
columns of the same names.
"""
title_id: str | None = None
save_target: str | None = None
save_target_layout: SaveTargetLayout | None = None
@classmethod
def from_rom(cls, rom: "Rom") -> "RomIdentity":
"""The identity a ROM already carries."""
return cls(
title_id=rom.title_id,
save_target=rom.save_target,
save_target_layout=rom.save_target_layout,
)
def as_rom_attrs(self) -> dict[str, Any]:
"""The identity as `Rom` column values, for a scan's attribute merge."""
return {
"title_id": self.title_id,
"save_target": self.save_target,
"save_target_layout": self.save_target_layout,
}
# Document-category files (manuals, walkthroughs) share one substrate: a
# RomFile plus an optional RomFileDocMeta sidecar for provenance.
DOCUMENT_CATEGORIES = frozenset({RomFileCategory.MANUAL, RomFileCategory.WALKTHROUGH})
class DocSource(enum.StrEnum):
"""Where a document (manual/walkthrough) came from."""
UPLOAD = "upload" # User-uploaded file
GAMEFAQS = "gamefaqs" # Fetched from a GameFAQs guide URL
SCRAPER = "scraper" # Downloaded by a metadata provider
# Provider ids that name a game rather than a file, so two ROMs sharing any of
# them are one title (regions, revisions, storefront copies). Every reader of
# "the same game" matches on this one list: `sibling_roms`, the
# `group_by_meta_id` gallery window, and the recommendation feed's exclusions.
#
# A field's position is the `provider` code stored in `rom_identity_keys`, so
# the tuple is append-only, and a new provider needs a migration that backfills
# its rows.
IDENTITY_ID_FIELDS: Final[tuple[str, ...]] = (
"igdb_id",
"moby_id",
"ss_id",
"launchbox_id",
"ra_id",
"hasheous_id",
"tgdb_id",
"steam_id",
"flashpoint_id",
)
# Wide enough for the longest of those columns (`flashpoint_id`), since
# `rom_identity_keys` holds every provider's id in one column.
IDENTITY_PROVIDER_ID_LENGTH: Final = 100
# The database file at the root of every RPG Maker 2000/2003 game folder.
EASYRPG_GAME_DATABASE: Final = "rpg_rt.ldb"
class RomIdentityKey(BaseModel):
"""One row per (ROM, provider it has a match id for), scoped to a platform.
Two ROMs are the same game when they share a row's (provider, platform,
provider id), so `sibling_roms` is an indexed lookup over this table rather
than an OR of one equality per `IDENTITY_ID_FIELDS` entry across the whole
of `roms`.
Maintained by database triggers on `roms` (migration 0127), so no write path
has to update it; deletes ride the foreign key's cascade.
"""
__tablename__ = "rom_identity_keys"
__table_args__ = (Index("idx_rom_identity_keys_rom_id", "rom_id"),)
provider: Mapped[int] = mapped_column(SmallInteger, primary_key=True)
platform_id: Mapped[int] = mapped_column(Integer, primary_key=True)
provider_id: Mapped[str] = mapped_column(
String(length=IDENTITY_PROVIDER_ID_LENGTH), primary_key=True
)
rom_id: Mapped[int] = mapped_column(
ForeignKey("roms.id", ondelete="CASCADE"), primary_key=True
)
created_at: Mapped[datetime] = mapped_column(
TIMESTAMP(timezone=True), server_default=func.now()
)
updated_at: Mapped[datetime] = mapped_column(
TIMESTAMP(timezone=True), server_default=func.now()
)
class SiblingRom(BaseModel):
"""Other files of the same game on the same platform.
A database view, not a table, over `RomIdentityKey` self-joined on its
(provider, platform, provider id). A pair matched by several providers
appears once per provider, which `get_siblings_for_roms` and
`UniqueSiblingList` both collapse.
"""
__tablename__ = "sibling_roms"
rom_id: Mapped[int] = mapped_column(Integer, primary_key=True)
sibling_rom_id: Mapped[int] = mapped_column(Integer, primary_key=True)
class UniqueSiblingList(list["Rom"]):
"""`Rom.sibling_roms`, holding each sibling once though the view repeats it."""
def __init__(self, roms: Iterable[Rom] = ()) -> None:
super().__init__(roms)
self._held = {id(rom) for rom in self}
# selectinload hands the collection every view row, duplicates included.
def append(self, rom: Rom, /) -> None:
# A list method other than this one changed the contents.
if len(self._held) != len(self):
self._held = {id(held) for held in self}
if id(rom) not in self._held:
self._held.add(id(rom))
super().append(rom)
class RomArchiveMember(TypedDict):
name: str
size: int
crc_hash: str
md5_hash: str
sha1_hash: str
class LookupHashes(NamedTuple):
"""The hashes a ROM database should be queried with."""
crc: str | None
md5: str | None
sha1: str | None
class RomFile(BaseModel):
__tablename__ = "rom_files"
__table_args__ = (
Index("idx_rom_files_rom_id_category", "rom_id", "category"),
# Searching the gallery by a hash digest
Index("idx_rom_files_crc_hash", "crc_hash"),
Index("idx_rom_files_md5_hash", "md5_hash"),
Index("idx_rom_files_sha1_hash", "sha1_hash"),
Index("idx_rom_files_ra_hash", "ra_hash"),
Index("idx_rom_files_chd_sha1_hash", "chd_sha1_hash"),
)
id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
rom_id: Mapped[int] = mapped_column(ForeignKey("roms.id", ondelete="CASCADE"))
file_name: Mapped[str] = mapped_column(String(length=FILE_NAME_MAX_LENGTH))
file_path: Mapped[str] = mapped_column(String(length=FILE_PATH_MAX_LENGTH))
file_size_bytes: Mapped[int] = mapped_column(BigInteger(), default=0)
last_modified: Mapped[float | None] = mapped_column(default=None)
crc_hash: Mapped[str | None] = mapped_column(String(100))
md5_hash: Mapped[str | None] = mapped_column(String(100))
sha1_hash: Mapped[str | None] = mapped_column(String(100))
ra_hash: Mapped[str | None] = mapped_column(String(100))
chd_sha1_hash: Mapped[str | None] = mapped_column(String(100))
title_id: Mapped[str | None] = mapped_column(String(length=TITLE_ID_MAX_LENGTH))
# BigInteger because Switch title versions exceed int32
title_version: Mapped[int | None] = mapped_column(BigInteger, default=None)
# When rom-converto last recognized the file; NULL queues it on the next scan.
converto_read_at: Mapped[datetime | None] = mapped_column(
TIMESTAMP(timezone=True), default=None
)
archive_members: Mapped[list[RomArchiveMember] | None] = mapped_column(
CustomJSON(), default=None, nullable=True
)
category: Mapped[RomFileCategory | None] = mapped_column(
Enum(RomFileCategory), default=None
)
missing_from_fs: Mapped[bool] = mapped_column(default=False, nullable=False)
rom: Mapped[Rom] = relationship(back_populates="files")
track_meta: Mapped[TrackMeta | None] = relationship(
back_populates="rom_file",
uselist=False,
cascade="all, delete-orphan",
)
doc_meta: Mapped[RomFileDocMeta | None] = relationship(
back_populates="rom_file",
uselist=False,
cascade="all, delete-orphan",
)
user_states: Mapped[list[RomFileUser]] = relationship(
back_populates="rom_file",
cascade="all, delete-orphan",
lazy="raise",
)
@cached_property
def full_path(self) -> str:
return f"{self.file_path}/{self.file_name}"
@cached_property
def file_name_no_tags(self) -> str:
from handler.filesystem import fs_rom_handler
return fs_rom_handler.get_file_name_with_no_tags(self.file_name)
@cached_property
def file_name_no_ext(self) -> str:
from handler.filesystem import fs_rom_handler
return fs_rom_handler.get_file_name_with_no_extension(self.file_name)
@cached_property
def file_extension(self) -> str:
from handler.filesystem import fs_rom_handler
return fs_rom_handler.parse_file_extension(self.file_name)
@cached_property
def lookup_hashes(self) -> LookupHashes:
"""The hashes to identify this file by against a ROM database.
Often not the file's own digests: a CHD is indexed by the disc data
embedded in its header, and a multi-file archive by its largest member
(the ROM itself, next to readmes and the like). The file's own hashes
cover the container, which no database holds.
"""
if self.chd_sha1_hash:
return LookupHashes(crc=None, md5=None, sha1=self.chd_sha1_hash)
if self.archive_members:
largest = max(self.archive_members, key=lambda m: m.get("size") or 0)
return LookupHashes(
crc=largest.get("crc_hash"),
md5=largest.get("md5_hash"),
sha1=largest.get("sha1_hash"),
)
return LookupHashes(crc=self.crc_hash, md5=self.md5_hash, sha1=self.sha1_hash)
@cached_property
def is_top_level(self) -> bool:
# The rom's own file, or a file directly inside the rom's folder
return self.rom.full_path in (self.full_path, self.file_path)
def file_name_for_download(self, hidden_folder: bool = False) -> str:
# This needs a trailing slash in the path to work!
return self.full_path.replace(
f"{self.rom.full_path}/", ".hidden/" if hidden_folder else ""
)
def __repr__(self) -> str:
return f"{self.file_name} ({self.id} -> {self.rom_id})"
class TrackMeta(BaseModel):
__tablename__ = "track_meta"
__table_args__ = (
Index("idx_track_meta_rom_id", "rom_id"),
Index("idx_track_meta_duration", "duration_seconds"),
Index("idx_track_meta_year", "year"),
Index("idx_track_meta_artist", "artist"),
Index("idx_track_meta_album", "album"),
)
rom_file_id: Mapped[int] = mapped_column(
ForeignKey("rom_files.id", ondelete="CASCADE"), primary_key=True
)
rom_id: Mapped[int] = mapped_column(ForeignKey("roms.id", ondelete="CASCADE"))
title: Mapped[str | None] = mapped_column(
String(length=AUDIO_TAG_MAX_LENGTH), default=None
)
artist: Mapped[str | None] = mapped_column(
String(length=AUDIO_TAG_MAX_LENGTH), default=None
)
album: Mapped[str | None] = mapped_column(
String(length=AUDIO_TAG_MAX_LENGTH), default=None
)
genre: Mapped[str | None] = mapped_column(String(length=255), default=None)
year: Mapped[int | None] = mapped_column(SmallInteger(), default=None)
track: Mapped[int | None] = mapped_column(SmallInteger(), default=None)
disc: Mapped[int | None] = mapped_column(SmallInteger(), default=None)
duration_seconds: Mapped[float | None] = mapped_column(Float(), default=None)
has_embedded_cover: Mapped[bool] = mapped_column(
Boolean(), default=False, nullable=False
)
cover_path: Mapped[str | None] = mapped_column(String(length=1024), default=None)
rom_file: Mapped[RomFile] = relationship(back_populates="track_meta")
# Max length for free-text document metadata (author / title).
DOC_META_MAX_LENGTH = 512
class RomFileDocMeta(BaseModel):
"""Provenance sidecar for document-category files (manuals, walkthroughs).
Only doc-category RomFiles carry a row here, so these fields don't bloat
every rom_files row. Format is intentionally not stored: it is derived from
the file extension.
"""
__tablename__ = "rom_file_doc_meta"
rom_file_id: Mapped[int] = mapped_column(
ForeignKey("rom_files.id", ondelete="CASCADE"), primary_key=True
)
source: Mapped[DocSource] = mapped_column(
Enum(DocSource), default=DocSource.UPLOAD, nullable=False
)
source_url: Mapped[str | None] = mapped_column(Text, default=None)
author: Mapped[str | None] = mapped_column(
String(length=DOC_META_MAX_LENGTH), default=None
)
title: Mapped[str | None] = mapped_column(
String(length=DOC_META_MAX_LENGTH), default=None
)
rom_file: Mapped[RomFile] = relationship(back_populates="doc_meta")
class RomFileUser(BaseModel):
"""Per-user reading state for a document-category file.
Keyed on (rom_file_id, user_id) so one mechanism covers both manuals and
walkthroughs. `progress` is a 0.0-1.0 scroll fraction; `last_page` tracks
the page for paginated (PDF) documents.
"""
__tablename__ = "rom_file_user"
__table_args__ = (
UniqueConstraint("rom_file_id", "user_id", name="unique_rom_file_user"),
Index("idx_rom_file_user", "rom_file_id", "user_id"),
)
id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
rom_file_id: Mapped[int] = mapped_column(
ForeignKey("rom_files.id", ondelete="CASCADE")
)
user_id: Mapped[int] = mapped_column(ForeignKey("users.id", ondelete="CASCADE"))
progress: Mapped[float] = mapped_column(Float(), default=0.0, nullable=False)
last_page: Mapped[int | None] = mapped_column(Integer(), default=None)
finished: Mapped[bool] = mapped_column(default=False, nullable=False)
last_read_at: Mapped[datetime | None] = mapped_column(TIMESTAMP(timezone=True))
rom_file: Mapped[RomFile] = relationship(
lazy="joined", back_populates="user_states"
)
user: Mapped[User] = relationship(lazy="joined")
class RomMetadata(BaseModel):
__tablename__ = "roms_metadata"
rom_id: Mapped[int] = mapped_column(
ForeignKey("roms.id", ondelete="CASCADE"), primary_key=True
)
genres: Mapped[list[str] | None] = mapped_column(CustomJSON(), default=[])
franchises: Mapped[list[str] | None] = mapped_column(CustomJSON(), default=[])
collections: Mapped[list[str] | None] = mapped_column(CustomJSON(), default=[])
companies: Mapped[list[str] | None] = mapped_column(CustomJSON(), default=[])
publishers: Mapped[list[str] | None] = mapped_column(CustomJSON(), default=[])
developers: Mapped[list[str] | None] = mapped_column(CustomJSON(), default=[])
game_modes: Mapped[list[str] | None] = mapped_column(CustomJSON(), default=[])
age_ratings: Mapped[list[str] | None] = mapped_column(CustomJSON(), default=[])
# IGDB-only descriptors: community tags plus the curated theme and
# viewpoint lists, more specific about how a game plays than genre.
keywords: Mapped[list[str] | None] = mapped_column(CustomJSON(), default=[])
themes: Mapped[list[str] | None] = mapped_column(CustomJSON(), default=[])
player_perspectives: Mapped[list[str] | None] = mapped_column(
CustomJSON(), default=[]
)
player_count: Mapped[str | None] = mapped_column(String(length=100), default="1")
first_release_date: Mapped[int | None] = mapped_column(BigInteger(), default=None)
average_rating: Mapped[float | None] = mapped_column(default=None)
# Votes behind `average_rating`; zero where no provider reported one.
rating_count: Mapped[int | None] = mapped_column(BigInteger(), default=0)
rom: Mapped[Rom] = relationship(lazy="joined", back_populates="metadatum")
@property
def primary_developer(self) -> str | None:
"""Developer for exporters, falling back to the pre-split companies ordering."""
companies = self.companies or []
return next(iter(self.developers or companies[:1]), None)
@property
def primary_publisher(self) -> str | None:
"""Publisher for exporters, falling back to the pre-split companies ordering."""
companies = self.companies or []
return next(iter(self.publishers or companies[1:2]), None)
class RomFacets(BaseModel):
"""Narrow mirror of the per-ROM values that back the filter dropdowns.
The same values live on `roms` (as STORED generated columns, plus the
region/language/tag columns), but those rows also carry the raw provider
metadata blobs, so aggregating them reads the whole multi-gigabyte table.
This table holds a few MB of the same data and is kept in sync by triggers
on `roms`, so no write path has to remember to update it.
"""
__tablename__ = "roms_facets"
__table_args__ = (Index("idx_roms_facets_platform_id", "platform_id"),)
rom_id: Mapped[int] = mapped_column(
ForeignKey("roms.id", ondelete="CASCADE"), primary_key=True
)
platform_id: Mapped[int] = mapped_column(Integer(), nullable=False)
genres: Mapped[list[str] | None] = mapped_column(CustomJSON(), default=[])
franchises: Mapped[list[str] | None] = mapped_column(CustomJSON(), default=[])
collections: Mapped[list[str] | None] = mapped_column(CustomJSON(), default=[])
companies: Mapped[list[str] | None] = mapped_column(CustomJSON(), default=[])
publishers: Mapped[list[str] | None] = mapped_column(CustomJSON(), default=[])
developers: Mapped[list[str] | None] = mapped_column(CustomJSON(), default=[])
game_modes: Mapped[list[str] | None] = mapped_column(CustomJSON(), default=[])
age_ratings: Mapped[list[str] | None] = mapped_column(CustomJSON(), default=[])
keywords: Mapped[list[str] | None] = mapped_column(CustomJSON(), default=[])
themes: Mapped[list[str] | None] = mapped_column(CustomJSON(), default=[])
player_perspectives: Mapped[list[str] | None] = mapped_column(
CustomJSON(), default=[]
)
player_count: Mapped[str | None] = mapped_column(String(length=100), default="1")
regions: Mapped[list[str] | None] = mapped_column(CustomJSON(), default=[])
languages: Mapped[list[str] | None] = mapped_column(CustomJSON(), default=[])
tags: Mapped[list[str] | None] = mapped_column(CustomJSON(), default=[])
# Provider match ids, mirrored so the Server Stats coverage breakdown counts
# them here instead of scanning `roms`. A populated column means a match.
igdb_id: Mapped[int | None] = mapped_column(Integer(), default=None)
ss_id: Mapped[int | None] = mapped_column(Integer(), default=None)
moby_id: Mapped[int | None] = mapped_column(Integer(), default=None)
launchbox_id: Mapped[int | None] = mapped_column(Integer(), default=None)
ra_id: Mapped[int | None] = mapped_column(Integer(), default=None)
hasheous_id: Mapped[int | None] = mapped_column(Integer(), default=None)
tgdb_id: Mapped[int | None] = mapped_column(Integer(), default=None)
flashpoint_id: Mapped[str | None] = mapped_column(String(length=100), default=None)
hltb_id: Mapped[int | None] = mapped_column(Integer(), default=None)
demozoo_id: Mapped[int | None] = mapped_column(Integer(), default=None)
pouet_id: Mapped[int | None] = mapped_column(Integer(), default=None)
csdb_id: Mapped[int | None] = mapped_column(Integer(), default=None)
steam_id: Mapped[int | None] = mapped_column(Integer(), default=None)
gamelist_id: Mapped[str | None] = mapped_column(String(length=100), default=None)
libretro_id: Mapped[str | None] = mapped_column(String(length=64), default=None)
min_age: Mapped[int | None] = mapped_column(Integer(), default=None)
created_at: Mapped[datetime] = mapped_column(
TIMESTAMP(timezone=True), server_default=func.now()
)
updated_at: Mapped[datetime] = mapped_column(
TIMESTAMP(timezone=True), server_default=func.now()
)
class RomVisibility(NamedTuple):
"""The columns a ROM's visibility check reads, without a full `Rom`."""
id: int
platform_id: int
min_age: int | None
class RomVisibilityLabel(NamedTuple):
"""`RomVisibility` plus the name pair the file-delete routes log."""
id: int
platform_id: int
min_age: int | None
name: str | None
fs_name: str
class RomInstallTarget(NamedTuple):
"""`RomVisibility` plus what a device install request is checked against."""
id: int
platform_id: int
min_age: int | None
platform_slug: str
missing_from_fs: bool
class RomDeletionTarget(NamedTuple):
"""The columns the bulk-delete route reads off one rom, and no relations."""
id: int
platform_id: int
min_age: int | None
name: str | None
fs_name: str
fs_path: str
platform_slug: str
platform_name: str
platform_custom_name: str | None
@property
def platform_display_name(self) -> str:
return self.platform_custom_name or self.platform_name
@property
def fs_resources_path(self) -> str:
return rom_fs_resources_path(self.platform_id, self.id)
def rom_fs_resources_path(platform_id: int, id: int) -> str:
"""Resources subfolder a rom's artwork and documents are written to."""
return f"roms/{platform_id}/{id}"
_ROM_RESOURCE_URL_ID = re.compile(
rf"^{re.escape(FRONTEND_RESOURCES_PATH)}/roms/\d+/(\d+)/"
)
def rom_id_from_resource_url(url: str) -> int | None:
"""The rom id in a URL under `rom_fs_resources_path`, or None for any other URL."""
match = _ROM_RESOURCE_URL_ID.match(url)
return int(match[1]) if match else None
class Rom(BaseModel):
__tablename__ = "roms"
id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
igdb_id: Mapped[int | None] = mapped_column(Integer(), default=None)
sgdb_id: Mapped[int | None] = mapped_column(Integer(), default=None)
moby_id: Mapped[int | None] = mapped_column(Integer(), default=None)
ss_id: Mapped[int | None] = mapped_column(Integer(), default=None)
ra_id: Mapped[int | None] = mapped_column(Integer(), default=None)
launchbox_id: Mapped[int | None] = mapped_column(Integer(), default=None)
hasheous_id: Mapped[int | None] = mapped_column(Integer(), default=None)
tgdb_id: Mapped[int | None] = mapped_column(Integer(), default=None)
flashpoint_id: Mapped[str | None] = mapped_column(String(length=100), default=None)
hltb_id: Mapped[int | None] = mapped_column(Integer(), default=None)
demozoo_id: Mapped[int | None] = mapped_column(Integer(), default=None)
pouet_id: Mapped[int | None] = mapped_column(Integer(), default=None)
csdb_id: Mapped[int | None] = mapped_column(Integer(), default=None)
steam_id: Mapped[int | None] = mapped_column(Integer(), default=None)
gamelist_id: Mapped[str | None] = mapped_column(String(length=100), default=None)
libretro_id: Mapped[str | None] = mapped_column(String(length=64), default=None)
__table_args__ = (
# A custom library structure can hold the same file name in two folders,
# so the whole path is the identity. Indexed through its digest because
# fs_path + fs_name is 5804 bytes, over InnoDB's 3072-byte key limit.
Index(
"idx_roms_platform_id_full_path_hash",
"platform_id",
"full_path_hash",
unique=True,
),
# The digest is opaque to a range scan, so the scan loop's
# (platform_id, fs_name) batch lookup needs an index of its own.
Index("idx_roms_platform_id_fs_name", "platform_id", "fs_name"),
Index("idx_roms_fs_name_no_ext", "fs_name_no_ext"),
# Covers the group_by_meta_id dedup window, which reads only these
# columns, so the index has to carry every one of them: a single
# missing column (flashpoint_id or fs_name_no_ext, the window's
# partition tail and sort tiebreaker) drops the plan to a full scan of
# the wide roms row, JSON metadata blobs included.
Index(
"idx_roms_sibling_cover",
"platform_id",
"igdb_id",
"moby_id",
"ss_id",
"launchbox_id",
"ra_id",
"hasheous_id",
"tgdb_id",
"flashpoint_id",
"steam_id",
"fs_name_no_ext",
"generated_primary_region",
"id",
),
Index("idx_roms_platform_fs_size", "platform_id", "fs_size_bytes"),
# Covers a platform's age-hidden totals; InnoDB appends `id` for direct hides.
Index("idx_roms_platform_min_age", "platform_id", "min_age", "fs_size_bytes"),
# The remaining gallery sorts, each paired with the `id` tiebreak the
# gallery orders by; the key alone would not match its ORDER BY.
Index("idx_roms_platform_id_sorted", "platform_id", "id"),
Index("idx_roms_fs_size_bytes_sorted", "fs_size_bytes", "id"),
Index("idx_roms_created_at_sorted", "created_at", "id"),
Index("idx_roms_missing_from_fs", "missing_from_fs", "name_sort_key"),
Index("idx_roms_platform_name_sort_key", "platform_id", "name_sort_key"),
Index("idx_roms_name", "name"),
Index("idx_roms_name_sort_key", "name_sort_key"),
# Gallery sorts exposed through ROM_METADATA_ORDER_COLUMNS: the value
# alone serves the range filters, the `_sort` triple the ascending sort
# through its `id` tiebreak. PostgreSQL adds `idx_roms_<column>_desc`.
Index("idx_roms_generated_first_release_date", "generated_first_release_date"),
Index(
"idx_roms_generated_first_release_date_sort",
"generated_first_release_date_unset",
"generated_first_release_date",
"id",
),
Index("idx_roms_generated_average_rating", "generated_average_rating"),
Index(
"idx_roms_generated_average_rating_sort",
"generated_average_rating_unset",
"generated_average_rating",
"id",
),
Index("idx_roms_generated_player_count", "generated_player_count"),
Index("idx_roms_igdb_id", "igdb_id"),
Index("idx_roms_moby_id", "moby_id"),
Index("idx_roms_ss_id", "ss_id"),
Index("idx_roms_ra_id", "ra_id"),
Index("idx_roms_sgdb_id", "sgdb_id"),
Index("idx_roms_launchbox_id", "launchbox_id"),
Index("idx_roms_hasheous_id", "hasheous_id"),
Index("idx_roms_tgdb_id", "tgdb_id"),
Index("idx_roms_flashpoint_id", "flashpoint_id"),
Index("idx_roms_hltb_id", "hltb_id"),
Index("idx_roms_hltb_main_story", "generated_hltb_main_story"),
Index(
"idx_roms_generated_hltb_main_story_sort",
"generated_hltb_main_story_unset",
"generated_hltb_main_story",
"id",
),
Index("idx_roms_demozoo_id", "demozoo_id"),
Index("idx_roms_pouet_id", "pouet_id"),
Index("idx_roms_csdb_id", "csdb_id"),
Index("idx_roms_steam_id", "steam_id"),
Index("idx_roms_gamelist_id", "gamelist_id"),
Index("idx_roms_libretro_id", "libretro_id"),
Index("idx_roms_title_id", "title_id"),
# Searching the gallery by a hash digest
Index("idx_roms_crc_hash", "crc_hash"),
Index("idx_roms_md5_hash", "md5_hash"),
Index("idx_roms_sha1_hash", "sha1_hash"),
Index("idx_roms_ra_hash", "ra_hash"),
Index("ix_roms_updated_at", "updated_at"),
)
fs_name: Mapped[str] = mapped_column(String(length=FILE_NAME_MAX_LENGTH))
fs_name_no_tags: Mapped[str] = mapped_column(String(length=FILE_NAME_MAX_LENGTH))
fs_name_no_ext: Mapped[str] = mapped_column(String(length=FILE_NAME_MAX_LENGTH))
fs_extension: Mapped[str] = mapped_column(String(length=FILE_EXTENSION_MAX_LENGTH))
fs_path: Mapped[str] = mapped_column(String(length=FILE_PATH_MAX_LENGTH))
full_path_hash: Mapped[str] = mapped_column(String(length=FULL_PATH_HASH_LENGTH))
fs_size_bytes: Mapped[int] = mapped_column(BigInteger(), default=0)
name: Mapped[str | None] = mapped_column(String(length=350))
name_sort_key: Mapped[str | None] = mapped_column(
String(length=NAME_SORT_KEY_MAX_LENGTH), default=None
)
slug: Mapped[str | None] = mapped_column(String(length=400))
summary: Mapped[str | None] = mapped_column(Text)
igdb_metadata: Mapped[dict[str, Any] | None] = mapped_column(
CustomJSON(), default=dict
)
moby_metadata: Mapped[dict[str, Any] | None] = mapped_column(
CustomJSON(), default=dict
)
ss_metadata: Mapped[dict[str, Any] | None] = mapped_column(
CustomJSON(), default=dict
)
ra_metadata: Mapped[dict[str, Any] | None] = mapped_column(
CustomJSON(), default=dict
)
launchbox_metadata: Mapped[dict[str, Any] | None] = mapped_column(
CustomJSON(), default=dict
)
hasheous_metadata: Mapped[dict[str, Any] | None] = mapped_column(
CustomJSON(), default=dict
)
flashpoint_metadata: Mapped[dict[str, Any] | None] = mapped_column(
CustomJSON(), default=dict
)
hltb_metadata: Mapped[dict[str, Any] | None] = mapped_column(
CustomJSON(), default=dict
)
demozoo_metadata: Mapped[dict[str, Any] | None] = mapped_column(
CustomJSON(), default=dict
)
pouet_metadata: Mapped[dict[str, Any] | None] = mapped_column(
CustomJSON(), default=dict
)
csdb_metadata: Mapped[dict[str, Any] | None] = mapped_column(
CustomJSON(), default=dict
)
steam_metadata: Mapped[dict[str, Any] | None] = mapped_column(
CustomJSON(), default=dict
)
gamelist_metadata: Mapped[dict[str, Any] | None] = mapped_column(
CustomJSON(), default=dict
)
manual_metadata: Mapped[dict[str, Any] | None] = mapped_column(
CustomJSON(), default=dict
)
# Read-only slice of the stored generated columns from the `roms_metadata` view
generated_first_release_date: Mapped[int | None] = mapped_column(
BigInteger(),
nullable=True,
server_default=FetchedValue(),
server_onupdate=FetchedValue(),
)
generated_average_rating: Mapped[float | None] = mapped_column(
Float(),
nullable=True,
server_default=FetchedValue(),
server_onupdate=FetchedValue(),
)
generated_player_count: Mapped[str | None] = mapped_column(
String(length=100),
nullable=True,
server_default=FetchedValue(),
server_onupdate=FetchedValue(),
)
# Seconds, as HowLongToBeat reports them.
generated_hltb_main_story: Mapped[int | None] = mapped_column(
BigInteger(),
nullable=True,
server_default=FetchedValue(),
server_onupdate=FetchedValue(),
)
# The ascending gallery sort leads with these so unset metadata lands last
# off an index. `nullable=True` matches the DDL, which MariaDB forces by
# taking no NOT NULL on a generated column; `IS NULL` never yields NULL, so
# the values themselves are always `bool`.
generated_first_release_date_unset: Mapped[bool] = mapped_column(
Boolean(),
nullable=True,
server_default=FetchedValue(),
server_onupdate=FetchedValue(),
)
generated_average_rating_unset: Mapped[bool] = mapped_column(
Boolean(),
nullable=True,
server_default=FetchedValue(),
server_onupdate=FetchedValue(),
)
generated_hltb_main_story_unset: Mapped[bool] = mapped_column(
Boolean(),
nullable=True,
server_default=FetchedValue(),
server_onupdate=FetchedValue(),
)
path_cover_s: Mapped[str | None] = mapped_column(Text, default="")
path_cover_l: Mapped[str | None] = mapped_column(Text, default="")
url_cover: Mapped[str | None] = mapped_column(
Text, default="", doc="URL to cover image stored in IGDB"
)
path_manual: Mapped[str | None] = mapped_column(Text, default="")
url_manual: Mapped[str | None] = mapped_column(
Text, default="", doc="URL to manual stored in ScreenScraper"
)
path_screenshots: Mapped[list[str] | None] = mapped_column(CustomJSON(), default=[])
url_screenshots: Mapped[list[str] | None] = mapped_column(
CustomJSON(), default=[], doc="URLs to screenshots stored in IGDB"
)
locked_fields: Mapped[list[str] | None] = mapped_column(
CustomJSON(),
default=[],
doc="Slots a user owns, whose stored file a scan must leave alone",
)
revision: Mapped[str | None] = mapped_column(String(length=100))
version: Mapped[str | None] = mapped_column(String(length=100))
regions: Mapped[list[str] | None] = mapped_column(CustomJSON(), default=[])
languages: Mapped[list[str] | None] = mapped_column(CustomJSON(), default=[])
tags: Mapped[list[str] | None] = mapped_column(CustomJSON(), default=[])
# STORED generated column over regions[0], carried by idx_roms_sibling_cover
# so the dedup window can rank regions without reading the JSON.
generated_primary_region: Mapped[str | None] = mapped_column(
String(length=50),
nullable=True,
server_default=FetchedValue(),
server_onupdate=FetchedValue(),
)
# The folded name and aliases, kept by `compute_search_titles` on every
# write. The gallery search filters on it and ranks whole titles against it.
search_titles: Mapped[str | None] = mapped_column(Text(), deferred=True)
# The strictest age any of the ROM's ratings sets, kept by `compute_min_age`
# on every write; NULL when unrated. Visibility rules compare against it.
min_age: Mapped[int | None] = mapped_column(Integer(), nullable=True)
crc_hash: Mapped[str | None] = mapped_column(String(length=100))
md5_hash: Mapped[str | None] = mapped_column(String(length=100))
sha1_hash: Mapped[str | None] = mapped_column(String(length=100))
ra_hash: Mapped[str | None] = mapped_column(String(length=100))
title_id: Mapped[str | None] = mapped_column(
String(length=TITLE_ID_MAX_LENGTH),
doc="Platform-native identity read from the ROM binary, normalized (0100ABCD12340000, SLUS-20152)",
)
save_target: Mapped[str | None] = mapped_column(
String(length=TITLE_ID_MAX_LENGTH),
doc="On-disk name an emulator gives this game's saves; a file stem, a folder, or a nested path",
)
save_target_layout: Mapped[SaveTargetLayout | None] = mapped_column(
Enum(SaveTargetLayout),
default=None,
doc="How to apply save_target: one folder, a folder prefix, a file prefix, or a nested path",
)
missing_from_fs: Mapped[bool] = mapped_column(default=False, nullable=False)
# Physical games are manually-added rows with no file on disk; they carry the
# same metadata as digital ROMs but must never be flagged missing or cleaned up.
is_physical: Mapped[bool] = mapped_column(default=False, nullable=False)
upc: Mapped[str | None] = mapped_column(String(length=64), default=None)
platform_id: Mapped[int] = mapped_column(
ForeignKey("platforms.id", ondelete="CASCADE")
)
platform: Mapped[Platform] = relationship(lazy="joined", back_populates="roms")
sibling_roms: Mapped[list[Rom]] = relationship(
secondary="sibling_roms",
primaryjoin="Rom.id == SiblingRom.rom_id",
secondaryjoin="Rom.id == SiblingRom.sibling_rom_id",
collection_class=UniqueSiblingList,
lazy="raise",
)
files: Mapped[list[RomFile]] = relationship(lazy="raise", back_populates="rom")
saves: Mapped[list[Save]] = relationship(lazy="raise", back_populates="rom")
states: Mapped[list[State]] = relationship(lazy="raise", back_populates="rom")
screenshots: Mapped[list[Screenshot]] = relationship(
lazy="raise", back_populates="rom"
)
rom_users: Mapped[list[RomUser]] = relationship(lazy="raise", back_populates="rom")
notes: Mapped[list[RomNote]] = relationship(lazy="raise", back_populates="rom")
metadatum: Mapped[RomMetadata] = relationship(
lazy="joined", back_populates="rom", uselist=False
)
collections: Mapped[list[Collection]] = relationship(
"Collection",
secondary="collections_roms",
collection_class=set,
lazy="raise",
back_populates="roms",
)
similar_roms: Mapped[list[RomSimilarity]] = relationship(
"RomSimilarity",
foreign_keys="RomSimilarity.rom_id",
lazy="raise",
back_populates="rom",
passive_deletes=True,
)
def __init__(self, *args: Any, **kwargs: Any) -> None:
super().__init__(*args, **kwargs)
self._is_identifying = False
@validates("name", "name_sort_key")
def _sync_name_sort_key(self, key: str, value: str | None) -> str | None:
"""Keep the indexed `name_sort_key` in sync with `name`"""
if key == "name_sort_key":
return compute_name_sort_key(value or self.name)
if self.name_sort_key is None or self.name_sort_key == compute_name_sort_key(
self.name
):
self.name_sort_key = compute_name_sort_key(value)
return value
@validates("fs_name", "fs_path")
def _sync_fs_derived_columns(self, key: str, value: str) -> str:
"""Derive the stored `fs_name_no_tags` / `fs_name_no_ext` /
`fs_extension` / `full_path_hash` columns from the file's name and path.
Fires on attribute set (ORM construction and mutation) only. Bulk
`update()` statements bypass the ORM and set these explicitly (see
`update_rom`).
"""
# `full_path` caches the two halves joined, so it cannot survive a set.
self.__dict__.pop("full_path", None)
# The hook runs before the value lands, so the digest reads the incoming
# half rather than the stale one on the instance.
if key == "fs_path":
self.full_path_hash = compute_full_path_hash(value, self.fs_name)
return value
parts = compute_file_name_parts(value)
self.fs_name_no_tags = parts.no_tags
self.fs_name_no_ext = parts.no_ext
self.fs_extension = parts.extension
self.full_path_hash = compute_full_path_hash(self.fs_path, value)
return value
@property
def platform_slug(self) -> str:
return self.platform.slug
@property
def platform_fs_slug(self) -> str:
return self.platform.fs_slug
@property
def platform_custom_name(self) -> str | None:
return self.platform.custom_name
@property
def platform_display_name(self) -> str:
return self.platform.custom_name or self.platform.name
@cached_property
def full_path(self) -> str:
return f"{self.fs_path}/{self.fs_name}"
@cached_property
def has_manual(self) -> bool:
return bool(self.path_manual)
@declared_attr
def has_soundtrack(cls) -> Mapped[bool]:
return column_property(
select(RomFile.id)
.where(
and_(
RomFile.rom_id == cls.id,
RomFile.category == RomFileCategory.SOUNDTRACK,
)
)
.correlate_except(RomFile)
.exists()
.select()
.scalar_subquery(),
deferred=True,
)
@cached_property
def merged_screenshots(self) -> list[str]:
if self.path_screenshots:
return [f"{FRONTEND_RESOURCES_PATH}/{s}" for s in self.path_screenshots]
return []
if TYPE_CHECKING:
# Defined out-of-line at module scope via column_property
multi_file: Mapped[bool]
top_level_file_count: Mapped[int]
is_easyrpg_game: Mapped[bool]
@property
def has_simple_single_file(self) -> bool:
return not self.multi_file and self.top_level_file_count == 1
@property
def has_nested_single_file(self) -> bool:
return self.multi_file and self.top_level_file_count == 1
@property
def has_multiple_files(self) -> bool:
return self.top_level_file_count > 1
@property
def fs_resources_path(self) -> str:
return rom_fs_resources_path(self.platform_id, self.id)
@property
def path_cover_small(self) -> str:
return (
f"{FRONTEND_RESOURCES_PATH}/{self.path_cover_s}?ts={self.updated_at}"
if self.path_cover_s
else ""
)
@property
def path_cover_large(self) -> str:
return (
f"{FRONTEND_RESOURCES_PATH}/{self.path_cover_l}?ts={self.updated_at}"
if self.path_cover_l
else ""
)
@property
def path_video(self) -> str | None:
return (
(self.ss_metadata or {}).get("video_path")
or (self.ss_metadata or {}).get("video_normalized_path")
or (self.gamelist_metadata or {}).get("video_path")
or (self.launchbox_metadata or {}).get("video_path")
)
def is_field_locked(self, field: str) -> bool:
"""Whether a user supplied this field by hand, so scans must leave it."""
return field in (self.locked_fields or [])
def locked_fields_with(self, field: str) -> list[str]:
"""This rom's locks plus ``field``, for handing to an update."""
return sorted({*(self.locked_fields or []), field})
def locked_fields_without(self, field: str) -> list[str]:
"""This rom's locks minus ``field``, for handing to an update."""
return sorted({*(self.locked_fields or [])} - {field})
@property
def is_unidentified(self) -> bool:
return not self.is_identified
@property
def is_identified(self) -> bool:
return any(
getattr(self, column.key) for column in METADATA_SOURCE_COLUMNS.values()
)
@property
def has_file_on_disk(self) -> bool:
"""Whether a readable file backs this rom.
False for two different reasons that every file-dependent surface
(download, playback, the ES-DE and Pegasus exporters, the device feeds)
needs to treat alike: a physical game never had a file, and a missing
one no longer does.
"""
return not self.is_physical and not self.missing_from_fs
def has_m3u_file(self) -> bool:
"""
Check if the ROM has an M3U file associated with it.
This is used for multi-disc games.
"""
return any(file.file_extension.lower() == "m3u" for file in self.files)
# Metadata fields
@property
def youtube_video_id(self) -> str | None:
"""The blobs are client-writable, so validate on read, not on scan."""
for blob in (
self.igdb_metadata,
self.launchbox_metadata,
self.demozoo_metadata,
self.pouet_metadata,
):
video_id = valid_youtube_id(blob.get("youtube_video_id")) if blob else None
if video_id:
return video_id
return None
@property
def alternative_names(self) -> list[str]:
"""Every title `resolve_alternative_names` finds, but the displayed name."""
folded_name = fold_search_title(self.name or "")
return [
title
for title in resolve_alternative_names(alternative_name_metadata(self))
if fold_search_title(title) != folded_name
]
@cached_property
def merged_ra_metadata(self) -> dict[str, Any] | None:
if not self.ra_metadata or "achievements" not in self.ra_metadata:
return self.ra_metadata
# New dicts keep the stored badge paths relative for the filesystem.
# The provider keys achievements by id, so the stored order is arbitrary.
achievements = [
{
**achievement,
"badge_path_lock": f"{FRONTEND_RESOURCES_PATH}/{achievement['badge_path_lock']}",
"badge_path": f"{FRONTEND_RESOURCES_PATH}/{achievement['badge_path']}",
}
for achievement in sorted(
self.ra_metadata["achievements"], key=_ra_achievement_sort_key
)
]
return {**self.ra_metadata, "achievements": achievements}
# Used only during scan process
@property
def is_identifying(self) -> bool:
return self._is_identifying or False
@is_identifying.setter
def is_identifying(self, value: bool) -> None:
self._is_identifying = value
def __repr__(self) -> str:
return f"{self.fs_name} ({self.id})"
# Correlated scalar subqueries against rom_files, deferred and opt-in via `undefer`
# Revisit (real columns, JOIN/aggregate, or added indexes) if gallery latency regresses
_rom_full_path = func.concat(Rom.fs_path, "/", Rom.fs_name)
Rom.multi_file = column_property(
select(RomFile.id)
.where(
and_(
RomFile.rom_id == Rom.id,
RomFile.file_path != Rom.fs_path,
)
)
.correlate_except(RomFile)
.exists()
.select()
.scalar_subquery(),
deferred=True,
)
Rom.top_level_file_count = column_property(
select(func.count(RomFile.id))
.where(
and_(
RomFile.rom_id == Rom.id,
or_(
func.concat(RomFile.file_path, "/", RomFile.file_name)
== _rom_full_path,
RomFile.file_path == _rom_full_path,
),
)
)
.correlate_except(RomFile)
.scalar_subquery(),
deferred=True,
)
Rom.is_easyrpg_game = column_property(
select(RomFile.id)
.where(
and_(
RomFile.rom_id == Rom.id,
RomFile.file_path == _rom_full_path,
func.lower(RomFile.file_name) == EASYRPG_GAME_DATABASE,
RomFile.missing_from_fs.is_(False),
)
)
.correlate_except(RomFile)
.exists()
.select()
.scalar_subquery(),
deferred=True,
)
def apply_file_stats(rom: Rom, files: Sequence[RomFile]) -> None:
"""Fill the deferred file-stat columns from an already-loaded file list."""
set_committed_value(
rom, "multi_file", any(f.file_path != rom.fs_path for f in files)
)
set_committed_value(
rom,
"top_level_file_count",
sum(
1
for f in files
if f.full_path == rom.full_path or f.file_path == rom.full_path
),
)
set_committed_value(
rom,
"has_soundtrack",
any(f.category == RomFileCategory.SOUNDTRACK for f in files),
)
set_committed_value(
rom,
"is_easyrpg_game",
any(
f.file_path == rom.full_path
and f.file_name.lower() == EASYRPG_GAME_DATABASE
and not f.missing_from_fs
for f in files
),
)
SEARCH_TITLE_COLUMNS = ("name", *(column for column, _ in ALTERNATIVE_NAME_SOURCES))
# What `RomVisibility` holds and the loaders feeding a visibility check select.
ROM_VISIBILITY_COLUMNS = (Rom.id, Rom.platform_id, Rom.min_age)
def alternative_name_metadata(rom: Rom) -> dict[str, Any]:
"""Each `ALTERNATIVE_NAME_SOURCES` column's current value, by column name."""
return {column: getattr(rom, column) for column, _ in ALTERNATIVE_NAME_SOURCES}
def rom_search_titles(rom: Rom) -> str:
"""`compute_search_titles` over the ROM's current name and metadata."""
return compute_search_titles(rom.name, alternative_name_metadata(rom))
@event.listens_for(Rom, "before_insert")
def _set_search_titles(_mapper: Any, _connection: Any, rom: Rom) -> None:
rom.search_titles = rom_search_titles(rom)
@event.listens_for(Rom, "before_update")
def _refresh_search_titles(_mapper: Any, _connection: Any, rom: Rom) -> None:
state = inspect(rom)
if any(
state.attrs[column].history.has_changes() for column in SEARCH_TITLE_COLUMNS
):
rom.search_titles = rom_search_titles(rom)
def rom_min_age(rom: Rom) -> int | None:
"""`compute_min_age` over the ROM's current metadata."""
return compute_min_age(
{column: getattr(rom, column) for column in MIN_AGE_SOURCE_COLUMNS}
)
@event.listens_for(Rom, "before_insert")
def _set_min_age(_mapper: Any, _connection: Any, rom: Rom) -> None:
rom.min_age = rom_min_age(rom)
@event.listens_for(Rom, "before_update")
def _refresh_min_age(_mapper: Any, _connection: Any, rom: Rom) -> None:
state = inspect(rom)
if any(
state.attrs[column].history.has_changes() for column in MIN_AGE_SOURCE_COLUMNS
):
rom.min_age = rom_min_age(rom)
class HasFileOnDiskFilters(TypedDict):
physical: bool
missing: bool
# Query-side twin of `Rom.has_file_on_disk`, for callers that enumerate roms and
# want the file-less ones dropped by the database rather than after loading.
HAS_FILE_ON_DISK_FILTERS: Final[HasFileOnDiskFilters] = {
"physical": False,
"missing": False,
}
# Maps a metadata-source slug (matching the MetadataSource enum) to the Rom
# column holding that source's match id. A populated column means the ROM
# matched that source, and a ROM that matched any of them is identified.
# Sources without a per-ROM match id (e.g. sgdb covers, playmatch) are
# intentionally absent.
METADATA_SOURCE_COLUMNS: dict[str, InstrumentedAttribute[Any]] = {
"igdb": Rom.igdb_id,
"ss": Rom.ss_id,
"moby": Rom.moby_id,
"launchbox": Rom.launchbox_id,
"ra": Rom.ra_id,
"hasheous": Rom.hasheous_id,
"tgdb": Rom.tgdb_id,
"flashpoint": Rom.flashpoint_id,
"hltb": Rom.hltb_id,
"demozoo": Rom.demozoo_id,
"pouet": Rom.pouet_id,
"csdb": Rom.csdb_id,
"steam": Rom.steam_id,
"gamelist": Rom.gamelist_id,
"libretro": Rom.libretro_id,
}
# Query-side twin of `Rom.is_identified`, for the gallery "matched" filter. A
# garbled manual edit can store a 0 or "" id, which counts as no match.
ROM_IS_IDENTIFIED: Final[ColumnElement[bool]] = or_(
*(is_non_blank(column) for column in METADATA_SOURCE_COLUMNS.values())
)
# Same slugs mapped to the `roms_facets` mirror columns. The stats coverage
# breakdown counts these off the narrow mirror instead of scanning `roms`.
METADATA_SOURCE_FACET_COLUMNS: dict[str, InstrumentedAttribute[Any]] = {
"igdb": RomFacets.igdb_id,
"ss": RomFacets.ss_id,
"moby": RomFacets.moby_id,
"launchbox": RomFacets.launchbox_id,
"ra": RomFacets.ra_id,
"hasheous": RomFacets.hasheous_id,
"tgdb": RomFacets.tgdb_id,
"flashpoint": RomFacets.flashpoint_id,
"hltb": RomFacets.hltb_id,
"demozoo": RomFacets.demozoo_id,
"pouet": RomFacets.pouet_id,
"csdb": RomFacets.csdb_id,
"steam": RomFacets.steam_id,
"gamelist": RomFacets.gamelist_id,
"libretro": RomFacets.libretro_id,
}
class RomUserStatus(enum.StrEnum):
INCOMPLETE = "incomplete" # Started but not finished
FINISHED = "finished" # Reached the end of the game
COMPLETED_100 = "completed_100" # Completed 100%
RETIRED = "retired" # Won't play again
NEVER_PLAYING = "never_playing" # Will never play
class RomNote(BaseModel):
__tablename__ = "rom_notes"
__table_args__ = (
UniqueConstraint(
"rom_id", "user_id", "title", name="unique_rom_user_note_title"
),
Index("idx_rom_notes_public", "is_public"),
Index("idx_rom_notes_title", "title"),
)
id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
# Core note fields
title: Mapped[str] = mapped_column(String(400))
content: Mapped[str] = mapped_column(Text)
is_public: Mapped[bool] = mapped_column(default=False)
# Future extensibility fields
tags: Mapped[list[str] | None] = mapped_column(CustomJSON(), default=list)
# Timestamps
created_at: Mapped[datetime] = mapped_column(
TIMESTAMP(timezone=True), server_default=func.now()
)
updated_at: Mapped[datetime] = mapped_column(
TIMESTAMP(timezone=True), server_default=func.now(), onupdate=func.now()
)
# Foreign keys
rom_id: Mapped[int] = mapped_column(ForeignKey("roms.id", ondelete="CASCADE"))
user_id: Mapped[int] = mapped_column(ForeignKey("users.id", ondelete="CASCADE"))
# Relationships
rom: Mapped[Rom] = relationship(lazy="joined", back_populates="notes")
user: Mapped[User] = relationship(lazy="joined", back_populates="notes")
class RomUser(BaseModel):
__tablename__ = "rom_user"
__table_args__ = (
UniqueConstraint("rom_id", "user_id", name="unique_rom_user_props"),
# `unique_rom_user_props` leads with `rom_id` and so only covers the
# gallery's outer join; these cover starting from this table instead.
Index("ix_rom_user_user_rom", "user_id", "rom_id"),
Index("ix_rom_user_user_last_played", "user_id", "last_played"),
)
id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
is_main_sibling: Mapped[bool] = mapped_column(default=False)
last_played: Mapped[datetime | None] = mapped_column(TIMESTAMP(timezone=True))
backlogged: Mapped[bool] = mapped_column(default=False)
now_playing: Mapped[bool] = mapped_column(default=False)
hidden: Mapped[bool] = mapped_column(default=False)
# `zero_is_unset`: 0 renders as unset in the UI, exactly like having no
# `rom_user` row at all; sorts fold it into the NULL bucket.
rating: Mapped[int] = mapped_column(default=0, info={"zero_is_unset": True})
difficulty: Mapped[int] = mapped_column(default=0, info={"zero_is_unset": True})
completion: Mapped[int] = mapped_column(default=0, info={"zero_is_unset": True})
status: Mapped[RomUserStatus | None] = mapped_column(
Enum(RomUserStatus), default=None
)
# NULL means the default selection; an empty list pins nothing.
pinned_media: Mapped[list[str] | None] = mapped_column(
CustomJSON(none_as_null=True), default=None, nullable=True
)
rom_id: Mapped[int] = mapped_column(ForeignKey("roms.id", ondelete="CASCADE"))
user_id: Mapped[int] = mapped_column(ForeignKey("users.id", ondelete="CASCADE"))
rom: Mapped[Rom] = relationship(lazy="joined", back_populates="rom_users")
user: Mapped[User] = relationship(lazy="joined", back_populates="rom_users")