Files

69 lines
3.5 KiB
Python

"""ORM rows for the public-document library.
The binary files deliberately stay outside the database. A document row is
the durable library entry; every uploaded original, generated output and
format-repaired output is an immutable version row beneath it.
"""
from __future__ import annotations
from datetime import UTC, datetime
import sqlalchemy as sa
from sqlalchemy import Integer, String
from sqlalchemy.orm import Mapped, mapped_column
from deerflow.persistence.base import Base
from deerflow.persistence.types import BeijingDateTime, PortableLongText
class GongwenDocumentRow(Base):
__tablename__ = "gongwen_documents"
id: Mapped[str] = mapped_column(String(64), primary_key=True)
user_id: Mapped[str | None] = mapped_column(String(64), nullable=True, index=True)
title: Mapped[str] = mapped_column(String(256), nullable=False, default="")
document_type: Mapped[str] = mapped_column(String(48), nullable=False, default="notice", index=True)
# upload | generated. Keep source separate from status so the library can
# filter both axes independently.
source: Mapped[str] = mapped_column(String(24), nullable=False, default="upload", index=True)
# checking | review_required | verified | repaired | failed
status: Mapped[str] = mapped_column(String(32), nullable=False, default="checking", index=True)
template_key: Mapped[str | None] = mapped_column(String(96), nullable=True)
research_session_id: Mapped[str | None] = mapped_column(String(64), nullable=True, index=True)
original_version_id: Mapped[str | None] = mapped_column(String(64), nullable=True)
latest_version_id: Mapped[str | None] = mapped_column(String(64), nullable=True)
issue_counts: Mapped[str | None] = mapped_column(PortableLongText, nullable=True)
error: Mapped[str | None] = mapped_column(PortableLongText, nullable=True)
created_at: Mapped[datetime] = mapped_column(
BeijingDateTime(), nullable=False, default=lambda: datetime.now(UTC)
)
updated_at: Mapped[datetime] = mapped_column(
BeijingDateTime(), nullable=False, default=lambda: datetime.now(UTC), onupdate=lambda: datetime.now(UTC)
)
class GongwenDocumentVersionRow(Base):
__tablename__ = "gongwen_document_versions"
__table_args__ = (
sa.UniqueConstraint("document_id", "version_no", name="uq_gongwen_document_version_no"),
sa.Index("ix_gongwen_document_versions_document_created", "document_id", "created_at"),
)
id: Mapped[str] = mapped_column(String(64), primary_key=True)
document_id: Mapped[str] = mapped_column(String(64), nullable=False, index=True)
version_no: Mapped[int] = mapped_column(Integer, nullable=False)
# original | generated | repaired | markdown
kind: Mapped[str] = mapped_column(String(32), nullable=False)
file_name: Mapped[str] = mapped_column(String(512), nullable=False)
storage_key: Mapped[str] = mapped_column(String(1024), nullable=False)
content_type: Mapped[str] = mapped_column(String(128), nullable=False, default="application/octet-stream")
size_bytes: Mapped[int] = mapped_column(Integer, nullable=False, default=0)
sha256: Mapped[str] = mapped_column(String(64), nullable=False, default="")
# Exact review snapshot for this version. Later rule updates must not
# rewrite history or make past repair decisions impossible to explain.
review_snapshot: Mapped[str | None] = mapped_column(PortableLongText, nullable=True)
created_at: Mapped[datetime] = mapped_column(
BeijingDateTime(), nullable=False, default=lambda: datetime.now(UTC)
)