mirror of
https://github.com/rommapp/romm.git
synced 2026-11-28 17:48:31 +01:00
Four query shapes had no index that could serve both their predicate and their sort, and a class of foreign-key columns was relying on an implicit index that only MariaDB and MySQL provide. New portable indexes: - `screenshots (rom_id, user_id)`. `Save.screenshot` / `State.screenshot` resolve a thumbnail per row, so this lookup runs once per card on the continue-playing rail. 0093 added the equivalent to `states` and noted `saves` already had it; `screenshots` was missed. - `rom_user (user_id, rom_id)` and `rom_user (user_id, last_played)`. `unique_rom_user_props` leads with `rom_id`, which covers the gallery's outer join but not the reverse direction, where sorting or scoping by a per-user column starts from this table. - `roms (platform_id, name_sort_key)`. The gallery is browsed one platform at a time and ordered by name, and no index carried both columns. On a 120k-row library across 20 platforms this takes the first page from 5.8ms/1568 buffers to 0.6ms/75 buffers. Deep offsets converge, since the skipped rows are walked either way. New PostgreSQL-only foreign-key indexes: MariaDB and MySQL index every foreign key whose column is not already some index's leftmost prefix; PostgreSQL does not. Eight such columns are queried directly or walked by `ON DELETE`, so they get a real index there. Creating them everywhere would leave the other backends with duplicates, so they are dialect-gated and exempt from autogenerate. Model/schema reconciliation: Fifteen live indexes existed only in the migrations. Worse, `alembic/env.py` never imported six models, so `devices`, `client_tokens`, `device_save_sync`, `sync_sessions`, `play_sessions` and `streaming_container_adoptions` were absent from the metadata autogenerate compares against, and autogenerate in fact failed outright resolving `saves.origin_device_id`. With the imports fixed it would have proposed dropping every one of those indexes. Declares all of them on their models, matches `PlaySession` to the unique index 0076 actually created, and adds a test that fails on any future index drift so this cannot silently recur. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01WgeQBXh6SJyxXVHSL5EuoF
36 lines
1.3 KiB
Python
36 lines
1.3 KiB
Python
from __future__ import annotations
|
|
|
|
from datetime import datetime
|
|
from typing import TYPE_CHECKING
|
|
|
|
from sqlalchemy import TIMESTAMP, ForeignKey, String
|
|
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
|
|
|
from models.base import BaseModel
|
|
|
|
if TYPE_CHECKING:
|
|
from models.device import Device
|
|
from models.user import User
|
|
|
|
|
|
class ClientToken(BaseModel):
|
|
__tablename__ = "client_tokens"
|
|
__table_args__ = {"extend_existing": True}
|
|
|
|
id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
|
|
user_id: Mapped[int] = mapped_column(
|
|
ForeignKey("users.id", ondelete="CASCADE"), index=True
|
|
)
|
|
name: Mapped[str] = mapped_column(String(255))
|
|
hashed_token: Mapped[str] = mapped_column(String(64), unique=True, index=True)
|
|
scopes: Mapped[str] = mapped_column(String(1000))
|
|
expires_at: Mapped[datetime | None] = mapped_column(TIMESTAMP(timezone=True))
|
|
last_used_at: Mapped[datetime | None] = mapped_column(TIMESTAMP(timezone=True))
|
|
|
|
device_id: Mapped[str | None] = mapped_column(
|
|
ForeignKey("devices.id", ondelete="SET NULL"), nullable=True, index=True
|
|
)
|
|
|
|
user: Mapped[User] = relationship(lazy="joined", back_populates="client_tokens")
|
|
device: Mapped[Device | None] = relationship(lazy="select")
|