From 9d80a588950296f6ef01fd7f1eb03c28a0749a12 Mon Sep 17 00:00:00 2001 From: Nargis Sultani Date: Tue, 28 Nov 2023 23:17:13 -0500 Subject: [PATCH] Ticket # 45: creating a new branch --- db_revisions/env.py | 3 ++- src/main.py | 12 ++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/db_revisions/env.py b/db_revisions/env.py index fe8291e..811e720 100644 --- a/db_revisions/env.py +++ b/db_revisions/env.py @@ -19,7 +19,8 @@ ENV = os.getenv("ENV", "LOCAL") if ENV == "LOCAL": - load_dotenv("src/.env.local") + file_dir = os.path.dirname(os.path.realpath(__file__)) + load_dotenv(f"{file_dir}/../src/.env.local") else: load_dotenv() diff --git a/src/main.py b/src/main.py index 7940eb8..0b6fba8 100644 --- a/src/main.py +++ b/src/main.py @@ -1,3 +1,4 @@ +import os import logging from http import HTTPStatus from fastapi import FastAPI, HTTPException, Request @@ -5,6 +6,8 @@ from fastapi.security import OAuth2AuthorizationCodeBearer from fastapi.middleware.cors import CORSMiddleware from starlette.middleware.authentication import AuthenticationMiddleware +from alembic.config import Config +from alembic import command from routers import admin_router, institutions_router @@ -17,6 +20,15 @@ app = FastAPI() +@app.on_event("startup") +async def app_start(): + file_dir = os.path.dirname(os.path.realpath(__file__)) + alembic_cfg = Config(f"{file_dir}/../alembic.ini") + alembic_cfg.set_main_option("script_location", f"{file_dir}/../db_revisions") + alembic_cfg.set_main_option("prepend_sys_path", f"{file_dir}/../") + command.upgrade(alembic_cfg, "head") + + @app.exception_handler(HTTPException) async def http_exception_handler(request: Request, exception: HTTPException) -> JSONResponse: log.error(exception, exc_info=True, stack_info=True)