Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 76 additions & 1 deletion apps/viz-backend/app/database.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from urllib.parse import quote

from settings import config
from sqlalchemy import create_engine, event
from sqlalchemy import MetaData, Table, create_engine, event
from sqlalchemy.engine.interfaces import DBAPIConnection
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
Expand Down Expand Up @@ -46,3 +46,78 @@ def set_timezone(
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)

Base = declarative_base()

metadata = MetaData()


def reflect_tables() -> dict[str, Table]:
"""
Reflect tables and views from the specified schemas.
Returns dynamically reflected tables.
"""
result_schema = config.omop.result_schema # Schema for result views
data_schema = config.omop.data_schema # Schema for data tables

interval_result = Table(
"interval_result", metadata, autoload_with=engine, schema=result_schema
)
full_day_coverage = Table(
"full_day_coverage", metadata, autoload_with=engine, schema=result_schema
)
partial_day_coverage = Table(
"partial_day_coverage", metadata, autoload_with=engine, schema=result_schema
)
criterion = Table("criterion", metadata, autoload_with=engine, schema=result_schema)

concept = Table("concept", metadata, autoload_with=engine, schema=data_schema)
person = Table("person", metadata, autoload_with=engine, schema=data_schema)
measurement = Table(
"measurement", metadata, autoload_with=engine, schema=data_schema
)
visit_occurrence = Table(
"visit_occurrence", metadata, autoload_with=engine, schema=data_schema
)
drug_exposure = Table(
"drug_exposure", metadata, autoload_with=engine, schema=data_schema
)
observation = Table(
"observation", metadata, autoload_with=engine, schema=data_schema
)
condition_occurrence = Table(
"condition_occurrence", metadata, autoload_with=engine, schema=data_schema
)
procedure_occurrence = Table(
"procedure_occurrence", metadata, autoload_with=engine, schema=data_schema
)

return {
"interval_result": interval_result,
"full_day_coverage": full_day_coverage,
"partial_day_coverage": partial_day_coverage,
"criterion": criterion,
"concept": concept,
"person": person,
"measurement": measurement,
"visit_occurrence": visit_occurrence,
"drug_exposure": drug_exposure,
"observation": observation,
"condition_occurrence": condition_occurrence,
"procedure_occurrence": procedure_occurrence,
}


# Reflect tables and expose them
tables = reflect_tables()
interval_result = tables["interval_result"]
full_day_coverage = tables["full_day_coverage"]
partial_day_coverage = tables["partial_day_coverage"]
criterion = tables["criterion"]

concept = tables["concept"]
person = tables["person"]
measurement = tables["measurement"]
visit_occurrence = tables["visit_occurrence"]
drug_exposure = tables["drug_exposure"]
observation = tables["observation"]
condition_occurrence = tables["condition_occurrence"]
procedure_occurrence = tables["procedure_occurrence"]
Loading