"""Database migration script.""" import os import sys import logging from alembic.config import Config from alembic import command # Add potential module paths to Python path possible_paths = [ # Docker path "/app", # Local development path os.path.dirname(os.path.dirname(os.path.abspath(__file__))), ] # Add paths to Python path for path in possible_paths: if path not in sys.path and os.path.exists(path): sys.path.insert(0, path) try: from app.models.database import Base except ImportError as e: print(f"Error importing Base: {e}") print("Current sys.path:", sys.path) print("Contents of /app directory:") if os.path.exists("/app"): print(os.listdir("/app")) print("Contents of app directory:") if os.path.exists("app"): print(os.listdir("app")) raise # 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) # In Docker, the project root is /app if os.path.exists("/app/alembic.ini"): project_root = "/app" # 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)}") raise if __name__ == "__main__": run_migrations()