import logging import alembic.config from pathlib import Path from app.core.config import settings logger = logging.getLogger(__name__) def init_db(): """Initialize the database by running migrations""" logger.info("Initializing database") # Create DB directory if it doesn't exist settings.DB_DIR.mkdir(parents=True, exist_ok=True) # Get the path to the alembic.ini file alembic_ini_path = Path(__file__).parent.parent.parent / "alembic.ini" if not alembic_ini_path.exists(): logger.error(f"Alembic config file not found at {alembic_ini_path}") return try: # Run the migrations alembic_args = [ '--raiseerr', '-c', str(alembic_ini_path), 'upgrade', 'head', ] alembic.config.main(argv=alembic_args) logger.info("Database initialized successfully") except Exception as e: logger.error(f"Error initializing database: {str(e)}") raise