"""Add notificationtypes.boardcategory (shared heading on the shopfloor board).

The board grouped cards by display style alone, so every type set to grid landed
inside the Recertification row and every carousel type inside Recognition's,
under a heading that named someone else's type. Each type now gets a row of its
own; this column is the opt-in for the other direction - several types that
belong together (Change, Awareness, Incident) share one row under a category
name instead of taking three.

Idempotent; downgrade drops the column.

Revision ID: notifications0004category
Revises: notifications0003grace
"""
from alembic import op
import sqlalchemy as sa


revision = 'notifications0004category'
down_revision = 'notifications0003grace'
branch_labels = None
depends_on = None

_TABLE = 'notificationtypes'
_COLUMN = 'boardcategory'


def _column_names(insp, table):
    return {c['name'] for c in insp.get_columns(table)}


def upgrade():
    bind = op.get_bind()
    insp = sa.inspect(bind)
    if _TABLE not in insp.get_table_names():
        return
    if _COLUMN not in _column_names(insp, _TABLE):
        op.add_column(_TABLE, sa.Column(_COLUMN, sa.String(50), nullable=True))


def downgrade():
    bind = op.get_bind()
    insp = sa.inspect(bind)
    if _TABLE not in insp.get_table_names():
        return
    if _COLUMN in _column_names(insp, _TABLE):
        op.drop_column(_TABLE, _COLUMN)
