Refactor Docker setup and migration scripts
- Updated Dockerfile to set permissions for the entrypoint script and defined the entrypoint for the container. - Consolidated Alembic migration history into a single initial migration file and removed obsolete revision files. - Added a new script to run Alembic migrations before starting the application. - Updated changelog to reflect changes in migration handling and Docker setup. - Enhanced pytest configuration for coverage reporting and excluded specific files from coverage calculations.
This commit is contained in:
42
scripts/run_migrations.py
Normal file
42
scripts/run_migrations.py
Normal file
@@ -0,0 +1,42 @@
|
||||
"""Utility for applying Alembic migrations before application startup."""
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
from pathlib import Path
|
||||
|
||||
from alembic import command
|
||||
from alembic.config import Config
|
||||
from dotenv import load_dotenv
|
||||
|
||||
logging.basicConfig(level=logging.INFO)
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def _load_env() -> None:
|
||||
"""Ensure environment variables from .env are available."""
|
||||
load_dotenv()
|
||||
|
||||
|
||||
def _alembic_config(project_root: Path) -> Config:
|
||||
config_path = project_root / "alembic.ini"
|
||||
if not config_path.exists():
|
||||
raise FileNotFoundError(f"Missing alembic.ini at {config_path}")
|
||||
|
||||
config = Config(str(config_path))
|
||||
config.set_main_option("script_location", str(project_root / "alembic"))
|
||||
return config
|
||||
|
||||
|
||||
def run_migrations(target_revision: str = "head") -> None:
|
||||
"""Apply Alembic migrations up to the given revision."""
|
||||
project_root = Path(__file__).resolve().parent.parent
|
||||
_load_env()
|
||||
|
||||
config = _alembic_config(project_root)
|
||||
logger.info("Applying database migrations up to %s", target_revision)
|
||||
command.upgrade(config, target_revision)
|
||||
logger.info("Database migrations applied successfully")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
run_migrations()
|
||||
Reference in New Issue
Block a user