Compare commits

...

2 Commits

Author SHA1 Message Date
mo
530867bb16 Merge pull request 'refactor: Simplify Dockerfile by reorganizing Alembic file copying and enhance migration handling in application startup' (#23) from ph4 into prod
Reviewed-on: #23
2025-06-01 17:03:25 +02:00
mohamad
26e06ddeaa refactor: Simplify Dockerfile by reorganizing Alembic file copying and enhance migration handling in application startup
All checks were successful
Deploy to Production, build images and push to Gitea Registry / build_and_push (pull_request) Successful in 1m21s
2025-06-01 17:03:13 +02:00
2 changed files with 22 additions and 13 deletions

View File

@ -45,10 +45,10 @@ WORKDIR /app
# Copy only necessary application files (be selective)
COPY --chown=appuser:appuser app/ ./app/
COPY --chown=appuser:appuser alembic/ ./alembic/
COPY --chown=appuser:appuser alembic.ini ./
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,7 @@ from sentry_sdk.integrations.fastapi import FastApiIntegration
from fastapi_users.authentication import JWTStrategy
from pydantic import BaseModel
from jose import jwt, JWTError
from sqlalchemy.ext.asyncio import AsyncEngine
from alembic.config import Config
from alembic import command
import os
@ -213,25 +214,33 @@ async def read_root():
}
# --- End Root Endpoint ---
async def run_migrations():
"""Run database migrations."""
try:
logger.info("Running database migrations...")
# Get the path to the alembic.ini file
alembic_ini_path = os.path.join(os.path.dirname(__file__), '..', 'alembic.ini')
# Create Alembic configuration
alembic_cfg = Config(alembic_ini_path)
# Run the migration
command.upgrade(alembic_cfg, "head")
logger.info("Database migrations completed successfully.")
except Exception as e:
logger.error(f"Error running migrations: {e}")
raise
# --- Application Startup/Shutdown Events (Optional) ---
@app.on_event("startup")
async def startup_event():
"""Initialize services on startup."""
logger.info(f"Application startup in {settings.ENVIRONMENT} environment...")
# Run database migrations
try:
logger.info("Running database migrations...")
alembic_cfg = Config()
alembic_cfg.set_main_option("script_location", "alembic")
alembic_cfg.set_main_option("sqlalchemy.url", settings.DATABASE_URL)
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
await run_migrations()
# Initialize scheduler
init_scheduler()
logger.info("Application startup complete.")