"""iCal-Abo: users.ical_token_hash

Revision ID: 0041
Revises: 0040
Create Date: 2026-07-08

Gehashtes, rotierbares Token für den read-only iCal-Kalender-Feed pro Nutzer
(/absences/ical/<token>.ics). NULL = Feed deaktiviert. Keine RLS-Änderung
(nur neue nullable Spalte auf bestehender users-Policy).
"""
from alembic import op

revision = "0041"
down_revision = "0040"
branch_labels = None
depends_on = None


def upgrade() -> None:
    op.execute("ALTER TABLE users ADD COLUMN IF NOT EXISTS ical_token_hash TEXT")
    op.execute(
        "CREATE UNIQUE INDEX IF NOT EXISTS ix_users_ical_token_hash "
        "ON users (ical_token_hash) WHERE ical_token_hash IS NOT NULL"
    )


def downgrade() -> None:
    op.execute("DROP INDEX IF EXISTS ix_users_ical_token_hash")
    op.execute("ALTER TABLE users DROP COLUMN IF EXISTS ical_token_hash")
