import uuid from datetime import datetime, timezone from sqlalchemy import ( Boolean, DateTime, ForeignKey, LargeBinary, String, Text, UniqueConstraint ) from sqlalchemy.dialects.postgresql import UUID from sqlalchemy.orm import Mapped, mapped_column, relationship from api.db import Base def _now() -> datetime: return datetime.now(timezone.utc) def _uuid() -> uuid.UUID: return uuid.uuid4() class User(Base): __tablename__ = "users" id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), primary_key=True, default=_uuid) email: Mapped[str] = mapped_column(String(255), unique=True, nullable=False, index=True) hashed_password: Mapped[str] = mapped_column(String(255), nullable=False) role: Mapped[str] = mapped_column(String(20), nullable=False, default="viewer") is_active: Mapped[bool] = mapped_column(Boolean, default=True, nullable=False) created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=_now) sessions: Mapped[list["ChatSession"]] = relationship(back_populates="user") rbac_grants: Mapped[list["RbacGrant"]] = relationship( back_populates="user", foreign_keys="RbacGrant.user_id" ) class Dataset(Base): __tablename__ = "datasets" id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), primary_key=True, default=_uuid) name: Mapped[str] = mapped_column(String(255), nullable=False) description: Mapped[str | None] = mapped_column(Text) # Ollama model name registered for this dataset (e.g. "tux-ai-mydata") model_name: Mapped[str | None] = mapped_column(String(255)) # True once a dump.rdb has been imported into Redis for this dataset rdb_imported: Mapped[bool] = mapped_column(Boolean, default=False, nullable=False) created_by: Mapped[uuid.UUID | None] = mapped_column( UUID(as_uuid=True), ForeignKey("users.id", ondelete="SET NULL") ) created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=_now) encryption_key: Mapped["EncryptionKey | None"] = relationship( back_populates="dataset", uselist=False ) rbac_grants: Mapped[list["RbacGrant"]] = relationship(back_populates="dataset") class EncryptionKey(Base): """ Stores the dataset AES key encrypted with the server's MASTER_KEY. The plaintext AES key never touches the database. """ __tablename__ = "encryption_keys" id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), primary_key=True, default=_uuid) dataset_id: Mapped[uuid.UUID] = mapped_column( UUID(as_uuid=True), ForeignKey("datasets.id", ondelete="CASCADE"), unique=True ) encrypted_key: Mapped[bytes] = mapped_column(LargeBinary, nullable=False) key_ref: Mapped[str] = mapped_column(String(24), nullable=False) created_by: Mapped[uuid.UUID | None] = mapped_column( UUID(as_uuid=True), ForeignKey("users.id", ondelete="SET NULL") ) created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=_now) dataset: Mapped["Dataset"] = relationship(back_populates="encryption_key") class RbacGrant(Base): """ Grants a specific user permission to see a specific entity type on a dataset. entity_type='*' means all types (used for Admin role auto-grants). """ __tablename__ = "rbac_grants" __table_args__ = ( UniqueConstraint("user_id", "dataset_id", "entity_type", name="uq_rbac_grant"), ) id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), primary_key=True, default=_uuid) user_id: Mapped[uuid.UUID] = mapped_column( UUID(as_uuid=True), ForeignKey("users.id", ondelete="CASCADE"), index=True ) dataset_id: Mapped[uuid.UUID] = mapped_column( UUID(as_uuid=True), ForeignKey("datasets.id", ondelete="CASCADE"), index=True ) entity_type: Mapped[str] = mapped_column(String(50), nullable=False) granted_by: Mapped[uuid.UUID | None] = mapped_column( UUID(as_uuid=True), ForeignKey("users.id", ondelete="SET NULL") ) created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=_now) user: Mapped["User"] = relationship(back_populates="rbac_grants", foreign_keys=[user_id]) dataset: Mapped["Dataset"] = relationship(back_populates="rbac_grants") class ChatSession(Base): __tablename__ = "chat_sessions" id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), primary_key=True, default=_uuid) user_id: Mapped[uuid.UUID] = mapped_column( UUID(as_uuid=True), ForeignKey("users.id", ondelete="CASCADE"), index=True ) dataset_id: Mapped[uuid.UUID | None] = mapped_column( UUID(as_uuid=True), ForeignKey("datasets.id", ondelete="SET NULL") ) title: Mapped[str | None] = mapped_column(String(255)) created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=_now) updated_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=_now, onupdate=_now) user: Mapped["User"] = relationship(back_populates="sessions") messages: Mapped[list["Message"]] = relationship( back_populates="session", order_by="Message.created_at" ) class Message(Base): __tablename__ = "messages" id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), primary_key=True, default=_uuid) session_id: Mapped[uuid.UUID] = mapped_column( UUID(as_uuid=True), ForeignKey("chat_sessions.id", ondelete="CASCADE"), index=True ) role: Mapped[str] = mapped_column(String(20), nullable=False) # 'user' | 'assistant' # Stored tokenized — plaintext PII is never persisted. content: Mapped[str] = mapped_column(Text, nullable=False) created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=_now) session: Mapped["ChatSession"] = relationship(back_populates="messages") class AuditLog(Base): __tablename__ = "audit_log" id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), primary_key=True, default=_uuid) user_id: Mapped[uuid.UUID | None] = mapped_column( UUID(as_uuid=True), ForeignKey("users.id", ondelete="SET NULL") ) entity_type: Mapped[str] = mapped_column(String(50), nullable=False) token: Mapped[str] = mapped_column(String(100), nullable=False) dataset_id: Mapped[uuid.UUID | None] = mapped_column( UUID(as_uuid=True), ForeignKey("datasets.id", ondelete="SET NULL") ) action: Mapped[str] = mapped_column(String(50), nullable=False, default="decrypt") created_at: Mapped[datetime] = mapped_column( DateTime(timezone=True), default=_now, index=True )