From b315ed6c34e3f3995b98560bed42ae6f2cf5f622 Mon Sep 17 00:00:00 2001 From: Automated Action Date: Thu, 15 May 2025 22:10:23 +0000 Subject: [PATCH] Fix alembic migration module import error - Fixed Python import path in alembic/env.py - Added settings import to ensure consistent database URL - Synchronized database configuration between app and alembic generated with BackendIM... (backend.im) --- alembic/env.py | 11 +++++++++++ app/core/config.py | 12 ++++++------ 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/alembic/env.py b/alembic/env.py index 384d826..eead04e 100644 --- a/alembic/env.py +++ b/alembic/env.py @@ -1,10 +1,17 @@ from logging.config import fileConfig +import os +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 Python's module search path +project_root = Path(__file__).parent.parent.absolute() +sys.path.append(str(project_root)) + # this is the Alembic Config object, which provides # access to the values within the .ini file in use. config = context.config @@ -13,6 +20,10 @@ config = context.config # This line sets up loggers basically. fileConfig(config.config_file_name) +# Import settings and set alembic's sqlalchemy.url to match the app's settings +from app.core.config import settings +config.set_main_option('sqlalchemy.url', settings.DATABASE_URL) + # add your model's MetaData object here # for 'autogenerate' support # from myapp import mymodel diff --git a/app/core/config.py b/app/core/config.py index bf29029..03b7098 100644 --- a/app/core/config.py +++ b/app/core/config.py @@ -7,20 +7,20 @@ class Settings(BaseSettings): PROJECT_NAME: str = "Todo API" API_V1_STR: str = "/api/v1" DEBUG: bool = True - + # Server settings HOST: str = "0.0.0.0" PORT: int = 8000 - + # Database settings DB_DIR: Path = Path("/app") / "storage" / "db" DATABASE_URL: str = f"sqlite:///{DB_DIR}/db.sqlite" - + class Config: env_file = ".env" env_file_encoding = "utf-8" -# Ensure DB directory exists -Path(settings.DB_DIR).mkdir(parents=True, exist_ok=True) +settings = Settings() -settings = Settings() \ No newline at end of file +# Ensure DB directory exists +settings.DB_DIR.mkdir(parents=True, exist_ok=True) \ No newline at end of file