from __future__ import annotations from datetime import datetime from uuid import UUID from sqlalchemy import BigInteger, DateTime, ForeignKey, Index, Integer, JSON, String, UniqueConstraint from sqlalchemy.orm import Mapped, mapped_column from sqlalchemy.types import Uuid from ..db.postgres import Base class AttachmentJobRow(Base): __tablename__ = 'attachment_jobs' __table_args__ = ( UniqueConstraint( 'attachment_id', 'pipeline_version', name='attachment_job_attachment_version_key' ), Index('attachment_jobs_lease_idx', 'state', 'lease_until'), ) id: Mapped[UUID] = mapped_column(Uuid, primary_key=True) attachment_id: Mapped[UUID] = mapped_column( ForeignKey('attachments.id', ondelete='CASCADE'), nullable=False ) pipeline_version: Mapped[int] = mapped_column(Integer, nullable=False) state: Mapped[str] = mapped_column(String(16), nullable=False) attempt: Mapped[int] = mapped_column(Integer, nullable=False) max_attempts: Mapped[int] = mapped_column(Integer, nullable=False) next_attempt_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False) lease_owner: Mapped[str | None] = mapped_column(String(128), nullable=True) lease_until: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True) last_error_code: Mapped[str | None] = mapped_column(String(64), nullable=True) replay_count: Mapped[int] = mapped_column(Integer, nullable=False, default=0) created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False) updated_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False) class OutboxEventRow(Base): __tablename__ = 'outbox_events' __table_args__ = (Index('outbox_events_pending_idx', 'published_at', 'available_at'),) id: Mapped[UUID] = mapped_column(Uuid, primary_key=True) event_type: Mapped[str] = mapped_column(String(64), nullable=False) job_id: Mapped[UUID] = mapped_column( ForeignKey('attachment_jobs.id', ondelete='CASCADE'), nullable=False ) payload: Mapped[dict] = mapped_column(JSON, nullable=False) available_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False) published_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True) claim_owner: Mapped[str | None] = mapped_column(String(128), nullable=True) claim_until: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True) publish_attempts: Mapped[int] = mapped_column(Integer, nullable=False, default=0) last_error_code: Mapped[str | None] = mapped_column(String(64), nullable=True) created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False) updated_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False) class JobEventRow(Base): __tablename__ = 'job_events' __table_args__ = (Index('job_events_job_created_idx', 'job_id', 'created_at'),) id: Mapped[int] = mapped_column(BigInteger, primary_key=True, autoincrement=True) job_id: Mapped[UUID] = mapped_column( ForeignKey('attachment_jobs.id', ondelete='CASCADE'), nullable=False ) kind: Mapped[str] = mapped_column(String(32), nullable=False) attempt: Mapped[int] = mapped_column(Integer, nullable=False) error_code: Mapped[str | None] = mapped_column(String(64), nullable=True) created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False)