"""Add OAuth/OIDC fields to users table."""

from alembic import op

revision = "0015"
down_revision = "0014"


def upgrade() -> None:
    op.execute("ALTER TABLE users ADD COLUMN IF NOT EXISTS oauth_sub TEXT")
    op.execute(
        "CREATE UNIQUE INDEX IF NOT EXISTS ix_users_oauth_sub ON users(oauth_sub)"
    )
    op.execute("ALTER TABLE users ALTER COLUMN password_hash DROP NOT NULL")


def downgrade() -> None:
    op.execute("ALTER TABLE users ALTER COLUMN password_hash SET NOT NULL")
    op.execute("DROP INDEX IF EXISTS ix_users_oauth_sub")
    op.execute("ALTER TABLE users DROP COLUMN IF EXISTS oauth_sub")
