Refactor OpenAPI fetcher for improved clarity and robustness

Reorganized and enhanced the OpenAPI fetch logic for better maintainability and error handling. Key updates include improved environment variable validation, more detailed error messages, streamlined configuration loading, and additional safety checks for file paths and directories. Added proper logging and ensured the process flow is easy to trace.
This commit is contained in:
GotthardG
2024-12-17 16:25:53 +01:00
parent dbf7864e7e
commit 9b6ad107bb
2 changed files with 25 additions and 9 deletions

View File

@ -17,7 +17,7 @@ from app.routers import (
auth,
sample,
)
from app.database import Base, engine, SessionLocal, load_sample_data, load_slots_data
from app.database import Base, engine, SessionLocal
# Utility function to fetch metadata from pyproject.toml
@ -94,6 +94,7 @@ app.add_middleware(
@app.on_event("startup")
def on_startup():
print("[INFO] Running application startup tasks...")
db = SessionLocal()
try:
if environment == "prod":
@ -102,20 +103,26 @@ def on_startup():
inspector = reflection.Inspector.from_engine(engine)
tables_exist = inspector.get_table_names()
# Ensure the production database is initialized
if not tables_exist:
print("Production database is empty. Initializing...")
Base.metadata.create_all(bind=engine)
load_slots_data(db)
else:
print("Production database already initialized.")
else: # dev or test
# Seed the database (slots + proposals)
from app.database import load_slots_data
load_slots_data(db)
else: # dev or test environments
print(f"{environment.capitalize()} environment: Regenerating database.")
Base.metadata.drop_all(bind=engine)
Base.metadata.create_all(bind=engine)
if environment == "dev":
from app.database import load_sample_data
load_sample_data(db)
elif environment == "test":
from app.database import load_slots_data
load_slots_data(db)
finally:
db.close()