"""SQLAlchemy column mixins for common column patterns.""" import uuid from datetime import datetime from sqlalchemy import DateTime, Uuid, text from sqlalchemy.orm import Mapped, mapped_column class UUIDMixin: """Mixin that adds a UUID primary key auto-generated by the database.""" id: Mapped[uuid.UUID] = mapped_column( Uuid, primary_key=True, server_default=text("gen_random_uuid()"), ) class UUIDv7Mixin: """Mixin that adds a UUIDv7 primary key auto-generated by the database.""" id: Mapped[uuid.UUID] = mapped_column( Uuid, primary_key=True, server_default=text("uuidv7()"), ) class CreatedAtMixin: """Mixin that adds a ``created_at`` timestamp column.""" created_at: Mapped[datetime] = mapped_column( DateTime(timezone=True), server_default=text("clock_timestamp()"), ) class UpdatedAtMixin: """Mixin that adds an ``updated_at`` timestamp column.""" updated_at: Mapped[datetime] = mapped_column( DateTime(timezone=True), server_default=text("clock_timestamp()"), onupdate=text("clock_timestamp()"), ) class TimestampMixin(CreatedAtMixin, UpdatedAtMixin): """Mixin that combines ``created_at`` and ``updated_at`` timestamp columns."""