From 9bfdefc0b412925d82eaa363a21a10a2d76a8a0b Mon Sep 17 00:00:00 2001 From: Automated Action Date: Mon, 9 Jun 2025 13:38:55 +0000 Subject: [PATCH] Fix Alembic migration import error - Updated env.py to correctly handle Python path by adding project root to sys.path - Added proper imports with noqa annotations to handle linting exceptions - Added prepend_sys_path setting to alembic.ini for better path resolution - Fixed import of models to ensure they're properly detected by Alembic --- alembic.ini | 3 +++ app/db/migrations/env.py | 15 ++++++++++++--- 2 files changed, 15 insertions(+), 3 deletions(-) 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,