mirror of
https://github.com/rommapp/romm.git
synced 2026-11-28 17:48:31 +01:00
* fix(backend): project the identifier endpoints onto the id column The `/identifiers` routes for saves, states, firmware, platforms, smart collections and rom notes each asked their handler for entities with `only_fields=[Model.id]`. `load_only` narrows the selected columns but does not cancel an eager relationship, so a `lazy="joined"` join still fired and a full entity was still built for every row. The platforms route was the worst of the six: `with_firmware` eager-loads every firmware row and its platform, and `load_only` does not cancel that either. Each handler grows a small query builder shared by its existing accessor and a new `*_ids` accessor that projects onto the id column with `with_only_columns`, which drops the entity and with it the eager joins. The firmware accessor already applied `noload`, so its SQL is unchanged; it moves to the projection only to stop building rows. `/api/users/identifiers` and the two `/collections` callers that use more than an id are left alone, because nothing eager fires for them. The `executed_statements` fixture moves to `tests/conftest.py` so the new endpoint tests can use it. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> * Update backend/tests/endpoints/test_identifier_endpoints.py Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com> * style(backend): drop the leading blank line black rejects Removing the module docstring in the GitHub edit left an empty first line, which is the only thing black wanted to change. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> * test(backend): drive the note-identifier test through the endpoint Greptile flagged the handler-level test for bypassing the route's auth, 404, and visibility checks. It now issues the request the frontend would and asserts the id-only projection holds among the route's own reads. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> * refactor(backend): fold the duplicate SQL-capture fixtures into the shared one Step 3 of the PR-ready gauntlet (simplify). The shared `executed_statements` fixture gains the two roms test files that still declared byte-identical local copies, and the id-only assertions share one statement parser instead of two. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> * docs(backend): tighten the get_platform_ids docstring to one line Step 4 of the PR-ready gauntlet (review-polish). Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> --------- Co-authored-by: Claude Sonnet 5 <noreply@anthropic.com> Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com>
182 lines
5.3 KiB
Python
182 lines
5.3 KiB
Python
from collections.abc import Collection, Sequence
|
|
|
|
from sqlalchemy import Select, and_, delete, desc, or_, select, update
|
|
from sqlalchemy.orm import Session
|
|
|
|
from decorators.database import begin_session
|
|
from models.assets import State
|
|
from models.rom import Rom
|
|
|
|
from .base_handler import DBBaseHandler
|
|
|
|
|
|
class DBStatesHandler(DBBaseHandler):
|
|
@begin_session
|
|
def add_state(
|
|
self,
|
|
state: State,
|
|
session: Session = None, # type: ignore
|
|
) -> State:
|
|
return session.merge(state)
|
|
|
|
@begin_session
|
|
def get_state(
|
|
self,
|
|
user_id: int,
|
|
id: int,
|
|
session: Session = None, # type: ignore
|
|
) -> State | None:
|
|
return session.scalar(select(State).filter_by(user_id=user_id, id=id).limit(1))
|
|
|
|
@begin_session
|
|
def get_state_by_filename(
|
|
self,
|
|
user_id: int,
|
|
rom_id: int,
|
|
file_name: str,
|
|
session: Session = None, # type: ignore
|
|
) -> State | None:
|
|
return session.scalar(
|
|
select(State)
|
|
.filter_by(rom_id=rom_id, user_id=user_id, file_name=file_name)
|
|
.limit(1)
|
|
)
|
|
|
|
def _states_query(
|
|
self,
|
|
user_id: int,
|
|
rom_ids: Collection[int] | None = None,
|
|
platform_id: int | None = None,
|
|
) -> Select[tuple[State]]:
|
|
query = select(State).filter_by(user_id=user_id)
|
|
|
|
# An empty collection is an explicit empty scope, not an absent filter.
|
|
if rom_ids is not None:
|
|
query = query.filter(State.rom_id.in_(rom_ids))
|
|
|
|
if platform_id:
|
|
query = query.join(Rom, State.rom_id == Rom.id).filter(
|
|
Rom.platform_id == platform_id
|
|
)
|
|
|
|
return query
|
|
|
|
@begin_session
|
|
def get_states(
|
|
self,
|
|
user_id: int,
|
|
rom_ids: Collection[int] | None = None,
|
|
platform_id: int | None = None,
|
|
session: Session = None, # type: ignore
|
|
) -> Sequence[State]:
|
|
query = self._states_query(
|
|
user_id=user_id, rom_ids=rom_ids, platform_id=platform_id
|
|
)
|
|
return session.scalars(query).all()
|
|
|
|
@begin_session
|
|
def get_state_ids(
|
|
self,
|
|
user_id: int,
|
|
rom_ids: Collection[int] | None = None,
|
|
platform_id: int | None = None,
|
|
session: Session = None, # type: ignore
|
|
) -> list[int]:
|
|
"""Ids only, so no `State` is built and no eager rom or user join fires."""
|
|
query = self._states_query(
|
|
user_id=user_id, rom_ids=rom_ids, platform_id=platform_id
|
|
)
|
|
return list(session.scalars(query.with_only_columns(State.id)).all())
|
|
|
|
@begin_session
|
|
def get_state_by_id(
|
|
self,
|
|
id: int,
|
|
session: Session = None, # type: ignore
|
|
) -> State | None:
|
|
"""Fetch a state by id without scoping to an owner. Used for the
|
|
visibility toggle and community downloads, where the caller may not own
|
|
the state. Mirrors db_screenshot_handler.get_screenshot_by_id."""
|
|
return session.get(State, id)
|
|
|
|
@begin_session
|
|
def get_rom_shared_states(
|
|
self,
|
|
rom_id: int,
|
|
user_id: int,
|
|
public_only: bool = False,
|
|
session: Session = None, # type: ignore
|
|
) -> Sequence[State]:
|
|
"""States for a ROM visible to the requesting user: own (public +
|
|
private) plus other users' public ones. Mirrors
|
|
db_screenshot_handler.get_rom_gallery_screenshots."""
|
|
query = select(State).filter(State.rom_id == rom_id)
|
|
|
|
if public_only:
|
|
query = query.filter(State.is_public)
|
|
else:
|
|
query = query.filter(or_(State.user_id == user_id, State.is_public))
|
|
|
|
query = query.order_by(desc(State.updated_at))
|
|
return session.scalars(query).all()
|
|
|
|
@begin_session
|
|
def update_state(
|
|
self,
|
|
id: int,
|
|
data: dict,
|
|
session: Session = None, # type: ignore
|
|
) -> State:
|
|
session.execute(
|
|
update(State)
|
|
.where(State.id == id)
|
|
.values(**data)
|
|
.execution_options(synchronize_session="evaluate")
|
|
)
|
|
return session.query(State).filter_by(id=id).one()
|
|
|
|
@begin_session
|
|
def delete_state(
|
|
self,
|
|
id: int,
|
|
session: Session = None, # type: ignore
|
|
) -> None:
|
|
session.execute(
|
|
delete(State)
|
|
.where(State.id == id)
|
|
.execution_options(synchronize_session="evaluate")
|
|
)
|
|
|
|
@begin_session
|
|
def mark_missing_states(
|
|
self,
|
|
rom_id: int,
|
|
user_id: int,
|
|
states_to_keep: list[str],
|
|
session: Session = None, # type: ignore
|
|
) -> Sequence[State]:
|
|
missing_states = session.scalars(
|
|
select(State).filter(
|
|
and_(
|
|
State.rom_id == rom_id,
|
|
State.user_id == user_id,
|
|
State.file_name.not_in(states_to_keep),
|
|
)
|
|
)
|
|
).all()
|
|
|
|
session.execute(
|
|
update(State)
|
|
.where(
|
|
and_(
|
|
State.rom_id == rom_id,
|
|
State.user_id == user_id,
|
|
State.file_name.not_in(states_to_keep),
|
|
)
|
|
)
|
|
.values(**{"missing_from_fs": True})
|
|
.execution_options(synchronize_session="evaluate")
|
|
)
|
|
|
|
return missing_states
|