#!/usr/bin/env python # -*- coding: utf-8 -*- """ Helper script to run Alembic migrations with the correct Python path. Usage: python run_migrations.py upgrade head python run_migrations.py revision --autogenerate -m "description" python run_migrations.py --help """ import os import sys from pathlib import Path # Add the project root directory to the Python path BASE_DIR = Path(__file__).resolve().parent sys.path.append(str(BASE_DIR)) print(f"Added {BASE_DIR} to Python path") # Set environment variables for containerized environments if not already set if "DATABASE_PATH" not in os.environ: # Try multiple possible paths for the database possible_paths = [ "/app/storage/db/db.sqlite", "/tmp/hrplatform/db/db.sqlite", str(BASE_DIR / "db" / "db.sqlite") ] for path in possible_paths: try: # Ensure directory exists db_dir = Path(path).parent db_dir.mkdir(parents=True, exist_ok=True) # Test if we can write to this directory test_file = db_dir / ".write_test" test_file.touch() test_file.unlink() os.environ["DATABASE_PATH"] = path print(f"Using database at: {path}") break except (PermissionError, OSError): continue else: print("WARNING: Could not find a writable location for the database.") print("Please set the DATABASE_PATH environment variable.") if __name__ == "__main__": try: # Import alembic's main function from alembic.config import main # Execute alembic command with sys.argv (e.g., 'upgrade', 'head') main(argv=sys.argv[1:]) except Exception as e: print(f"Error running migrations: {e}") sys.exit(1)