from datetime import datetime from sqlalchemy import Boolean, DateTime, ForeignKey, String, UniqueConstraint, func from sqlalchemy.orm import Mapped, mapped_column from app.database import Base class Organization(Base): __tablename__ = "organizations" id: Mapped[int] = mapped_column(primary_key=True) name: Mapped[str] = mapped_column(String(160), nullable=False) slug: Mapped[str] = mapped_column(String(180), unique=True, index=True, nullable=False) owner_id: Mapped[int] = mapped_column(ForeignKey("users.id"), index=True, nullable=False) created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), server_default=func.now(), nullable=False) class Workspace(Base): __tablename__ = "workspaces" id: Mapped[int] = mapped_column(primary_key=True) organization_id: Mapped[int] = mapped_column(ForeignKey("organizations.id"), index=True, nullable=False) name: Mapped[str] = mapped_column(String(160), nullable=False) slug: Mapped[str] = mapped_column(String(180), nullable=False) created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), server_default=func.now(), nullable=False) __table_args__ = (UniqueConstraint("organization_id", "slug", name="uq_workspace_org_slug"),) class WorkspaceMembership(Base): __tablename__ = "workspace_memberships" id: Mapped[int] = mapped_column(primary_key=True) workspace_id: Mapped[int] = mapped_column(ForeignKey("workspaces.id"), index=True, nullable=False) user_id: Mapped[int] = mapped_column(ForeignKey("users.id"), index=True, nullable=False) role: Mapped[str] = mapped_column(String(30), default="member", nullable=False) created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), server_default=func.now(), nullable=False) __table_args__ = (UniqueConstraint("workspace_id", "user_id", name="uq_workspace_member"),) class WorkspaceInvitation(Base): __tablename__ = "workspace_invitations" id: Mapped[int] = mapped_column(primary_key=True) workspace_id: Mapped[int] = mapped_column(ForeignKey("workspaces.id"), index=True, nullable=False) inviter_id: Mapped[int] = mapped_column(ForeignKey("users.id"), nullable=False) email: Mapped[str] = mapped_column(String(255), index=True, nullable=False) role: Mapped[str] = mapped_column(String(30), default="member", nullable=False) token: Mapped[str] = mapped_column(String(128), unique=True, index=True, nullable=False) accepted: Mapped[bool] = mapped_column(Boolean, default=False, nullable=False) expires_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False) created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), server_default=func.now(), nullable=False) class TenantAuditEvent(Base): __tablename__ = "tenant_audit_events" id: Mapped[int] = mapped_column(primary_key=True) organization_id: Mapped[int] = mapped_column(ForeignKey("organizations.id"), index=True, nullable=False) workspace_id: Mapped[int | None] = mapped_column(ForeignKey("workspaces.id"), index=True) actor_id: Mapped[int] = mapped_column(ForeignKey("users.id"), nullable=False) action: Mapped[str] = mapped_column(String(80), nullable=False) target: Mapped[str] = mapped_column(String(255), nullable=False) created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), server_default=func.now(), nullable=False)