From ce5624b470bd5dd678fae0d097db15b6518395ea Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Tue, 14 Apr 2026 02:35:19 +0000 Subject: [PATCH 2/2] Add index on rom_files(rom_id) to fix PostgreSQL performance issue Agent-Logs-Url: https://github.com/rommapp/romm/sessions/7bb628cb-282d-44c3-b912-9c81212611a9 Co-authored-by: gantoine <3247106+gantoine@users.noreply.github.com> --- .../0079_add_rom_files_rom_id_index.py | 30 +++++++++++++++++++ backend/models/rom.py | 2 ++ 2 files changed, 32 insertions(+) create mode 100644 backend/alembic/versions/0079_add_rom_files_rom_id_index.py diff --git a/backend/alembic/versions/0079_add_rom_files_rom_id_index.py b/backend/alembic/versions/0079_add_rom_files_rom_id_index.py new file mode 100644 index 000000000..14613655b --- /dev/null +++ b/backend/alembic/versions/0079_add_rom_files_rom_id_index.py @@ -0,0 +1,30 @@ +"""Add index on rom_id for rom_files table + +Revision ID: 0079_add_rom_files_rom_id_index +Revises: 0078_add_libretro_id_to_roms +Create Date: 2026-04-14 00:00:00.000000 + +""" + +from alembic import op + +# revision identifiers, used by Alembic. +revision = "0079_add_rom_files_rom_id_index" +down_revision = "0078_add_libretro_id_to_roms" +branch_labels = None +depends_on = None + + +def upgrade() -> None: + with op.batch_alter_table("rom_files", schema=None) as batch_op: + batch_op.create_index( + "idx_rom_files_rom_id", + ["rom_id"], + unique=False, + if_not_exists=True, + ) + + +def downgrade() -> None: + with op.batch_alter_table("rom_files", schema=None) as batch_op: + batch_op.drop_index("idx_rom_files_rom_id", if_exists=True) diff --git a/backend/models/rom.py b/backend/models/rom.py index 9e8a67e93..88fd257d8 100644 --- a/backend/models/rom.py +++ b/backend/models/rom.py @@ -64,6 +64,8 @@ class SiblingRom(BaseModel): class RomFile(BaseModel): __tablename__ = "rom_files" + __table_args__ = (Index("idx_rom_files_rom_id", "rom_id"),) + id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True) rom_id: Mapped[int] = mapped_column(ForeignKey("roms.id", ondelete="CASCADE")) file_name: Mapped[str] = mapped_column(String(length=FILE_NAME_MAX_LENGTH))