jameszokah commited on
Commit
890b0ad
·
1 Parent(s): d5bb983

Enhance Dockerfile and migration script: add debug verification for copied files in Dockerfile, and update logging in migrate.py to reflect changes in PYTHONPATH and improve error handling with detailed directory listings on import failure.

Browse files
Files changed (2) hide show
  1. Dockerfile +4 -0
  2. scripts/migrate.py +45 -37
Dockerfile CHANGED
@@ -23,6 +23,10 @@ RUN pip install --no-cache-dir -r requirements.txt
23
  # Copy application code first
24
  COPY . .
25
 
 
 
 
 
26
  # Create storage directories (after copying code)
27
  RUN mkdir -p /app/storage/audio /app/storage/text /app/storage/temp && \
28
  # Set up permissions
 
23
  # Copy application code first
24
  COPY . .
25
 
26
+ # ---- DEBUG: Verify files were copied ----
27
+ RUN ls -la /app/app/models
28
+ # ----------------------------------------
29
+
30
  # Create storage directories (after copying code)
31
  RUN mkdir -p /app/storage/audio /app/storage/text /app/storage/temp && \
32
  # Set up permissions
scripts/migrate.py CHANGED
@@ -12,58 +12,64 @@ logging.basicConfig(
12
  )
13
  logger = logging.getLogger(__name__)
14
 
15
- # Add the project root to Python path
16
- project_root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
17
- if project_root not in sys.path:
18
- sys.path.insert(0, project_root)
19
 
20
- # Debug: Print current directory and file existence
21
  logger.info(f"Current directory: {os.getcwd()}")
22
- logger.info(f"Project root: {project_root}")
23
 
24
- models_init = os.path.join(project_root, "app", "models", "__init__.py")
25
- models_db = os.path.join(project_root, "app", "models", "database.py")
26
-
27
- logger.info(f"Checking if files exist:")
28
- logger.info(f"__init__.py exists: {os.path.exists(models_init)}")
29
- logger.info(f"database.py exists: {os.path.exists(models_db)}")
30
-
31
- if os.path.exists(models_db):
32
- logger.info(f"Content of {models_db}:")
33
- with open(models_db, 'r') as f:
34
- logger.info(f.read())
35
 
36
  try:
 
37
  from app.models.database import Base
 
38
  except ImportError as e:
39
  logger.error(f"Error importing Base: {e}")
40
- logger.info(f"Current sys.path: {sys.path}")
41
- logger.info("Contents of project root:")
42
- if os.path.exists(project_root):
43
- logger.info(os.listdir(project_root))
44
- logger.info("Contents of app directory:")
45
- app_dir = os.path.join(project_root, "app")
46
- if os.path.exists(app_dir):
47
- logger.info(os.listdir(app_dir))
48
- logger.info("Contents of models directory:")
49
- models_dir = os.path.join(app_dir, "models")
50
- if os.path.exists(models_dir):
51
- logger.info(os.listdir(models_dir))
 
 
 
 
 
 
 
52
  raise
53
 
54
  def run_migrations():
55
  """Run database migrations using Alembic."""
56
  try:
57
- # Get the directory containing this script
58
- current_dir = os.path.dirname(os.path.abspath(__file__))
59
- # Get the project root directory (one level up)
60
- project_root = os.path.dirname(current_dir)
61
 
62
- # Create Alembic configuration
63
- alembic_cfg = Config(os.path.join(project_root, "alembic.ini"))
 
 
 
 
 
 
64
 
65
- # Set the script location
66
- alembic_cfg.set_main_option("script_location", os.path.join(project_root, "alembic"))
 
 
67
 
68
  # Run the migration
69
  logger.info("Starting database migration...")
@@ -72,6 +78,8 @@ def run_migrations():
72
 
73
  except Exception as e:
74
  logger.error(f"Error during database migration: {str(e)}")
 
 
75
  raise
76
 
77
  if __name__ == "__main__":
 
12
  )
13
  logger = logging.getLogger(__name__)
14
 
15
+ # --- Rely on PYTHONPATH set in Dockerfile ---
16
+ # project_root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
17
+ # if project_root not in sys.path:
18
+ # sys.path.insert(0, project_root)
19
 
20
+ # Debug: Print relevant info
21
  logger.info(f"Current directory: {os.getcwd()}")
22
+ logger.info(f"Python Path (sys.path): {sys.path}")
23
 
24
+ models_db_path = "/app/app/models/database.py"
25
+ logger.info(f"Checking existence of {models_db_path}: {os.path.exists(models_db_path)}")
 
 
 
 
 
 
 
 
 
26
 
27
  try:
28
+ # Direct import, assuming PYTHONPATH is correct
29
  from app.models.database import Base
30
+ logger.info("Successfully imported Base from app.models.database")
31
  except ImportError as e:
32
  logger.error(f"Error importing Base: {e}")
33
+ # Log directory contents for deeper debugging
34
+ logger.info("Contents of /app:")
35
+ if os.path.exists("/app"):
36
+ try:
37
+ logger.info(os.listdir("/app"))
38
+ except Exception as list_err:
39
+ logger.error(f"Could not list /app: {list_err}")
40
+ logger.info("Contents of /app/app:")
41
+ if os.path.exists("/app/app"):
42
+ try:
43
+ logger.info(os.listdir("/app/app"))
44
+ except Exception as list_err:
45
+ logger.error(f"Could not list /app/app: {list_err}")
46
+ logger.info("Contents of /app/app/models:")
47
+ if os.path.exists("/app/app/models"):
48
+ try:
49
+ logger.info(os.listdir("/app/app/models"))
50
+ except Exception as list_err:
51
+ logger.error(f"Could not list /app/app/models: {list_err}")
52
  raise
53
 
54
  def run_migrations():
55
  """Run database migrations using Alembic."""
56
  try:
57
+ # Project root inside the container is /app
58
+ project_root = "/app"
 
 
59
 
60
+ # Create Alembic configuration using path inside container
61
+ alembic_cfg_path = os.path.join(project_root, "alembic.ini")
62
+ logger.info(f"Using Alembic config: {alembic_cfg_path}")
63
+ if not os.path.exists(alembic_cfg_path):
64
+ logger.error(f"Alembic config not found at {alembic_cfg_path}")
65
+ raise FileNotFoundError(f"Alembic config not found at {alembic_cfg_path}")
66
+
67
+ alembic_cfg = Config(alembic_cfg_path)
68
 
69
+ # Set the script location relative to project root
70
+ script_location = os.path.join(project_root, "alembic")
71
+ alembic_cfg.set_main_option("script_location", script_location)
72
+ logger.info(f"Set Alembic script location to: {script_location}")
73
 
74
  # Run the migration
75
  logger.info("Starting database migration...")
 
78
 
79
  except Exception as e:
80
  logger.error(f"Error during database migration: {str(e)}")
81
+ import traceback
82
+ logger.error(traceback.format_exc())
83
  raise
84
 
85
  if __name__ == "__main__":