from __future__ import annotations from datetime import datetime from sqlalchemy import DateTime, ForeignKey, Index, String, text from sqlalchemy.orm import Mapped, mapped_column from app.models.base import Base, TimestampMixin class TeamManagerHistory(Base, TimestampMixin): __tablename__ = "team_manager_history" __table_args__ = ( Index("ix_team_manager_history_team_time", "team_id", "started_at", "ended_at"), Index("ix_team_manager_history_manager_time", "manager_user_id", "started_at", "ended_at"), Index( "uq_team_manager_history_current", "team_id", unique=True, postgresql_where=text("ended_at IS NULL"), ), ) id: Mapped[str] = mapped_column(String(32), primary_key=True) team_id: Mapped[str] = mapped_column( String(32), ForeignKey("teams.id", ondelete="RESTRICT"), nullable=False ) manager_user_id: Mapped[str] = mapped_column( String(32), ForeignKey("users.id", ondelete="RESTRICT"), nullable=False ) started_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False) ended_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True)