diff --git a/alembic.ini b/alembic.ini index d2ab457..46f433e 100644 --- a/alembic.ini +++ b/alembic.ini @@ -4,6 +4,9 @@ # path to migration scripts script_location = app/db/migrations +# prepend current directory to path (helps with imports) +prepend_sys_path = . + # template used to generate migration files # file_template = %%(rev)s_%%(slug)s diff --git a/app/db/migrations/env.py b/app/db/migrations/env.py index 20f0ad6..da958d4 100644 --- a/app/db/migrations/env.py +++ b/app/db/migrations/env.py @@ -1,10 +1,21 @@ 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 -from app.db.base import Base + +# Add the parent directory to sys.path to allow imports from the app package +# Determine the parent directory of the migrations directory +current_path = Path(__file__).parent.parent.parent.parent +sys.path.insert(0, str(current_path)) + +# Import Base after setting up sys.path +from app.db.base import Base # noqa: E402 +# Import all models to register them with SQLAlchemy metadata +from app.models.file import File # noqa: E402, F401 - import needed for Alembic to detect models # this is the Alembic Config object, which provides # access to the values within the .ini file in use. @@ -16,8 +27,6 @@ fileConfig(config.config_file_name) # add your model's MetaData object here # for 'autogenerate' support -# from myapp import mymodel -# target_metadata = mymodel.Base.metadata target_metadata = Base.metadata # other values from the config, defined by the needs of env.py,