"""Add composite (repo_id, name) index on musehub_branches. get_branch_head_commit_id() does a point lookup WHERE repo_id = ? AND name = ? on every push and fetch. Without a composite index this scans all branches for the repo. list_branches_with_detail() also benefits: the composite index covers both the WHERE clause and the ORDER BY name, eliminating an in-memory sort. Revision ID: 0057 Revises: 0056 """ from __future__ import annotations from alembic import op revision = "0057" down_revision = "0056" branch_labels = None depends_on = None def upgrade() -> None: op.create_index( "ix_musehub_branches_repo_name", "musehub_branches", ["repo_id", "name"], ) # The composite index on (repo_id, name) makes the single-column repo_id # index redundant — PostgreSQL can satisfy any repo_id-only scan using the # leftmost prefix of the composite index. op.drop_index("ix_musehub_branches_repo_id", table_name="musehub_branches") def downgrade() -> None: op.create_index( "ix_musehub_branches_repo_id", "musehub_branches", ["repo_id"], ) op.drop_index("ix_musehub_branches_repo_name", table_name="musehub_branches")