Files
payment-processing-test/migrations/versions/20260916_0001_create_payments_and_outbox.py
T

77 lines
3.0 KiB
Python

"""Create payments and outbox tables.
Revision ID: 20260916_0001
Revises:
Create Date: 2026-09-16 08:00:00
"""
from collections.abc import Sequence
import sqlalchemy as sa
from alembic import op
from sqlalchemy.dialects import postgresql
revision: str = "20260916_0001"
down_revision: str | None = None
branch_labels: str | Sequence[str] | None = None
depends_on: str | Sequence[str] | None = None
def upgrade() -> None:
currency = postgresql.ENUM("RUB", "USD", "EUR", name="currency", create_type=False)
payment_status = postgresql.ENUM(
"pending", "succeeded", "failed", name="payment_status", create_type=False
)
currency.create(op.get_bind(), checkfirst=True)
payment_status.create(op.get_bind(), checkfirst=True)
op.create_table(
"payments",
sa.Column("id", postgresql.UUID(as_uuid=True), nullable=False),
sa.Column("amount", sa.Numeric(precision=18, scale=2), nullable=False),
sa.Column("currency", currency, nullable=False),
sa.Column("description", sa.String(length=500), nullable=False),
sa.Column("metadata", postgresql.JSONB(astext_type=sa.Text()), nullable=False),
sa.Column("status", payment_status, nullable=False),
sa.Column("idempotency_key", sa.String(length=255), nullable=False),
sa.Column("webhook_url", sa.Text(), nullable=False),
sa.Column(
"created_at",
sa.DateTime(timezone=True),
server_default=sa.text("now()"),
nullable=False,
),
sa.Column("processed_at", sa.DateTime(timezone=True), nullable=True),
sa.Column("webhook_delivered_at", sa.DateTime(timezone=True), nullable=True),
sa.PrimaryKeyConstraint("id"),
sa.UniqueConstraint("idempotency_key"),
)
op.create_table(
"outbox",
sa.Column("id", postgresql.UUID(as_uuid=True), nullable=False),
sa.Column("aggregate_id", postgresql.UUID(as_uuid=True), nullable=False),
sa.Column("event_type", sa.String(length=100), nullable=False),
sa.Column("payload", postgresql.JSONB(astext_type=sa.Text()), nullable=False),
sa.Column("attempts", sa.Integer(), nullable=False),
sa.Column("last_error", sa.Text(), nullable=True),
sa.Column(
"created_at",
sa.DateTime(timezone=True),
server_default=sa.text("now()"),
nullable=False,
),
sa.Column("published_at", sa.DateTime(timezone=True), nullable=True),
sa.PrimaryKeyConstraint("id"),
)
op.create_index("ix_outbox_aggregate_id", "outbox", ["aggregate_id"])
op.create_index("ix_outbox_published_at", "outbox", ["published_at"])
def downgrade() -> None:
op.drop_index("ix_outbox_published_at", table_name="outbox")
op.drop_index("ix_outbox_aggregate_id", table_name="outbox")
op.drop_table("outbox")
op.drop_table("payments")
postgresql.ENUM(name="payment_status").drop(op.get_bind(), checkfirst=True)
postgresql.ENUM(name="currency").drop(op.get_bind(), checkfirst=True)