Fix Alembic migration error and database path resolution

- Fix 'No module named app' error by adding project root to Python path
- Ensure consistent database path between app and migrations
- Update database configuration to work in both development and deployment
- Set appropriate SQLite dialect configuration with render_as_batch
- Update documentation with additional migration instructions
- Fix imports order to comply with linting standards
This commit is contained in:
Automated Action 2025-05-17 21:06:06 +00:00
parent 938b6d4153
commit b6561799a2
3 changed files with 31 additions and 6 deletions

View File

@ -68,7 +68,11 @@ pip install -r requirements.txt
4. Run database migrations: 4. Run database migrations:
```bash ```bash
# Make sure you are in the project root directory
alembic upgrade head alembic upgrade head
# If you encounter any module import errors, you can also try:
PYTHONPATH=$PWD alembic upgrade head
``` ```
5. Start the application: 5. Start the application:

View File

@ -1,15 +1,25 @@
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 pool
from alembic import context from alembic import context
from app.db.base import Base from sqlalchemy import engine_from_config, pool
# Add the project root directory to Python's module search path
parent_dir = Path(__file__).parents[1]
sys.path.append(str(parent_dir))
# Import models after setting up the path
from app.db.base import Base # noqa
from app.db.session import SQLALCHEMY_DATABASE_URL # 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 in the alembic.ini file
config.set_main_option("sqlalchemy.url", 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)
@ -78,4 +88,4 @@ def run_migrations_online():
if context.is_offline_mode(): if context.is_offline_mode():
run_migrations_offline() run_migrations_offline()
else: else:
run_migrations_online() run_migrations_online()

View File

@ -1,8 +1,19 @@
from pathlib import Path from pathlib import Path
import os
from sqlalchemy import create_engine from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker from sqlalchemy.orm import sessionmaker
DB_DIR = Path("/app") / "storage" / "db" # Get the project root directory
BASE_DIR = Path(__file__).resolve().parent.parent.parent
# Ensure this works in both development and deployment environments
if os.path.exists("/app"):
# Docker/deployment environment
DB_DIR = Path("/app/storage/db")
else:
# Development environment
DB_DIR = BASE_DIR / "storage" / "db"
DB_DIR.mkdir(parents=True, exist_ok=True) DB_DIR.mkdir(parents=True, exist_ok=True)
SQLALCHEMY_DATABASE_URL = f"sqlite:///{DB_DIR}/db.sqlite" SQLALCHEMY_DATABASE_URL = f"sqlite:///{DB_DIR}/db.sqlite"