"""add cache_hits column to translation_runs

Revision ID: dabc97097e0e
Revises: ed28d34edde7
Create Date: 2026-06-02 11:54:03.550164

"""
from collections.abc import Sequence

from alembic import op
import sqlalchemy as sa
from sqlalchemy import inspect


# revision identifiers, used by Alembic.
revision: str = 'dabc97097e0e'
down_revision: str | Sequence[str] | None = 'ed28d34edde7'
branch_labels: str | Sequence[str] | None = None
depends_on: str | Sequence[str] | None = None


def _table_exists(table_name: str) -> bool:
    conn = op.get_bind()
    inspector = inspect(conn)
    return table_name in inspector.get_table_names()


def upgrade() -> None:
    """Add cache_hits column to translation_runs table."""
    if not _table_exists("translation_runs"):
        return
    op.add_column("translation_runs", sa.Column(
        "cache_hits", sa.Integer(), nullable=False, server_default=sa.text("0"),
        comment="Number of rows served from translation cache",
    ))


def downgrade() -> None:
    """Remove cache_hits column from translation_runs table."""
    op.drop_column("translation_runs", "cache_hits")
