feat: Add Alembic configuration and migration command to application startup #21

Merged
mo merged 1 commits from ph4 into prod 2025-06-01 16:54:30 +02:00
2 changed files with 16 additions and 2 deletions
Showing only changes of commit 411c3c91b2 - Show all commits

View File

@ -47,6 +47,8 @@ WORKDIR /app
COPY --chown=appuser:appuser app/ ./app/
COPY --chown=appuser:appuser *.py ./
COPY --chown=appuser:appuser requirements.txt ./
COPY --chown=appuser:appuser alembic.ini ./
COPY --chown=appuser:appuser alembic/ ./alembic/
# Create logs directory
RUN mkdir -p /app/logs && chown -R appuser:appuser /app

View File

@ -9,6 +9,9 @@ from sentry_sdk.integrations.fastapi import FastApiIntegration
from fastapi_users.authentication import JWTStrategy
from pydantic import BaseModel
from jose import jwt, JWTError
from alembic.config import Config
from alembic import command
import os
from app.api.api_router import api_router
from app.config import settings
@ -216,8 +219,17 @@ async def read_root():
async def startup_event():
"""Initialize services on startup."""
logger.info(f"Application startup in {settings.ENVIRONMENT} environment...")
# You might perform initial checks or warm-up here
# await database.engine.connect() # Example check (get_db handles sessions per request)
# Run database migrations
try:
logger.info("Running database migrations...")
alembic_cfg = Config("alembic.ini")
command.upgrade(alembic_cfg, "head")
logger.info("Database migrations completed successfully.")
except Exception as e:
logger.error(f"Failed to run database migrations: {e}")
raise
init_scheduler()
logger.info("Application startup complete.")