feat: add automatic database initialization and recovery

- Add init_database() with alembic programmatic API and retry logic
- Add connection retry with exponential backoff (5 attempts)
- Improve error messages for connection/auth failures
- Add table existence check before seeding data
- Update startup event to run migrations before seeding
- Add wait-for-db.sh script for Docker containers
- Update Docker and docker-compose configurations

Quality gates: ruff ✓, mypy ✓, unit tests (8 passed)
This commit is contained in:
Fusion
2026-05-18 22:09:52 +02:00
parent 29c3563148
commit ea6c466c6c
11 changed files with 338 additions and 22 deletions
+7 -1
View File
@@ -25,6 +25,7 @@ WORKDIR /app
RUN apt-get update && apt-get install -y --no-install-recommends \
libpq5 \
git \
netcat-openbsd \
&& rm -rf /var/lib/apt/lists/*
# Copy dependencies from builder
@@ -37,6 +38,10 @@ COPY --chown=appuser:appgroup . .
# Create directories for repo storage
RUN mkdir -p /data/repos && chown -R appuser:appgroup /data/repos
# Copy wait-for-db script
COPY wait-for-db.sh /usr/local/bin/wait-for-db.sh
RUN chmod +x /usr/local/bin/wait-for-db.sh
# Switch to non-root user
USER appuser
@@ -47,5 +52,6 @@ EXPOSE 8000
HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \
CMD python -c "import urllib.request; urllib.request.urlopen('http://localhost:8000/health')" || exit 1
# Run the application
# Run the application (with database wait)
ENTRYPOINT ["/usr/local/bin/wait-for-db.sh"]
CMD ["uvicorn", "src.main:app", "--host", "0.0.0.0", "--port", "8000"]