Refactor database initialization and remove Alembic migrations
- Removed legacy Alembic migration files and consolidated schema management into a new Pydantic-backed initializer (`scripts/init_db.py`). - Updated `main.py` to ensure the new DB initializer runs on startup, maintaining idempotency. - Adjusted session management in `config/database.py` to prevent DetachedInstanceError. - Introduced new enums in `models/enums.py` for better organization and clarity. - Refactored various models to utilize the new enums, improving code maintainability. - Enhanced middleware to handle JSON validation more robustly, ensuring non-JSON requests do not trigger JSON errors. - Added tests for middleware and enums to ensure expected behavior and consistency. - Updated changelog to reflect significant changes and improvements.
This commit is contained in:
@@ -1,9 +0,0 @@
|
||||
#!/usr/bin/env sh
|
||||
set -e
|
||||
|
||||
PYTHONPATH="/app:${PYTHONPATH}"
|
||||
export PYTHONPATH
|
||||
|
||||
python -m scripts.run_migrations
|
||||
|
||||
exec "$@"
|
||||
@@ -1,42 +0,0 @@
|
||||
"""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