"""add is_admin column to roles table

Revision ID: 86c7b1d6a710
Revises: b0c1d2e3f4a5
Create Date: 2026-05-26 15:27:06.159151

"""
from collections.abc import Sequence

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

# revision identifiers, used by Alembic.
revision: str = '86c7b1d6a710'
down_revision: str | Sequence[str] | None = 'b0c1d2e3f4a5'
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 is_admin column to roles table, set True for existing Admin roles."""
    if not _table_exists("roles"):
        return
    op.add_column("roles", sa.Column("is_admin", sa.Boolean(), nullable=False, server_default=sa.text("false")))
    op.execute("UPDATE roles SET is_admin = true WHERE name = 'Admin'")


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