Fix database migration path issues for container environment
- Updated migrations/env.py to handle Python import paths correctly - Fixed app/db/base.py to import all required models - Updated database path in app/db/session.py to work in both local and container environments - Updated SQLite path in alembic.ini for container compatibility
This commit is contained in:
parent
06df0285b1
commit
cf63bb6a60
@ -36,7 +36,8 @@ script_location = migrations
|
||||
# output_encoding = utf-8
|
||||
|
||||
# SQLite URL example
|
||||
sqlalchemy.url = sqlite:////app/storage/db/db.sqlite
|
||||
# For container environment, use /app/repo/storage/db/db.sqlite
|
||||
sqlalchemy.url = sqlite:////app/repo/storage/db/db.sqlite
|
||||
|
||||
[post_write_hooks]
|
||||
# post_write_hooks defines scripts or Python functions that are run
|
||||
|
@ -2,9 +2,19 @@ from sqlalchemy import create_engine
|
||||
from sqlalchemy.ext.declarative import declarative_base
|
||||
from sqlalchemy.orm import sessionmaker
|
||||
from pathlib import Path
|
||||
import os
|
||||
|
||||
# Check if we're running in the container environment
|
||||
IN_CONTAINER = os.path.exists('/app/repo')
|
||||
|
||||
# Create the directory for the database if it doesn't exist
|
||||
DB_DIR = Path("/app") / "storage" / "db"
|
||||
if IN_CONTAINER:
|
||||
# Container path
|
||||
DB_DIR = Path("/app") / "repo" / "storage" / "db"
|
||||
else:
|
||||
# Local development path
|
||||
DB_DIR = Path("/app") / "storage" / "db"
|
||||
|
||||
DB_DIR.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
SQLALCHEMY_DATABASE_URL = f"sqlite:///{DB_DIR}/db.sqlite"
|
||||
|
@ -1,10 +1,24 @@
|
||||
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
|
||||
from app.db.base import Base
|
||||
|
||||
# Add the parent directory to sys.path so that 'app' can be imported
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
|
||||
|
||||
try:
|
||||
# Try to import using the container path structure
|
||||
from app.db.base import Base
|
||||
except ImportError:
|
||||
# If that fails, try to import using the local path structure
|
||||
project_root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||||
sys.path.insert(0, project_root)
|
||||
from app.db.base import Base
|
||||
|
||||
# this is the Alembic Config object, which provides
|
||||
# access to the values within the .ini file in use.
|
||||
|
Loading…
x
Reference in New Issue
Block a user