"""bookmark: saved locations

Adds the ``bookmark`` table backing the "bookmarked locations" feature — one row
per (user, grid cell), following the same shape as ``subscription``.

Conditional on purpose, like 0003_user_discord_only: revision 0001 builds the
schema from the *current* models.py via ``Base.metadata.create_all``, so a
database created after ``Bookmark`` was added to the ORM (fresh envs, most tests)
already has the table, and an unconditional ``create_table`` would fail there
with a duplicate-table error. Only a database stamped before this revision is
missing it.

Revision ID: 0004_bookmarks
Revises: 0003_user_discord_only
Create Date: 2026-07-26
"""
import sqlalchemy as sa
from alembic import op

revision = "0004_bookmarks"
down_revision = "0003_user_discord_only"
branch_labels = None
depends_on = None


def _has_table() -> bool:
    return sa.inspect(op.get_bind()).has_table("bookmark")


def upgrade() -> None:
    if _has_table():
        return
    # Built from the live ORM table (accounts.models.Bookmark) rather than
    # hand-duplicated column definitions, so this can never drift from models.py.
    from accounts.models import Bookmark

    Bookmark.__table__.create(bind=op.get_bind())


def downgrade() -> None:
    if not _has_table():
        return
    from accounts.models import Bookmark

    Bookmark.__table__.drop(bind=op.get_bind())
