"""Document artifact metadata. Printed document files are stored outside SQLite. This table keeps metadata about generated artifacts (type, storage key, template version, hashes, currency flag). Binary content is never stored here. """ from sqlalchemy import ( Boolean, Column, DateTime, ForeignKey, Integer, String, Index, text, ) from sqlalchemy.orm import relationship from sqlalchemy.sql import func from app.database.database import Base class DocumentArtifact(Base): __tablename__ = "document_artifacts" __table_args__ = ( Index("ix_document_artifacts_type_doc", "document_type", "document_id"), Index("ix_document_artifacts_project_id", "project_id"), Index( "ix_document_artifacts_current", "document_type", "document_id", "is_current", ), # At most one current artifact per (document_type, document_id, format). # Partial unique index: history rows (is_current = 0) may repeat freely. Index( "uq_document_artifact_current", "document_type", "document_id", "format", unique=True, sqlite_where=text("is_current = 1"), ), ) id = Column(Integer, primary_key=True, index=True) project_id = Column(Integer, ForeignKey("projects.id"), nullable=False) # Polymorphic link to the source document (ks2 / ks3 / ...). document_type = Column(String, nullable=False) document_id = Column(Integer, nullable=False) format = Column(String, nullable=False) # Relative path under DOCUMENT_STORAGE_ROOT. Never an absolute path or URL. storage_key = Column(String, nullable=False) template_type = Column(String, nullable=False) template_version = Column(Integer, nullable=False) content_hash = Column(String(64), nullable=False) input_hash = Column(String(64), nullable=False) is_current = Column(Boolean, nullable=False, default=True, server_default="1") generated_at = Column(DateTime, nullable=False) created_at = Column(DateTime, nullable=False, server_default=func.now()) updated_at = Column( DateTime, nullable=False, server_default=func.now(), onupdate=func.now() ) project = relationship("Project") def __repr__(self): return ( f"" )