File size: 3,170 Bytes
126ae8d
d4255ec
275b1e7
d4255ec
 
 
275b1e7
d5bb983
d4255ec
 
 
 
 
 
890b0ad
 
 
 
d5bb983
890b0ad
d5bb983
890b0ad
d5bb983
c27f115
890b0ad
d5bb983
053d168
890b0ad
c27f115
 
053d168
 
890b0ad
 
 
 
 
 
 
 
 
 
 
 
 
c27f115
 
890b0ad
c27f115
890b0ad
c27f115
053d168
 
d4255ec
 
 
890b0ad
 
d4255ec
890b0ad
 
 
 
 
 
 
 
d4255ec
890b0ad
 
 
 
d4255ec
 
 
 
 
 
 
 
890b0ad
 
126ae8d
d4255ec
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
"""Database migration script."""
import os
import sys
import logging
from alembic.config import Config
from alembic import command

# Configure logging first
logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)

# --- Rely on PYTHONPATH set in Dockerfile --- 
# project_root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# if project_root not in sys.path:
#     sys.path.insert(0, project_root)

# Debug: Print relevant info
logger.info(f"Current directory: {os.getcwd()}")
logger.info(f"Python Path (sys.path): {sys.path}")

models_db_path = "/app/app/models/database.py"
logger.info(f"Checking existence of {models_db_path}: {os.path.exists(models_db_path)}")

try:
    # Direct import, assuming PYTHONPATH is correct
    from app.models.database import Base
    logger.info("Successfully imported Base from app.models.database")
except ImportError as e:
    logger.error(f"Error importing Base: {e}")
    # Log directory contents for deeper debugging
    logger.info("Contents of /app:")
    if os.path.exists("/app"):
        try:
            logger.info(os.listdir("/app"))
        except Exception as list_err:
            logger.error(f"Could not list /app: {list_err}")
    logger.info("Contents of /app/app:")
    if os.path.exists("/app/app"):
        try:
            logger.info(os.listdir("/app/app"))
        except Exception as list_err:
            logger.error(f"Could not list /app/app: {list_err}")
    logger.info("Contents of /app/app/models:")
    if os.path.exists("/app/app/models"):
        try:
            logger.info(os.listdir("/app/app/models"))
        except Exception as list_err:
            logger.error(f"Could not list /app/app/models: {list_err}")
    raise

def run_migrations():
    """Run database migrations using Alembic."""
    try:
        # Project root inside the container is /app
        project_root = "/app"
        
        # Create Alembic configuration using path inside container
        alembic_cfg_path = os.path.join(project_root, "alembic.ini")
        logger.info(f"Using Alembic config: {alembic_cfg_path}")
        if not os.path.exists(alembic_cfg_path):
            logger.error(f"Alembic config not found at {alembic_cfg_path}")
            raise FileNotFoundError(f"Alembic config not found at {alembic_cfg_path}")
            
        alembic_cfg = Config(alembic_cfg_path)
        
        # Set the script location relative to project root
        script_location = os.path.join(project_root, "alembic")
        alembic_cfg.set_main_option("script_location", script_location)
        logger.info(f"Set Alembic script location to: {script_location}")
        
        # 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)}")
        import traceback
        logger.error(traceback.format_exc())
        raise

if __name__ == "__main__":
    run_migrations()