update configuration docker setup for data platform
This commit is contained in:
@@ -1,12 +1,54 @@
|
||||
from sqlalchemy import text
|
||||
from sqlalchemy.orm import Session
|
||||
import logging
|
||||
|
||||
from app.db.base import Base
|
||||
from app.db.engine import engine
|
||||
from app.models.user import User, Role # Import models to ensure they're registered
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def init_db() -> None:
|
||||
"""Initialize database schemas and tables"""
|
||||
with engine.begin() as conn:
|
||||
# Create schemas
|
||||
conn.execute(text("CREATE SCHEMA IF NOT EXISTS fastapi"))
|
||||
conn.execute(text("CREATE SCHEMA IF NOT EXISTS operationbi"))
|
||||
|
||||
conn.execute(text("CREATE SCHEMA IF NOT EXISTS rawdata"))
|
||||
|
||||
# Create all tables
|
||||
Base.metadata.create_all(bind=conn)
|
||||
logger.info("Database schemas and tables created")
|
||||
|
||||
# Seed default roles
|
||||
seed_roles()
|
||||
|
||||
|
||||
def seed_roles() -> None:
|
||||
"""Seed default roles if they don't exist"""
|
||||
from app.db.session import SessionLocal
|
||||
|
||||
db = SessionLocal()
|
||||
try:
|
||||
roles_data = [
|
||||
{"name": "admin", "description": "Full system access - can manage users and access all features"},
|
||||
{"name": "operation", "description": "Data management access - can upload and manage data"}
|
||||
]
|
||||
|
||||
for role_data in roles_data:
|
||||
existing = db.query(Role).filter(Role.name == role_data["name"]).first()
|
||||
if not existing:
|
||||
role = Role(**role_data)
|
||||
db.add(role)
|
||||
logger.info(f"Created role: {role_data['name']}")
|
||||
else:
|
||||
logger.info(f"Role already exists: {role_data['name']}")
|
||||
|
||||
db.commit()
|
||||
logger.info("Role seeding completed")
|
||||
except Exception as e:
|
||||
logger.error(f"Error seeding roles: {e}")
|
||||
db.rollback()
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
@@ -46,6 +46,25 @@ class RawWaitingTime(Base):
|
||||
updated_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False, server_default=func.now())
|
||||
|
||||
|
||||
class PatientAppointment(Base):
|
||||
__tablename__ = "patient_appointment"
|
||||
__table_args__ = (
|
||||
UniqueConstraint("hn", "date", "time", name="uq_patient_appointment_hn_date_time"),
|
||||
{"schema": "rawdata"},
|
||||
)
|
||||
|
||||
id: Mapped[int] = mapped_column(BigInteger, primary_key=True, autoincrement=True)
|
||||
hn: Mapped[str] = mapped_column(String(50), nullable=False)
|
||||
txn: Mapped[int | None] = mapped_column(BigInteger, nullable=True)
|
||||
date: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False)
|
||||
time: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False)
|
||||
doctor_code: Mapped[str | None] = mapped_column(String(50), nullable=True)
|
||||
period: Mapped[str | None] = mapped_column(String(50), nullable=True)
|
||||
appointment_type: Mapped[str | None] = mapped_column(String(100), nullable=True)
|
||||
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False, server_default=func.now())
|
||||
updated_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False, server_default=func.now())
|
||||
|
||||
|
||||
class ApiClient(Base):
|
||||
__tablename__ = "api_client"
|
||||
__table_args__ = {"schema": "fastapi"}
|
||||
|
||||
27
03-apiservice/app/db/session.py
Normal file
27
03-apiservice/app/db/session.py
Normal file
@@ -0,0 +1,27 @@
|
||||
"""
|
||||
Database session management
|
||||
"""
|
||||
from sqlalchemy.orm import sessionmaker, Session
|
||||
from app.db.engine import engine
|
||||
|
||||
# Create SessionLocal class
|
||||
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
||||
|
||||
|
||||
def get_db() -> Session:
|
||||
"""
|
||||
Dependency to get database session
|
||||
|
||||
Usage:
|
||||
@app.get("/items")
|
||||
def read_items(db: Session = Depends(get_db)):
|
||||
...
|
||||
|
||||
Yields:
|
||||
Database session
|
||||
"""
|
||||
db = SessionLocal()
|
||||
try:
|
||||
yield db
|
||||
finally:
|
||||
db.close()
|
||||
Reference in New Issue
Block a user