Skip to content

Commit

Permalink
fix: Close db connection on startup (#205) (#212)
Browse files Browse the repository at this point in the history
  • Loading branch information
gromdimon authored Nov 15, 2023
1 parent e8ed6be commit 1c2b913
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions backend/app/backend_pre_start.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 1c2b913

Please sign in to comment.