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:
parent
c8aed27755
commit
667fee7466
@ -35,7 +35,8 @@ script_location = migrations
|
|||||||
# are written from script.py.mako
|
# are written from script.py.mako
|
||||||
# output_encoding = utf-8
|
# 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]
|
||||||
# post_write_hooks defines scripts or Python functions that are run
|
# post_write_hooks defines scripts or Python functions that are run
|
||||||
|
@ -21,7 +21,12 @@ class Settings(BaseSettings):
|
|||||||
|
|
||||||
# Database
|
# Database
|
||||||
DB_DIR: Path = Path("/app") / "storage" / "db"
|
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
|
# CORS
|
||||||
BACKEND_CORS_ORIGINS: List[AnyHttpUrl] = []
|
BACKEND_CORS_ORIGINS: List[AnyHttpUrl] = []
|
||||||
|
@ -3,8 +3,7 @@ from sqlalchemy.orm import sessionmaker
|
|||||||
|
|
||||||
from app.core.config import settings
|
from app.core.config import settings
|
||||||
|
|
||||||
# Ensure the DB directory exists
|
# Note: Directory creation is now handled in the settings property
|
||||||
settings.DB_DIR.mkdir(parents=True, exist_ok=True)
|
|
||||||
|
|
||||||
engine = create_engine(
|
engine = create_engine(
|
||||||
settings.SQLALCHEMY_DATABASE_URL,
|
settings.SQLALCHEMY_DATABASE_URL,
|
||||||
|
@ -1,17 +1,28 @@
|
|||||||
|
import sys
|
||||||
from logging.config import fileConfig
|
from logging.config import fileConfig
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
from sqlalchemy import engine_from_config
|
from sqlalchemy import engine_from_config
|
||||||
from sqlalchemy import pool
|
from sqlalchemy import pool
|
||||||
|
|
||||||
from alembic import context
|
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
|
# 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
|
# this is the Alembic Config object, which provides
|
||||||
# access to the values within the .ini file in use.
|
# access to the values within the .ini file in use.
|
||||||
config = context.config
|
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.
|
# Interpret the config file for Python logging.
|
||||||
# This line sets up loggers basically.
|
# This line sets up loggers basically.
|
||||||
fileConfig(config.config_file_name)
|
fileConfig(config.config_file_name)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user