feat: add document artifact model, migration, and storage root config

This commit is contained in:
Арсентьев Александр
2026-09-29 15:29:45 +03:00
parent 11758a33e9
commit adc92e3460
9 changed files with 1378 additions and 0 deletions
+39
View File
@@ -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]}")