Fix Alembic migrations module import error

- Updated migrations/env.py to add project root to Python path
- Created run_migrations.py helper script for running migrations
- Updated README with instructions for using the helper script
- Fixed the 'No module named app' error when running migrations
This commit is contained in:
Automated Action 2025-06-03 01:23:07 +00:00
parent 27c9268a6a
commit 84555ef4e8
3 changed files with 42 additions and 3 deletions

View File

@ -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

View File

@ -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

25
run_migrations.py Executable file
View File

@ -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:])