feat: add document artifact model, migration, and storage root config
This commit is contained in:
@@ -0,0 +1,23 @@
|
||||
"""Application configuration.
|
||||
|
||||
Storage root for generated document artifacts. The default is anchored to the
|
||||
application package directory, not the process working directory.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
from pathlib import Path
|
||||
|
||||
# <app>/../storage — project-level storage directory.
|
||||
APP_DIR = Path(__file__).resolve().parent
|
||||
_DEFAULT_STORAGE_ROOT = APP_DIR.parent / "storage"
|
||||
|
||||
ENV_DOCUMENT_STORAGE_ROOT = "DOCUMENT_STORAGE_ROOT"
|
||||
|
||||
|
||||
def document_storage_root() -> Path:
|
||||
"""Return the document artifact storage root without creating it."""
|
||||
override = os.environ.get(ENV_DOCUMENT_STORAGE_ROOT, "").strip()
|
||||
if override:
|
||||
return Path(override).expanduser().resolve()
|
||||
return _DEFAULT_STORAGE_ROOT.resolve()
|
||||
@@ -11,6 +11,7 @@ KS2_REPORTING_PERIOD_MIGRATION = "20260925_ks2_reporting_period"
|
||||
PAYMENT_STATUS_MIGRATION = "20260926_payment_status"
|
||||
KS3_NUMBER_UNIQUE_MIGRATION = "20260927_ks3_number_unique"
|
||||
BOQ_DOCUMENT_REQUISITES_MIGRATION = "20260928_boq_document_requisites"
|
||||
DOCUMENT_ARTIFACTS_MIGRATION = "20260928_document_artifacts"
|
||||
|
||||
|
||||
def run_migrations(engine):
|
||||
@@ -209,6 +210,44 @@ def run_migrations(engine):
|
||||
connection.execute("ALTER TABLE boq_documents ADD COLUMN contract VARCHAR")
|
||||
connection.execute("INSERT INTO schema_migrations(version, applied_at) VALUES (?, ?)",
|
||||
(BOQ_DOCUMENT_REQUISITES_MIGRATION, datetime.now(timezone.utc).isoformat()))
|
||||
if DOCUMENT_ARTIFACTS_MIGRATION not in applied:
|
||||
connection.execute("""CREATE TABLE IF NOT EXISTS document_artifacts (
|
||||
id INTEGER NOT NULL PRIMARY KEY,
|
||||
project_id INTEGER NOT NULL REFERENCES projects(id),
|
||||
document_type VARCHAR NOT NULL,
|
||||
document_id INTEGER NOT NULL,
|
||||
format VARCHAR NOT NULL,
|
||||
storage_key VARCHAR NOT NULL,
|
||||
template_type VARCHAR NOT NULL,
|
||||
template_version INTEGER NOT NULL,
|
||||
content_hash VARCHAR(64) NOT NULL,
|
||||
input_hash VARCHAR(64) NOT NULL,
|
||||
is_current BOOLEAN NOT NULL DEFAULT 1,
|
||||
generated_at DATETIME NOT NULL,
|
||||
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
|
||||
)""")
|
||||
connection.execute(
|
||||
"CREATE INDEX IF NOT EXISTS ix_document_artifacts_id ON document_artifacts (id)"
|
||||
)
|
||||
connection.execute(
|
||||
"CREATE INDEX IF NOT EXISTS ix_document_artifacts_project_id ON document_artifacts (project_id)"
|
||||
)
|
||||
connection.execute(
|
||||
"CREATE INDEX IF NOT EXISTS ix_document_artifacts_type_doc "
|
||||
"ON document_artifacts (document_type, document_id)"
|
||||
)
|
||||
connection.execute(
|
||||
"CREATE INDEX IF NOT EXISTS ix_document_artifacts_current "
|
||||
"ON document_artifacts (document_type, document_id, is_current)"
|
||||
)
|
||||
# Partial unique index: one current artifact per (type, id, format).
|
||||
connection.execute(
|
||||
"CREATE UNIQUE INDEX IF NOT EXISTS uq_document_artifact_current "
|
||||
"ON document_artifacts (document_type, document_id, format) WHERE is_current = 1"
|
||||
)
|
||||
connection.execute("INSERT INTO schema_migrations(version, applied_at) VALUES (?, ?)",
|
||||
(DOCUMENT_ARTIFACTS_MIGRATION, datetime.now(timezone.utc).isoformat()))
|
||||
violations = connection.execute("PRAGMA foreign_key_check").fetchall()
|
||||
if violations:
|
||||
raise RuntimeError(f"Foreign key violations after agreement migration: {violations[:10]}")
|
||||
|
||||
@@ -15,6 +15,7 @@ from app.models import agreement as agreement_models
|
||||
from app.models import ks2 as ks2_models
|
||||
from app.models import ks3 as ks3_models
|
||||
from app.models import payment as payment_models
|
||||
from app.models import document_artifact as document_artifact_models
|
||||
|
||||
# Initialize database
|
||||
from app.database.database import engine
|
||||
|
||||
@@ -0,0 +1,71 @@
|
||||
"""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"<DocumentArtifact(id={self.id}, type={self.document_type!r}, "
|
||||
f"doc={self.document_id}, format={self.format!r}, current={self.is_current})>"
|
||||
)
|
||||
Reference in New Issue
Block a user