"""Transactional outbox model — ADR-006.""" from __future__ import annotations import uuid from datetime import datetime from sqlalchemy import DateTime, Index, Integer, String, func from sqlalchemy import Enum as SAEnum from sqlalchemy.orm import Mapped, mapped_column from sqlalchemy.types import JSON from app.core.database import Base from app.models.base import UUIDPrimaryKeyMixin from app.models.types import GUID from shared.events import EventStatus class OutboxEvent(UUIDPrimaryKeyMixin, Base): """Pending domain events written in the same DB transaction as mutations.""" __tablename__ = "outbox_events" tenant_id: Mapped[uuid.UUID] = mapped_column(GUID(), nullable=False) event_type: Mapped[str] = mapped_column(String(150), nullable=False) aggregate_type: Mapped[str] = mapped_column(String(100), nullable=False) aggregate_id: Mapped[str] = mapped_column(String(100), nullable=False) payload: Mapped[dict] = mapped_column(JSON, nullable=False, default=dict) status: Mapped[EventStatus] = mapped_column( SAEnum(EventStatus, name="healthcare_event_status", native_enum=False), default=EventStatus.PENDING, nullable=False, ) retry_count: Mapped[int] = mapped_column(Integer, default=0, nullable=False) created_at: Mapped[datetime] = mapped_column( DateTime(timezone=True), server_default=func.now(), nullable=False ) processed_at: Mapped[datetime | None] = mapped_column( DateTime(timezone=True), nullable=True ) __table_args__ = ( Index("ix_healthcare_outbox_status", "status"), Index("ix_healthcare_outbox_tenant", "tenant_id"), )