"""Add ap_delivery_log table for idempotent federation delivery

Revision ID: s9q7n3o5p6
Revises: r8p6m2n4o5
"""
from alembic import op
import sqlalchemy as sa

revision = "s9q7n3o5p6"
down_revision = "r8p6m2n4o5"
branch_labels = None
depends_on = None


def upgrade():
    op.create_table(
        "ap_delivery_log",
        sa.Column("id", sa.Integer, primary_key=True, autoincrement=True),
        sa.Column("activity_id", sa.Integer, sa.ForeignKey("ap_activities.id", ondelete="CASCADE"), nullable=False),
        sa.Column("inbox_url", sa.String(512), nullable=False),
        sa.Column("status_code", sa.Integer, nullable=True),
        sa.Column("delivered_at", sa.DateTime(timezone=True), nullable=False, server_default=sa.func.now()),
        sa.UniqueConstraint("activity_id", "inbox_url", name="uq_delivery_activity_inbox"),
    )
    op.create_index("ix_ap_delivery_activity", "ap_delivery_log", ["activity_id"])


def downgrade():
    op.drop_index("ix_ap_delivery_activity", table_name="ap_delivery_log")
    op.drop_table("ap_delivery_log")
