diff --git a/README.md b/README.md index e245618..23bfd21 100644 --- a/README.md +++ b/README.md @@ -49,7 +49,8 @@ A comprehensive HR platform backend built with FastAPI and SQLite, providing emp │ └── versions # Migration versions ├── openapi.json # Generated OpenAPI schema ├── openapi_schema.py # Script to generate OpenAPI schema -└── requirements.txt # Python dependencies +├── requirements.txt # Python dependencies +└── run_migrations.py # Helper script for running Alembic migrations ``` ## Getting Started @@ -81,10 +82,16 @@ A comprehensive HR platform backend built with FastAPI and SQLite, providing emp ### Running Migrations -Run Alembic migrations to set up the database schema: +Run Alembic migrations to set up the database schema using the provided script: ```bash -alembic upgrade head +python run_migrations.py upgrade head +``` + +Alternatively, if you need to create a new migration: + +```bash +python run_migrations.py revision --autogenerate -m "description" ``` ### Running the Application diff --git a/migrations/env.py b/migrations/env.py index b44e165..596a66c 100644 --- a/migrations/env.py +++ b/migrations/env.py @@ -1,10 +1,17 @@ from logging.config import fileConfig +import sys +from pathlib import Path from sqlalchemy import engine_from_config from sqlalchemy import pool from alembic import context +# Add the project root directory to the Python path +# This allows Alembic to find the app module +BASE_DIR = Path(__file__).resolve().parent.parent +sys.path.append(str(BASE_DIR)) + # this is the Alembic Config object, which provides # access to the values within the .ini file in use. config = context.config diff --git a/run_migrations.py b/run_migrations.py new file mode 100755 index 0000000..af307f4 --- /dev/null +++ b/run_migrations.py @@ -0,0 +1,25 @@ +#!/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 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") + +if __name__ == "__main__": + # 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:]) \ No newline at end of file