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)
This commit is contained in:
Automated Action 2025-05-15 22:10:23 +00:00
parent d20c3af0bc
commit b315ed6c34
2 changed files with 17 additions and 6 deletions

View File

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

View File

@ -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()
# Ensure DB directory exists
settings.DB_DIR.mkdir(parents=True, exist_ok=True)