Skip to content

Commit

Permalink
Add Robustness to Database Initialization (#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
berggren authored Nov 27, 2024
1 parent 6231d9a commit 0e86fb8
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@

from fastapi import Depends, FastAPI
from fastapi.middleware.cors import CORSMiddleware
from sqlalchemy import not_, or_
from sqlalchemy import not_, or_, text
from sqlalchemy.exc import ProgrammingError
from starlette.middleware.sessions import SessionMiddleware

from api.v1 import configs as configs_v1
Expand Down Expand Up @@ -71,7 +72,11 @@ async def lifespan(app: FastAPI):
# This is run before the application accepts requests (before start)
try:
db = SessionLocal()
# Try a simple query that requires the table to exist
db.execute(text("SELECT 1 FROM user LIMIT 1")).all()
await populate_everyone_group(db)
except ProgrammingError: # Catch table-not-found errors
pass
finally:
db.close()
yield
Expand Down

0 comments on commit 0e86fb8

Please sign in to comment.