From 596ea55eb09c634957d36e8c77520e3ffdf9cc27 Mon Sep 17 00:00:00 2001 From: gromdimon Date: Wed, 15 Nov 2023 11:17:30 +0100 Subject: [PATCH 1/3] fix: Close db connection on startup --- backend/app/backend_pre_start.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/backend/app/backend_pre_start.py b/backend/app/backend_pre_start.py index f3a2562b..5d823e11 100644 --- a/backend/app/backend_pre_start.py +++ b/backend/app/backend_pre_start.py @@ -23,15 +23,15 @@ ) async def init(): try: - db = SessionLocal() - # 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) + 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) except Exception as e: logger.error(e) raise e From 105a58bb18af9e8d43162917d3d5a1aa28c3f510 Mon Sep 17 00:00:00 2001 From: gromdimon Date: Wed, 15 Nov 2023 11:34:50 +0100 Subject: [PATCH 2/3] fix: none --- backend/app/backend_pre_start.py | 1 + 1 file changed, 1 insertion(+) diff --git a/backend/app/backend_pre_start.py b/backend/app/backend_pre_start.py index 5d823e11..c0f88225 100644 --- a/backend/app/backend_pre_start.py +++ b/backend/app/backend_pre_start.py @@ -30,6 +30,7 @@ async def init(): 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: From f1668630b682135cd92d6a55693b944b9633c969 Mon Sep 17 00:00:00 2001 From: gromdimon Date: Wed, 15 Nov 2023 11:59:04 +0100 Subject: [PATCH 3/3] wip --- backend/app/backend_pre_start.py | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/backend/app/backend_pre_start.py b/backend/app/backend_pre_start.py index c0f88225..15580ed6 100644 --- a/backend/app/backend_pre_start.py +++ b/backend/app/backend_pre_start.py @@ -23,16 +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", "") + await db.commit() + logger.info("DB connection ready") - alembicArgs = ["--raiseerr", "upgrade", "head"] - alembic.config.main(alembicArgs) + # 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