import uuid from sqlalchemy import ForeignKey, Text, UniqueConstraint from sqlalchemy.dialects.postgresql import UUID from sqlalchemy.orm import Mapped, mapped_column, relationship from api.models.base import Base, TimestampMixin, new_uuid class WorkstreamDependency(Base, TimestampMixin): """Directed dependency edge: `from_workstream` depends on `to_workstream`. Semantics: `to_workstream` must reach a satisfactory state before `from_workstream` can fully proceed. Hard deletes are intentional — removing an edge removes a constraint, not information. """ __tablename__ = "workstream_dependencies" __table_args__ = ( UniqueConstraint("from_workstream_id", "to_workstream_id", name="uq_ws_dep_pair"), ) id: Mapped[uuid.UUID] = mapped_column( UUID(as_uuid=True), primary_key=True, default=new_uuid ) from_workstream_id: Mapped[uuid.UUID] = mapped_column( UUID(as_uuid=True), ForeignKey("workstreams.id", ondelete="CASCADE"), nullable=False, index=True, ) to_workstream_id: Mapped[uuid.UUID] = mapped_column( UUID(as_uuid=True), ForeignKey("workstreams.id", ondelete="CASCADE"), nullable=False, index=True, ) description: Mapped[str | None] = mapped_column(Text, nullable=True) from_workstream: Mapped["Workstream"] = relationship( # noqa: F821 "Workstream", foreign_keys=[from_workstream_id] ) to_workstream: Mapped["Workstream"] = relationship( # noqa: F821 "Workstream", foreign_keys=[to_workstream_id] )