"""cms_pages: add subheading column

Revision ID: 0032
Revises: 0031
Create Date: 2026-06-29
"""
from alembic import op
import sqlalchemy as sa

revision = "0032"
down_revision = "0031"
branch_labels = None
depends_on = None


def upgrade() -> None:
    conn = op.get_bind()
    exists = conn.execute(sa.text(
        "SELECT 1 FROM information_schema.columns "
        "WHERE table_name='cms_pages' AND column_name='subheading'"
    )).fetchone()
    if not exists:
        op.add_column("cms_pages", sa.Column("subheading", sa.String(500), nullable=True))
    op.execute("UPDATE cms_pages SET subheading = '' WHERE subheading IS NULL")
    op.execute("UPDATE cms_pages SET subheading = 'Run the cemetery beautifully.' WHERE path = 'home'")
    op.alter_column("cms_pages", "subheading", existing_type=sa.String(500), nullable=False)


def downgrade() -> None:
    op.drop_column("cms_pages", "subheading")
