Compare commits

...

2 Commits

Author SHA1 Message Date
mo
792a7878f0 Merge pull request 'feat: Add Alembic configuration and migration command to application startup' (#21) from ph4 into prod
Reviewed-on: #21
2025-06-01 16:54:30 +02:00
mohamad
411c3c91b2 feat: Add Alembic configuration and migration command to application startup
All checks were successful
Deploy to Production, build images and push to Gitea Registry / build_and_push (pull_request) Successful in 1m16s
2025-06-01 16:54:16 +02:00
2 changed files with 16 additions and 2 deletions

View File

@ -47,6 +47,8 @@ WORKDIR /app
COPY --chown=appuser:appuser app/ ./app/ COPY --chown=appuser:appuser app/ ./app/
COPY --chown=appuser:appuser *.py ./ COPY --chown=appuser:appuser *.py ./
COPY --chown=appuser:appuser requirements.txt ./ COPY --chown=appuser:appuser requirements.txt ./
COPY --chown=appuser:appuser alembic.ini ./
COPY --chown=appuser:appuser alembic/ ./alembic/
# Create logs directory # Create logs directory
RUN mkdir -p /app/logs && chown -R appuser:appuser /app 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 fastapi_users.authentication import JWTStrategy
from pydantic import BaseModel from pydantic import BaseModel
from jose import jwt, JWTError 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.api.api_router import api_router
from app.config import settings from app.config import settings
@ -216,8 +219,17 @@ async def read_root():
async def startup_event(): async def startup_event():
"""Initialize services on startup.""" """Initialize services on startup."""
logger.info(f"Application startup in {settings.ENVIRONMENT} environment...") 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() init_scheduler()
logger.info("Application startup complete.") logger.info("Application startup complete.")