Skip to content

Commit

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

0 comments on commit 596ea55

Please sign in to comment.