diff --git a/backend/app/backend_pre_start.py b/backend/app/backend_pre_start.py index 5d823e11..15580ed6 100644 --- a/backend/app/backend_pre_start.py +++ b/backend/app/backend_pre_start.py @@ -23,15 +23,19 @@ ) async def init(): try: - with SessionLocal() as db: + async with SessionLocal() as db: # Try to create session to check if DB is awake await db.execute(text("SELECT 1")) - # Ensure to run Alembic on startup - for key in ("DATABASE_URL", "SQLALCHEMY_DATABASE_URI"): - if key in os.environ: - os.environ[key] = os.environ[key].replace("+asyncpg", "") - alembicArgs = ["--raiseerr", "upgrade", "head"] - alembic.config.main(alembicArgs) + await db.commit() + logger.info("DB connection ready") + + # Ensure to run Alembic on startup + for key in ("DATABASE_URL", "SQLALCHEMY_DATABASE_URI"): + if key in os.environ: + os.environ[key] = os.environ[key].replace("+asyncpg", "") + + alembicArgs = ["--raiseerr", "upgrade", "head"] + alembic.config.main(alembicArgs) except Exception as e: logger.error(e) raise e