Deployment Script, Postgres migration, UX improvements

This commit is contained in:
2026-05-08 23:07:01 +12:00
parent 9afc3170ff
commit cfc193b713
37 changed files with 4390 additions and 2715 deletions
+52 -6
View File
@@ -3,6 +3,7 @@ from __future__ import annotations
from collections import Counter
from datetime import date, datetime
import logging
import os
from pathlib import Path
import re
@@ -22,10 +23,48 @@ from app.services.client_access_service import MODULE_CATALOG, default_access_le
TENANT_ID = "hunter-premium-produce"
WORKBOOK_EFFECTIVE_DATE = date(2025, 9, 1)
WORKBOOK_SENTINEL_ITEM_ID = "404266"
WORKBOOK_PATH = Path(__file__).resolve().parents[2] / "Input Cost Spreadsheet(1).xlsx"
WORKBOOK_FILENAME = "Input Cost Spreadsheet(1).xlsx"
logger = logging.getLogger("data_entry_app.seed")
def _workbook_candidates() -> list[Path]:
env_value = os.getenv("WORKBOOK_PATH")
env_path = env_value.strip() if isinstance(env_value, str) and env_value.strip() else None
repo_root = Path(__file__).resolve().parents[2]
cwd = Path.cwd()
candidates = [
Path(env_path) if env_path else None,
Path("/srv/lean101-clients") / WORKBOOK_FILENAME,
repo_root / WORKBOOK_FILENAME,
cwd / WORKBOOK_FILENAME,
Path("/app") / WORKBOOK_FILENAME,
Path("/") / WORKBOOK_FILENAME,
]
ordered: list[Path] = []
seen: set[str] = set()
for candidate in candidates:
if candidate is None:
continue
key = str(candidate)
if key in seen:
continue
seen.add(key)
ordered.append(candidate)
return ordered
def _resolve_workbook_path() -> Path:
for candidate in _workbook_candidates():
if candidate.exists():
return candidate
return _workbook_candidates()[0]
WORKBOOK_PATH = _resolve_workbook_path()
def _text(value) -> str | None:
if value is None:
return None
@@ -129,9 +168,12 @@ def _build_process_key(label, grading_cost: float, bagging_cost: float, cracking
def _load_workbook():
if not WORKBOOK_PATH.exists():
raise FileNotFoundError(f"Workbook not found at {WORKBOOK_PATH}")
return load_workbook(WORKBOOK_PATH, data_only=True)
workbook_path = _resolve_workbook_path()
if not workbook_path.exists():
raise FileNotFoundError(
f"Workbook not found. Checked: {', '.join(str(path) for path in _workbook_candidates())}"
)
return load_workbook(workbook_path, data_only=True)
def _read_raw_material_rows(workbook) -> list[dict]:
@@ -684,10 +726,14 @@ def seed_costing_workspace(db):
def seed_if_empty():
Base.metadata.create_all(bind=engine)
with SessionLocal() as db:
if WORKBOOK_PATH.exists():
workbook_path = _resolve_workbook_path()
if workbook_path.exists():
seed_costing_workspace(db)
else:
logger.warning("Skipping costing workspace seed because workbook is missing at %s", WORKBOOK_PATH)
logger.warning(
"Skipping costing workspace seed because workbook is missing. Checked: %s",
", ".join(str(path) for path in _workbook_candidates()),
)
seed_client_access(db)
seed_access(db)
db.commit()