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
This commit is contained in:
commit
530867bb16
@ -45,10 +45,10 @@ WORKDIR /app
|
|||||||
|
|
||||||
# Copy only necessary application files (be selective)
|
# Copy only necessary application files (be selective)
|
||||||
COPY --chown=appuser:appuser app/ ./app/
|
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 *.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
|
||||||
|
@ -9,6 +9,7 @@ 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 sqlalchemy.ext.asyncio import AsyncEngine
|
||||||
from alembic.config import Config
|
from alembic.config import Config
|
||||||
from alembic import command
|
from alembic import command
|
||||||
import os
|
import os
|
||||||
@ -213,25 +214,33 @@ async def read_root():
|
|||||||
}
|
}
|
||||||
# --- End Root Endpoint ---
|
# --- 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")
|
@app.on_event("startup")
|
||||||
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...")
|
||||||
|
|
||||||
# Run database migrations
|
# Run database migrations
|
||||||
try:
|
await run_migrations()
|
||||||
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
|
|
||||||
|
|
||||||
|
# Initialize scheduler
|
||||||
init_scheduler()
|
init_scheduler()
|
||||||
logger.info("Application startup complete.")
|
logger.info("Application startup complete.")
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user