"""Add webhook secret to Forgejo repositories.

Revision ID: b6c7d8e9f0a1
Revises: a1b2c3d4e5f7
Create Date: 2026-05-19 12:00:00.000000

"""

from __future__ import annotations

import sqlalchemy as sa
from alembic import op

# revision identifiers, used by Alembic.
revision = "b6c7d8e9f0a1"
down_revision = "b2c3d4e5f6a7"
branch_labels = None
depends_on = None


def _has_column(table_name: str, column_name: str) -> bool:
    bind = op.get_bind()
    inspector = sa.inspect(bind)
    if not inspector.has_table(table_name):
        return False
    return any(column["name"] == column_name for column in inspector.get_columns(table_name))


def upgrade() -> None:
    """Store the shared secret used by Forgejo webhooks."""
    if not _has_column("forgejo_repositories", "webhook_secret"):
        op.add_column(
            "forgejo_repositories",
            sa.Column("webhook_secret", sa.String(), nullable=True),
        )


def downgrade() -> None:
    """Remove the Forgejo webhook shared secret column."""
    if _has_column("forgejo_repositories", "webhook_secret"):
        op.drop_column("forgejo_repositories", "webhook_secret")
