File size: 3,838 Bytes
1d92db8 af21975 1d92db8 31cf797 1d92db8 31cf797 1d92db8 af21975 31cf797 af21975 1d92db8 31cf797 1d92db8 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 | from __future__ import annotations
import enum
import uuid
from datetime import datetime
from sqlalchemy import (
DateTime,
Enum,
ForeignKey,
Integer,
String,
UniqueConstraint,
func,
)
from sqlalchemy.dialects.postgresql import UUID
from sqlalchemy.orm import Mapped, mapped_column, relationship
from app.database.database import Base
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from app.models.document_chunk import DocumentChunk
class DocumentVersionStatus(str, enum.Enum):
UPLOADED = "UPLOADED"
PROCESSING = "PROCESSING"
PROCESSED = "PROCESSED"
FAILED = "FAILED"
class DocumentVersion(Base):
"""
Represents a specific uploaded version of a document.
Each upload creates a new DocumentVersion while preserving
the logical Document identity.
"""
__tablename__ = "document_versions"
__table_args__ = (
UniqueConstraint(
"document_id",
"version_number",
name="uq_document_version",
),
)
id: Mapped[uuid.UUID] = mapped_column(
UUID(as_uuid=True),
primary_key=True,
default=uuid.uuid4,
)
document_id: Mapped[uuid.UUID] = mapped_column(
UUID(as_uuid=True),
ForeignKey("documents.id", ondelete="CASCADE"),
nullable=False,
index=True,
)
version_number: Mapped[int] = mapped_column(
Integer,
nullable=False,
index=True,
)
filename: Mapped[str] = mapped_column(
String(255),
nullable=False,
)
file_type: Mapped[str] = mapped_column(
String(20),
nullable=False,
)
checksum: Mapped[str] = mapped_column(
String(128),
nullable=False,
index=True,
)
storage_path: Mapped[str] = mapped_column(
String(1000),
nullable=False,
)
status: Mapped[DocumentVersionStatus] = mapped_column(
Enum(DocumentVersionStatus, name="document_version_status"),
nullable=False,
default=DocumentVersionStatus.UPLOADED,
server_default=DocumentVersionStatus.UPLOADED.value,
index=True,
)
uploaded_by: Mapped[uuid.UUID] = mapped_column(
UUID(as_uuid=True),
ForeignKey("users.id"),
nullable=False,
)
uploaded_at: Mapped[datetime] = mapped_column(
DateTime(timezone=True),
nullable=False,
server_default=func.now(),
)
processed_at: Mapped[datetime | None] = mapped_column(
DateTime(timezone=True),
nullable=True,
)
# ------------------------------------------------------------------
# Relationships
# ------------------------------------------------------------------
document: Mapped["Document"] = relationship(
back_populates="versions",
)
uploader: Mapped["User"] = relationship()
knowledge_items: Mapped[list["KnowledgeItem"]] = relationship(
back_populates="document_version",
cascade="all, delete-orphan",
passive_deletes=True,
)
evidence: Mapped[list["KnowledgeEvidence"]] = relationship(
back_populates="document_version",
cascade="all, delete-orphan",
passive_deletes=True,
)
chunks: Mapped[list["DocumentChunk"]] = relationship(
back_populates="document_version",
cascade="all, delete-orphan",
passive_deletes=True,
)
workflow_runs: Mapped[list["WorkflowRun"]] = relationship(
back_populates="document_version",
cascade="all, delete-orphan",
passive_deletes=True,
)
def __repr__(self) -> str:
return (
f"<DocumentVersion("
f"document={self.document_id}, "
f"version={self.version_number}, "
f"status='{self.status.value}')>"
) |