seed_sop_cc_001_content.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #!/usr/bin/env python3
  2. """将 docs/sop-templates/SOP-CC-001-E01-01.html 写入工艺 SOP-CC-001-E01-01 的 sop_content。
  3. 生产环境请执行 docs/sql/2026-08-25-seed-sop-cc-001-content.sql,勿依赖本脚本。
  4. """
  5. from __future__ import annotations
  6. import sys
  7. from datetime import datetime
  8. from pathlib import Path
  9. ROOT = Path(__file__).resolve().parents[1]
  10. sys.path.insert(0, str(ROOT / "backend"))
  11. from sqlalchemy import create_engine, text # noqa: E402
  12. from app.core.config import settings # noqa: E402
  13. PROCESS_CODE = "SOP-CC-001-E01-01"
  14. HTML_PATH = ROOT / "docs" / "sop-templates" / "SOP-CC-001-E01-01.html"
  15. def main() -> None:
  16. html = HTML_PATH.read_text(encoding="utf-8")
  17. engine = create_engine(settings.DATABASE_URL)
  18. with engine.begin() as conn:
  19. result = conn.execute(
  20. text(
  21. """
  22. UPDATE biz_process
  23. SET sop_content = :html,
  24. update_time = :now
  25. WHERE process_code = :code
  26. AND del_flag = 0
  27. """
  28. ),
  29. {"html": html, "now": datetime.now(), "code": PROCESS_CODE},
  30. )
  31. if result.rowcount < 1:
  32. raise SystemExit(f"未找到工艺 process_code={PROCESS_CODE}")
  33. row = conn.execute(
  34. text(
  35. "SELECT id, process_name, LENGTH(sop_content) AS n "
  36. "FROM biz_process WHERE process_code=:code AND del_flag=0"
  37. ),
  38. {"code": PROCESS_CODE},
  39. ).fetchone()
  40. print(f"已更新 {PROCESS_CODE}({row[1]}),sop_content 长度 {row[2]}")
  41. if __name__ == "__main__":
  42. main()