mirror of
https://github.com/rommapp/romm.git
synced 2026-12-03 18:43:23 +01:00
Centralize the *_no_tags / *_no_ext / *_extension columns (derived from a file name) behind @validates hooks instead of computing them by hand at every write site: - Add pure helpers (compute_file_name_parts and friends) to models.base; the filesystem base handler now delegates to them. - Add @validates on Rom (fs_name), BaseAsset (file_name, inherited by all asset subclasses), and Firmware (file_name). - update_rom keeps the fs_name-derived columns in sync on bulk update(), which also fixes the rename path never updating fs_extension. - Drop the now-redundant computations at the scan/rename call sites. Also fix the migration backfill loop and a pre-existing list[str | None] type mismatch surfaced in scan_handler. Add tests for the helpers, the validators, and the update_rom bulk-sync path. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
127 lines
4.4 KiB
Python
127 lines
4.4 KiB
Python
from __future__ import annotations
|
|
|
|
from functools import cached_property
|
|
from typing import TYPE_CHECKING
|
|
|
|
from sqlalchemy import BigInteger, ForeignKey, String
|
|
from sqlalchemy.orm import Mapped, mapped_column, relationship, validates
|
|
|
|
from models.base import (
|
|
FILE_EXTENSION_MAX_LENGTH,
|
|
FILE_NAME_MAX_LENGTH,
|
|
FILE_PATH_MAX_LENGTH,
|
|
BaseModel,
|
|
compute_file_name_parts,
|
|
)
|
|
|
|
if TYPE_CHECKING:
|
|
from models.device_save_sync import DeviceSaveSync
|
|
from models.rom import Rom
|
|
from models.user import User
|
|
|
|
|
|
class BaseAsset(BaseModel):
|
|
__abstract__ = True
|
|
|
|
id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
|
|
file_name: Mapped[str] = mapped_column(String(length=FILE_NAME_MAX_LENGTH))
|
|
file_name_no_tags: Mapped[str] = mapped_column(String(length=FILE_NAME_MAX_LENGTH))
|
|
file_name_no_ext: Mapped[str] = mapped_column(String(length=FILE_NAME_MAX_LENGTH))
|
|
file_extension: Mapped[str] = mapped_column(
|
|
String(length=FILE_EXTENSION_MAX_LENGTH)
|
|
)
|
|
file_path: Mapped[str] = mapped_column(String(length=FILE_PATH_MAX_LENGTH))
|
|
file_size_bytes: Mapped[int] = mapped_column(BigInteger(), default=0)
|
|
|
|
missing_from_fs: Mapped[bool] = mapped_column(default=False, nullable=False)
|
|
|
|
@validates("file_name")
|
|
def _sync_file_name_parts(self, _key: str, file_name: str) -> str:
|
|
"""Derive the stored `file_name_no_tags` / `file_name_no_ext` /
|
|
`file_extension` columns whenever `file_name` is assigned.
|
|
|
|
Defined on the abstract base so every asset subclass inherits it.
|
|
"""
|
|
parts = compute_file_name_parts(file_name)
|
|
self.file_name_no_tags = parts.no_tags
|
|
self.file_name_no_ext = parts.no_ext
|
|
self.file_extension = parts.extension
|
|
return file_name
|
|
|
|
@cached_property
|
|
def full_path(self) -> str:
|
|
return f"{self.file_path}/{self.file_name}"
|
|
|
|
@cached_property
|
|
def download_path(self) -> str:
|
|
return f"/api/raw/assets/{self.full_path}?timestamp={self.updated_at}"
|
|
|
|
|
|
class RomAsset(BaseAsset):
|
|
__abstract__ = True
|
|
|
|
rom_id: Mapped[int] = mapped_column(ForeignKey("roms.id", ondelete="CASCADE"))
|
|
user_id: Mapped[int] = mapped_column(ForeignKey("users.id", ondelete="CASCADE"))
|
|
|
|
|
|
class Screenshot(RomAsset):
|
|
__tablename__ = "screenshots"
|
|
__table_args__ = {"extend_existing": True}
|
|
|
|
rom: Mapped[Rom] = relationship(lazy="joined", back_populates="screenshots")
|
|
user: Mapped[User] = relationship(lazy="joined", back_populates="screenshots")
|
|
|
|
|
|
class Save(RomAsset):
|
|
__tablename__ = "saves"
|
|
__table_args__ = {"extend_existing": True}
|
|
|
|
emulator: Mapped[str | None] = mapped_column(String(length=50))
|
|
slot: Mapped[str | None] = mapped_column(String(length=255))
|
|
content_hash: Mapped[str | None] = mapped_column(String(length=32))
|
|
origin_device_id: Mapped[str | None] = mapped_column(
|
|
String(length=255),
|
|
ForeignKey("devices.id", ondelete="SET NULL"),
|
|
default=None,
|
|
)
|
|
|
|
rom: Mapped[Rom] = relationship(lazy="joined", back_populates="saves")
|
|
user: Mapped[User] = relationship(lazy="joined", back_populates="saves")
|
|
device_syncs: Mapped[list[DeviceSaveSync]] = relationship(
|
|
back_populates="save",
|
|
cascade="all, delete-orphan",
|
|
lazy="raise",
|
|
)
|
|
|
|
@cached_property
|
|
def screenshot(self) -> Screenshot | None:
|
|
from handler.database import db_screenshot_handler
|
|
|
|
return db_screenshot_handler.get_screenshot(
|
|
rom_id=self.rom_id,
|
|
user_id=self.user_id,
|
|
file_name=self.file_name, # Match state filename against screenshot filename stem
|
|
file_name_no_ext=self.file_name_no_ext,
|
|
)
|
|
|
|
|
|
class State(RomAsset):
|
|
__tablename__ = "states"
|
|
__table_args__ = {"extend_existing": True}
|
|
|
|
emulator: Mapped[str | None] = mapped_column(String(length=50))
|
|
|
|
rom: Mapped[Rom] = relationship(lazy="joined", back_populates="states")
|
|
user: Mapped[User] = relationship(lazy="joined", back_populates="states")
|
|
|
|
@cached_property
|
|
def screenshot(self) -> Screenshot | None:
|
|
from handler.database import db_screenshot_handler
|
|
|
|
return db_screenshot_handler.get_screenshot(
|
|
rom_id=self.rom_id,
|
|
user_id=self.user_id,
|
|
file_name=self.file_name, # Match state filename against screenshot filename stem
|
|
file_name_no_ext=self.file_name_no_ext,
|
|
)
|