"""Drop the orphaned ``search_cache`` and ``cache`` tables.

Background
==========
Both tables were dead code:

* ``search_cache`` (model ``SearchCache``) was read/written only by the
  ``utilities/search_cache.py`` cache utility, removed in #4621 — its sole
  consumer, an experimental search strategy, had already been deleted in
  #4420, and ``get_or_fetch`` was never wired into the live search path.
* ``cache`` (model ``Cache``) was *dead on arrival*: introduced together
  with its tests but never imported, instantiated, or queried by any
  production code. The table existed only as a ``Base.metadata``
  side-effect and was always empty.

Both models have now been removed from ``database/models``, so fresh
databases no longer create either table. This migration drops them from
existing databases. Neither table held production data.

What this migration does
========================
Drops ``search_cache`` and ``cache`` if present. The ``has_table`` guard
makes it a no-op on fresh databases (created after the models were removed,
where the tables never existed) and on any database where they are already
absent. Removal tracked in #4620.

Revision ID: 0016
Revises: 0015
Create Date: 2026-06-18

Renumbered from 0015 to 0016: a separate 0015 (``0015_drop_document_notes``)
landed on main concurrently, so this migration now chains after it.
"""

from __future__ import annotations

from alembic import op
from loguru import logger
from sqlalchemy import inspect

revision = "0016"
down_revision = "0015"
branch_labels = None
depends_on = None

ORPHANED_TABLES = ("search_cache", "cache")


def upgrade() -> None:
    conn = op.get_bind()
    inspector = inspect(conn)

    for table in ORPHANED_TABLES:
        if inspector.has_table(table):
            op.drop_table(table)
            logger.info("Dropped orphaned '{}' table.", table)


def downgrade() -> None:
    """No-op.

    Both tables were orphaned dead code holding no data, and their models
    have been removed, so there is nothing meaningful to recreate. Mirrors
    the no-op downgrade in migration 0013.
    """
