41 lines
1.5 KiB
Python
41 lines
1.5 KiB
Python
|
|
"""Add consumer_inbox table for outbox idempotency.
|
||
|
|
|
||
|
|
Revision ID: 0065
|
||
|
|
Revises: 0064
|
||
|
|
Create Date: 2026-07-29
|
||
|
|
|
||
|
|
Without idempotency, a worker crash between sending an email/webhook
|
||
|
|
and marking the event as published can lead to duplicate deliveries.
|
||
|
|
|
||
|
|
This migration creates a consumer_inbox table that tracks which
|
||
|
|
consumers have already processed which events.
|
||
|
|
"""
|
||
|
|
|
||
|
|
from alembic import op
|
||
|
|
import sqlalchemy as sa
|
||
|
|
from sqlalchemy.dialects.postgresql import UUID
|
||
|
|
|
||
|
|
revision = "0065"
|
||
|
|
down_revision = "0064"
|
||
|
|
branch_labels = None
|
||
|
|
depends_on = None
|
||
|
|
|
||
|
|
|
||
|
|
def upgrade() -> None:
|
||
|
|
op.create_table(
|
||
|
|
"consumer_inbox",
|
||
|
|
sa.Column("id", UUID(as_uuid=True), primary_key=True, server_default=sa.text("gen_random_uuid()")),
|
||
|
|
sa.Column("event_id", UUID(as_uuid=True), sa.ForeignKey("event_outbox.id", ondelete="CASCADE"), nullable=False, index=True),
|
||
|
|
sa.Column("consumer_name", sa.String(100), nullable=False, index=True),
|
||
|
|
sa.Column("status", sa.String(20), nullable=False, default="pending"), # pending, processed, failed
|
||
|
|
sa.Column("processed_at", sa.DateTime(timezone=True), nullable=True),
|
||
|
|
sa.Column("error_message", sa.Text, nullable=True),
|
||
|
|
sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.text("NOW()"), nullable=False),
|
||
|
|
sa.UniqueConstraint("event_id", "consumer_name", name="uq_consumer_inbox_event_consumer"),
|
||
|
|
)
|
||
|
|
op.execute("ALTER TABLE consumer_inbox ENABLE ROW LEVEL SECURITY")
|
||
|
|
|
||
|
|
|
||
|
|
def downgrade() -> None:
|
||
|
|
op.drop_table("consumer_inbox")
|