Spaces:
Sleeping
Sleeping

Refactor model structure: update import paths from 'app.modelz' to 'app.models' across multiple files for consistency, remove obsolete 'modelz' directory, and adjust Dockerfile and migration script to reflect these changes, enhancing clarity and organization in the codebase.
c27f115
"""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() |