import os import sys import logging from alembic.config import Config from alembic import command # Configure logging logging.basicConfig( level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s' ) logger = logging.getLogger(__name__) def run_migrations(): """Run database migrations using Alembic.""" try: # Get the directory containing this script current_dir = os.path.dirname(os.path.abspath(__file__)) # Get the project root directory (one level up) project_root = os.path.dirname(current_dir) # Create Alembic configuration alembic_cfg = Config(os.path.join(project_root, "alembic.ini")) # Set the script location alembic_cfg.set_main_option("script_location", os.path.join(project_root, "alembic")) # Run the migration logger.info("Starting database migration...") command.upgrade(alembic_cfg, "head") logger.info("Database migration completed successfully.") except Exception as e: logger.error(f"Error during database migration: {str(e)}") sys.exit(1) if __name__ == "__main__": run_migrations()