Files
romm/backend/models/channel.py
Stu Anderson 355ce188a4 perf(rq): Save 70MB+ RAM; keep SQLAlchemy out of RQ workers, cron processes (#5736)
* perf(rq): keep SQLAlchemy out of the RQ worker and cron processes

The RQ worker parents and rq cron imported SQLAlchemy and the ORM models at
start-up, through config/__init__.py (safe_* helpers in utils.database) and
config_manager (sqlalchemy.URL and the rom-converto adapter). None of them
query the database themselves; each job's work horse loads what it needs.

- Move safe_int, safe_str_to_bool and get_nested from utils.database to utils.
- Import sqlalchemy.URL inside get_db_engine.
- Import the rom-converto normalizer only when converto.platform_formats is set.
- Add a test that tasks.cron_config and handler.rq_worker start without
  loading SQLAlchemy.
- Run a killed work horse's failure callback and exception handlers in a
  short-lived child, so the job code they import never stays in the parent.

Measured on the production image: idle PSS of rq_worker, rq_scan_worker and
rq_cron fell by 98 MB in total.

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

* perf(rq): move to constants file, tests prevent huge imports

* perf(rq) revert forked failure report.

* pr-ready: code-review

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

* pr-ready: simplify

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

* pr-ready: review-polish

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

* pr-ready: plain-prose

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

* refactor(db): build the database URL in utils.database

ConfigManager.get_db_engine becomes utils.database.get_db_url, so
config_manager no longer needs SQLAlchemy at all and the function-local
import goes away. Every caller already loads SQLAlchemy.

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

* fix(roms): clear a match id that is not a number instead of storing 0

safe_int_or_none fell back to safe_int's default of 0, so sgdb_id,
hasheous_id and tgdb_id (which have no refetch to reset them) were saved
as 0. Use utils.int_or_none instead.

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

* docs(models): describe where each shared column limit is used

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-11-28 12:08:54 -05:00

60 lines
2.2 KiB
Python

from __future__ import annotations
import uuid
from sqlalchemy import BigInteger, ForeignKey, Index, String, Uuid
from sqlalchemy.orm import Mapped, mapped_column
from models.assets import SAVE_SLOT_MAX_LENGTH
from models.base import FILE_NAME_MAX_LENGTH, BaseModel
from models.limits import ROM_SHA1_MAX_LENGTH
# A legacy slot becomes a channel label and back, so they share a limit.
CHANNEL_LABEL_MAX_LENGTH = SAVE_SLOT_MAX_LENGTH
DEFAULT_CHANNEL_LABEL = "default"
class Channel(BaseModel):
"""A playthrough of one ROM file: a mutable pointer to its current snapshot."""
__tablename__ = "channels"
__table_args__ = (
Index("ix_channels_user_rom", "user_id", "rom_id"),
Index("ix_channels_rom_id", "rom_id"),
Index("ix_channels_platform_file_hash", "platform_id", "target_file_hash"),
Index(
"ix_channels_platform_file_name",
"platform_id",
"target_file_name",
"target_file_size",
),
Index("ix_channels_current_snapshot_id", "current_snapshot_id"),
{"extend_existing": True},
)
id: Mapped[uuid.UUID] = mapped_column(Uuid(), primary_key=True, default=uuid.uuid7)
user_id: Mapped[int] = mapped_column(ForeignKey("users.id", ondelete="CASCADE"))
rom_id: Mapped[int | None] = mapped_column(
ForeignKey("roms.id", ondelete="SET NULL"), default=None
)
platform_id: Mapped[int | None] = mapped_column(
ForeignKey("platforms.id", ondelete="SET NULL"), default=None
)
target_file_hash: Mapped[str | None] = mapped_column(
String(length=ROM_SHA1_MAX_LENGTH), default=None
)
target_file_name: Mapped[str] = mapped_column(String(length=FILE_NAME_MAX_LENGTH))
target_file_size: Mapped[int] = mapped_column(BigInteger())
label: Mapped[str] = mapped_column(String(length=CHANNEL_LABEL_MAX_LENGTH))
current_snapshot_id: Mapped[int | None] = mapped_column(
ForeignKey(
"snapshots.id",
ondelete="SET NULL",
use_alter=True,
name="fk_channels_current_snapshot_id",
),
default=None,
)
is_hardcore: Mapped[bool] = mapped_column(default=False)
is_public: Mapped[bool] = mapped_column(default=False)