Fix database migration error with proper Python path configuration

- Added Python path configuration in migrations/env.py to correctly import app modules
- Fixed SQLite database path configuration
- Fixed migration revision ID format
- Added proper error handling for imports

Generated with BackendIM... (backend.im)
This commit is contained in:
Automated Action 2025-05-12 13:00:10 +00:00
parent 54bf9880b9
commit 4a875f5f83
6 changed files with 52 additions and 9 deletions

View File

@ -35,7 +35,7 @@ script_location = migrations
# are written from script.py.mako
# output_encoding = utf-8
sqlalchemy.url = sqlite:///app/storage/db/db.sqlite
sqlalchemy.url = sqlite:////app/storage/db/db.sqlite
[post_write_hooks]
# post_write_hooks defines scripts or Python functions that are run

View File

@ -5,6 +5,15 @@ from sqlalchemy import pool
from alembic import context
from pathlib import Path
import sys
import os
# Get the directory containing this file (migrations directory)
migrations_dir = os.path.dirname(os.path.abspath(__file__))
# Get the root project directory (parent of migrations directory)
project_root = os.path.dirname(migrations_dir)
# Add the project root to sys.path
sys.path.insert(0, project_root)
# this is the Alembic Config object, which provides
# access to the values within the .ini file in use.
@ -14,10 +23,16 @@ config = context.config
# This line sets up loggers basically.
fileConfig(config.config_file_name)
# add your model's MetaData object here
# for 'autogenerate' support
from app.db.base import Base
target_metadata = Base.metadata
# Import Base after adding project_root to sys.path
try:
from app.db.base import Base
target_metadata = Base.metadata
except ImportError as e:
print(f"Import error: {e}")
# Fallback to direct import if module structure is an issue
sys.path.append(os.path.join(project_root, 'app'))
from db.base import Base
target_metadata = Base.metadata
# other values from the config, defined by the needs of env.py,
# can be acquired:
@ -25,9 +40,13 @@ target_metadata = Base.metadata
# ... etc.
# Ensure the DB directory exists
DB_DIR = Path("/app") / "storage" / "db"
DB_DIR = Path("/app/storage/db")
DB_DIR.mkdir(parents=True, exist_ok=True)
# Override sqlalchemy.url from the INI file
DB_PATH = DB_DIR / "db.sqlite"
config.set_main_option('sqlalchemy.url', f"sqlite:///{DB_PATH}")
def run_migrations_offline():
"""Run migrations in 'offline' mode.

View File

@ -1,6 +1,6 @@
"""initial migration
Revision ID: 001
Revision ID: 00001
Revises:
Create Date: 2025-05-12
@ -11,7 +11,7 @@ from sqlalchemy import Column, Integer, String, Float, DateTime, Text, Boolean,
import enum
# revision identifiers, used by Alembic.
revision = '001'
revision = '00001'
down_revision = None
branch_labels = None
depends_on = None

View File

@ -0,0 +1,24 @@
"""fix migration identifier
Revision ID: 83463d3f6b97
Revises: 00001
Create Date: 2025-05-12 12:59:34.013735
"""
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision = '83463d3f6b97'
down_revision = '00001'
branch_labels = None
depends_on = None
def upgrade():
pass
def downgrade():
pass