Commit 3203a0a
committed
fastapi-sqlalchemy-pg-catalog/app: clearer DATABASE_URL error + async-safe create_all
Two nits from the Copilot review:
* DATABASE_URL: `os.environ["DATABASE_URL"]` raised a bare KeyError
at import time, which surfaces as a noisy traceback in container
logs with no hint about what's missing. Switched to os.getenv with
an explicit RuntimeError that names the env var and gives a sample
URL format.
* lifespan: `Base.metadata.create_all(engine)` is synchronous
psycopg2 I/O running inside an async lifespan, blocking uvicorn's
event loop until pg_class probe + any CREATE TABLE round-trips
complete. Switched to `await asyncio.to_thread(...)` so the loop
stays responsive. For this minimal repro the difference is small,
but the pattern is the right FastAPI shape for any startup that
touches a sync DB driver.
Signed-off-by: Akash Kumar <meakash7902@gmail.com>1 parent ec3174e commit 3203a0a
1 file changed
Lines changed: 14 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
16 | 16 | | |
17 | 17 | | |
18 | 18 | | |
| 19 | + | |
19 | 20 | | |
20 | 21 | | |
21 | 22 | | |
| |||
32 | 33 | | |
33 | 34 | | |
34 | 35 | | |
35 | | - | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
36 | 42 | | |
37 | 43 | | |
38 | 44 | | |
| |||
59 | 65 | | |
60 | 66 | | |
61 | 67 | | |
62 | | - | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
63 | 75 | | |
64 | 76 | | |
65 | 77 | | |
| |||
0 commit comments