fix(persistence): persist surgical procedures and structured oncology in database layer

This commit is contained in:
Abhinav Gupta
2026-10-09 11:51:30 +05:30
parent 7885abf57d
commit 211a1bc480
5 changed files with 112 additions and 2 deletions
+12 -1
View File
@@ -104,7 +104,7 @@ def init_db(settings: Settings | None = None) -> None:
engine = get_engine(settings=settings)
Base.metadata.create_all(bind=engine)
# Safely migrate new columns to diagnosis_records if it was created under previous schema
# Safely migrate new columns to diagnosis_records and processed_documents if created under previous schema
try:
inspector = inspect(engine)
if "diagnosis_records" in inspector.get_table_names():
@@ -117,6 +117,17 @@ def init_db(settings: Settings | None = None) -> None:
if "cpt" not in columns:
conn.execute(text("ALTER TABLE diagnosis_records ADD COLUMN cpt VARCHAR(32);"))
conn.commit()
if "processed_documents" in inspector.get_table_names():
doc_columns = {col["name"] for col in inspector.get_columns("processed_documents")}
with engine.connect() as conn:
if "procedures_json" not in doc_columns:
conn.execute(text("ALTER TABLE processed_documents ADD COLUMN procedures_json TEXT DEFAULT '[]';"))
if "operative_findings_json" not in doc_columns:
conn.execute(text("ALTER TABLE processed_documents ADD COLUMN operative_findings_json TEXT DEFAULT '[]';"))
if "oncology_context_json" not in doc_columns:
conn.execute(text("ALTER TABLE processed_documents ADD COLUMN oncology_context_json TEXT DEFAULT '{}';"))
conn.commit()
except Exception as exc:
logger.warning("Database column migration check notice: %s", exc)
+29
View File
@@ -47,6 +47,11 @@ class ProcessedDocument(Base):
abstention_count: Mapped[int] = mapped_column(Integer, default=0, nullable=False)
processing_time_ms: Mapped[float] = mapped_column(Float, default=0.0, nullable=False)
# Surgical Oncology & Procedures
procedures_json: Mapped[str] = mapped_column(Text, default="[]", nullable=False)
operative_findings_json: Mapped[str] = mapped_column(Text, default="[]", nullable=False)
oncology_context_json: Mapped[str] = mapped_column(Text, default="{}", nullable=False)
# Metadata & Auditing
metadata_json: Mapped[str] = mapped_column(Text, default="{}", nullable=False)
created_at: Mapped[datetime] = mapped_column(
@@ -85,6 +90,27 @@ class ProcessedDocument(Base):
except Exception:
pass
procedures = []
if getattr(self, "procedures_json", None):
try:
procedures = json.loads(self.procedures_json)
except Exception:
procedures = []
operative_findings = []
if getattr(self, "operative_findings_json", None):
try:
operative_findings = json.loads(self.operative_findings_json)
except Exception:
operative_findings = []
oncology_context = {}
if getattr(self, "oncology_context_json", None):
try:
oncology_context = json.loads(self.oncology_context_json)
except Exception:
oncology_context = {}
return {
"id": self.id,
"document_id": self.document_id,
@@ -101,6 +127,9 @@ class ProcessedDocument(Base):
"primary_confidence": self.primary_confidence,
"secondary_count": self.secondary_count,
"abstention_count": self.abstention_count,
"procedures": procedures,
"operative_findings": operative_findings,
"oncology_context": oncology_context,
"processing_time_ms": self.processing_time_ms,
"metadata": meta,
"created_at": self.created_at.isoformat() if self.created_at else None,
+14
View File
@@ -81,6 +81,13 @@ class MedicalCodingRepository:
doc.abstention_count = len(result.abstentions)
doc.processing_time_ms = result.processing_time_ms
doc.metadata_json = json.dumps(meta_dict)
doc.procedures_json = json.dumps(result.procedures) if getattr(result, "procedures", None) else "[]"
doc.operative_findings_json = (
json.dumps(result.operative_findings) if getattr(result, "operative_findings", None) else "[]"
)
doc.oncology_context_json = (
json.dumps(result.oncology_context) if getattr(result, "oncology_context", None) else "{}"
)
# Clear previous diagnoses, abstentions, checks for clean re-save
doc.diagnoses.clear()
@@ -106,6 +113,13 @@ class MedicalCodingRepository:
),
secondary_count=len(result.secondary_diagnoses),
abstention_count=len(result.abstentions),
procedures_json=json.dumps(result.procedures) if getattr(result, "procedures", None) else "[]",
operative_findings_json=(
json.dumps(result.operative_findings) if getattr(result, "operative_findings", None) else "[]"
),
oncology_context_json=(
json.dumps(result.oncology_context) if getattr(result, "oncology_context", None) else "{}"
),
processing_time_ms=result.processing_time_ms,
metadata_json=json.dumps(meta_dict),
created_at=datetime.now(UTC),