"""add webhook idempotency constraint

Revision ID: 33b3a136ea29
Revises: 96997c4da432
Create Date: 2026-03-22 10:37:41.935617

"""

import sqlalchemy as sa
from alembic import op

# revision identifiers, used by Alembic.
revision = "33b3a136ea29"
down_revision = "96997c4da432"
branch_labels = None
depends_on = None


def upgrade() -> None:
  # ### commands auto generated by Alembic - please adjust! ###
  op.create_index(
    "idx_webhook_idempotency",
    "billing_audit_logs",
    [
      sa.literal_column("(event_data->>'event_id')"),
      sa.literal_column("(event_data->>'provider')"),
    ],
    unique=True,
    postgresql_where=sa.text("event_type = 'webhook_received'"),
  )
  # ### end Alembic commands ###


def downgrade() -> None:
  # ### commands auto generated by Alembic - please adjust! ###
  op.drop_index(
    "idx_webhook_idempotency",
    table_name="billing_audit_logs",
    postgresql_where=sa.text("event_type = 'webhook_received'"),
  )
  # ### end Alembic commands ###
