mirror of
https://github.com/rommapp/romm.git
synced 2026-11-28 17:48:31 +01:00
Swap the hand-rolled platform picker in MissingFirmwareSection for the shared PlatformSelect, and fold the triplicated firmware builder in the tests into an add_firmware factory fixture. Keep Platform out of list_firmware: the relationship is lazy="joined", so every row also ran Platform's rom_count and fs_size_bytes aggregate subqueries over roms, for data no caller reads. In useTaskCompletion, the resolver already identifies the live wait, so drop the parallel generation counter and the unused cancel export. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
138 lines
4.1 KiB
Python
138 lines
4.1 KiB
Python
from collections.abc import Sequence
|
|
|
|
from sqlalchemy import and_, delete, select, update
|
|
from sqlalchemy.orm import QueryableAttribute, Session, load_only, noload
|
|
|
|
from decorators.database import begin_session
|
|
from models.firmware import Firmware
|
|
|
|
from .base_handler import DBBaseHandler
|
|
|
|
|
|
class DBFirmwareHandler(DBBaseHandler):
|
|
@begin_session
|
|
def add_firmware(
|
|
self,
|
|
firmware: Firmware,
|
|
session: Session = None, # type: ignore
|
|
) -> Firmware:
|
|
return session.merge(firmware)
|
|
|
|
@begin_session
|
|
def get_firmware(
|
|
self,
|
|
id: int,
|
|
*,
|
|
session: Session = None, # type: ignore
|
|
) -> Firmware | None:
|
|
return session.scalar(select(Firmware).filter_by(id=id).limit(1))
|
|
|
|
@begin_session
|
|
def list_firmware(
|
|
self,
|
|
*,
|
|
platform_id: int | None = None,
|
|
missing: bool | None = None,
|
|
only_fields: Sequence[QueryableAttribute] | None = None,
|
|
hidden_platform_ids: Sequence[int] | None = None,
|
|
session: Session = None, # type: ignore
|
|
) -> Sequence[Firmware]:
|
|
# `Firmware.platform` is `lazy="joined"`, which drags in Platform's
|
|
# rom_count / fs_size_bytes aggregate subqueries over `roms`. No
|
|
# caller here reads the relationship, so keep it out of the query.
|
|
query = (
|
|
select(Firmware)
|
|
.options(noload(Firmware.platform))
|
|
.order_by(Firmware.file_name.asc())
|
|
)
|
|
|
|
if platform_id:
|
|
query = query.filter_by(platform_id=platform_id)
|
|
|
|
if missing is not None:
|
|
query = query.filter(Firmware.missing_from_fs == missing)
|
|
|
|
# Firmware inherits its platform's visibility: hide firmware whose
|
|
# platform an admin has hidden from the caller.
|
|
if hidden_platform_ids:
|
|
query = query.filter(Firmware.platform_id.not_in(hidden_platform_ids))
|
|
|
|
if only_fields:
|
|
query = query.options(load_only(*only_fields))
|
|
|
|
return session.scalars(query).all()
|
|
|
|
@begin_session
|
|
def get_firmware_by_filename(
|
|
self,
|
|
platform_id: int,
|
|
file_name: str,
|
|
session: Session = None, # type: ignore
|
|
):
|
|
return session.scalar(
|
|
select(Firmware)
|
|
.filter_by(platform_id=platform_id, file_name=file_name)
|
|
.limit(1)
|
|
)
|
|
|
|
@begin_session
|
|
def update_firmware(
|
|
self,
|
|
id: int,
|
|
data: dict,
|
|
session: Session = None, # type: ignore
|
|
) -> Firmware:
|
|
session.execute(
|
|
update(Firmware)
|
|
.where(Firmware.id == id)
|
|
.values(**data)
|
|
.execution_options(synchronize_session="evaluate")
|
|
)
|
|
return session.query(Firmware).filter_by(id=id).one()
|
|
|
|
@begin_session
|
|
def delete_firmware(
|
|
self,
|
|
id: int,
|
|
session: Session = None, # type: ignore
|
|
) -> None:
|
|
session.execute(
|
|
delete(Firmware)
|
|
.where(Firmware.id == id)
|
|
.execution_options(synchronize_session="evaluate")
|
|
)
|
|
|
|
@begin_session
|
|
def mark_missing_firmware(
|
|
self,
|
|
platform_id: int,
|
|
fs_firmwares_to_keep: list[str],
|
|
session: Session = None, # type: ignore
|
|
) -> Sequence[Firmware]:
|
|
missing_firmware = (
|
|
session.scalars(
|
|
select(Firmware)
|
|
.order_by(Firmware.file_name.asc())
|
|
.where(
|
|
and_(
|
|
Firmware.platform_id == platform_id,
|
|
Firmware.file_name.not_in(fs_firmwares_to_keep),
|
|
)
|
|
)
|
|
) # type: ignore[attr-defined]
|
|
.unique()
|
|
.all()
|
|
)
|
|
session.execute(
|
|
update(Firmware)
|
|
.where(
|
|
and_(
|
|
Firmware.platform_id == platform_id,
|
|
Firmware.file_name.not_in(fs_firmwares_to_keep),
|
|
)
|
|
)
|
|
.values(**{"missing_from_fs": True})
|
|
.execution_options(synchronize_session="evaluate")
|
|
)
|
|
return missing_firmware
|