Fix database migration issues

- Add project root to Python path in migrations/env.py
- Change database URL configuration to ensure database directory exists
- Update alembic.ini to use relative database path
- Make database URL configuration more resilient
This commit is contained in:
Automated Action 2025-06-06 11:36:18 +00:00
parent c8aed27755
commit 667fee7466
4 changed files with 21 additions and 5 deletions

View File

@ -35,7 +35,8 @@ script_location = migrations
# are written from script.py.mako
# output_encoding = utf-8
sqlalchemy.url = sqlite:////app/storage/db/db.sqlite
# SQLite URL - use a relative path that will be resolved at runtime
sqlalchemy.url = sqlite:///db.sqlite
[post_write_hooks]
# post_write_hooks defines scripts or Python functions that are run

View File

@ -21,7 +21,12 @@ class Settings(BaseSettings):
# Database
DB_DIR: Path = Path("/app") / "storage" / "db"
SQLALCHEMY_DATABASE_URL: str = f"sqlite:///{DB_DIR}/db.sqlite"
@property
def SQLALCHEMY_DATABASE_URL(self) -> str:
# Ensure the database directory exists
self.DB_DIR.mkdir(parents=True, exist_ok=True)
return f"sqlite:///{self.DB_DIR}/db.sqlite"
# CORS
BACKEND_CORS_ORIGINS: List[AnyHttpUrl] = []

View File

@ -3,8 +3,7 @@ from sqlalchemy.orm import sessionmaker
from app.core.config import settings
# Ensure the DB directory exists
settings.DB_DIR.mkdir(parents=True, exist_ok=True)
# Note: Directory creation is now handled in the settings property
engine = create_engine(
settings.SQLALCHEMY_DATABASE_URL,

View File

@ -1,17 +1,28 @@
import sys
from logging.config import fileConfig
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
project_root = Path(__file__).parent.parent
sys.path.insert(0, str(project_root))
# Import from the application models
from app.db.base import Base
from app.db.base import Base # noqa
from app.core.config import settings # noqa
# this is the Alembic Config object, which provides
# access to the values within the .ini file in use.
config = context.config
# Override the SQLAlchemy URL with the one from our settings
# This ensures we use the same database URL as the application
config.set_main_option("sqlalchemy.url", settings.SQLALCHEMY_DATABASE_URL)
# Interpret the config file for Python logging.
# This line sets up loggers basically.
fileConfig(config.config_file_name)