Run migrations via entrypoint (alembic upgrade head) instead of nested asyncio.run in startup hook
Some checks failed
build-and-deploy / build (push) Failing after 16s

This commit is contained in:
sirius0xdev 2026-07-07 18:24:01 -04:00
parent 985fedec7b
commit 0a6da2f9c1
No known key found for this signature in database
3 changed files with 17 additions and 6 deletions

View file

@ -12,7 +12,8 @@ RUN pip install --no-cache-dir -r requirements.txt
COPY app/ ./app/ COPY app/ ./app/
COPY alembic.ini ./alembic.ini COPY alembic.ini ./alembic.ini
COPY alembic/ ./alembic/ COPY alembic/ ./alembic/
RUN chmod +x /app/app/docker-entrypoint.sh
EXPOSE 8000 EXPOSE 8000
ENV PYTHONPATH=/app/app ENV PYTHONPATH=/app/app
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000"] ENTRYPOINT ["/app/app/docker-entrypoint.sh"]

10
app/docker-entrypoint.sh Normal file
View file

@ -0,0 +1,10 @@
#!/usr/bin/env bash
# App entrypoint: apply migrations, then launch the API.
set -e
cd /app
echo "[entrypoint] running migrations..."
alembic upgrade head
echo "[entrypoint] starting uvicorn..."
exec uvicorn app.main:app --host 0.0.0.0 --port 8000

View file

@ -124,12 +124,12 @@ async def health():
@app.on_event("startup") @app.on_event("startup")
async def startup(): async def startup():
"""Initialize extensions and run migrations.""" """Initialize PostGIS/TimescaleDB extensions on first connection.
Schema migrations are applied by the container entrypoint (alembic upgrade
head) before uvicorn starts, so they don't run nested inside the event loop.
"""
await init_extensions() await init_extensions()
from alembic import command
from alembic.config import Config
alembic_cfg = Config(str(Path(__file__).parent.parent / "alembic.ini"))
command.upgrade(alembic_cfg, "head")
# ── Feed Sources ────────────────────────────────────────────────────────── # ── Feed Sources ──────────────────────────────────────────────────────────