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:
parent
54bf9880b9
commit
4a875f5f83
@ -35,7 +35,7 @@ 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
|
sqlalchemy.url = sqlite:////app/storage/db/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
|
||||||
|
@ -5,6 +5,15 @@ from sqlalchemy import pool
|
|||||||
|
|
||||||
from alembic import context
|
from alembic import context
|
||||||
from pathlib import Path
|
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
|
# 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.
|
||||||
@ -14,10 +23,16 @@ config = context.config
|
|||||||
# This line sets up loggers basically.
|
# This line sets up loggers basically.
|
||||||
fileConfig(config.config_file_name)
|
fileConfig(config.config_file_name)
|
||||||
|
|
||||||
# add your model's MetaData object here
|
# Import Base after adding project_root to sys.path
|
||||||
# for 'autogenerate' support
|
try:
|
||||||
from app.db.base import Base
|
from app.db.base import Base
|
||||||
target_metadata = Base.metadata
|
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,
|
# other values from the config, defined by the needs of env.py,
|
||||||
# can be acquired:
|
# can be acquired:
|
||||||
@ -25,9 +40,13 @@ target_metadata = Base.metadata
|
|||||||
# ... etc.
|
# ... etc.
|
||||||
|
|
||||||
# Ensure the DB directory exists
|
# 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)
|
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():
|
def run_migrations_offline():
|
||||||
"""Run migrations in 'offline' mode.
|
"""Run migrations in 'offline' mode.
|
||||||
|
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
"""initial migration
|
"""initial migration
|
||||||
|
|
||||||
Revision ID: 001
|
Revision ID: 00001
|
||||||
Revises:
|
Revises:
|
||||||
Create Date: 2025-05-12
|
Create Date: 2025-05-12
|
||||||
|
|
||||||
"""
|
"""
|
||||||
@ -11,7 +11,7 @@ from sqlalchemy import Column, Integer, String, Float, DateTime, Text, Boolean,
|
|||||||
import enum
|
import enum
|
||||||
|
|
||||||
# revision identifiers, used by Alembic.
|
# revision identifiers, used by Alembic.
|
||||||
revision = '001'
|
revision = '00001'
|
||||||
down_revision = None
|
down_revision = None
|
||||||
branch_labels = None
|
branch_labels = None
|
||||||
depends_on = None
|
depends_on = None
|
||||||
|
24
migrations/versions/83463d3f6b97_fix_migration_identifier.py
Normal file
24
migrations/versions/83463d3f6b97_fix_migration_identifier.py
Normal 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
|
Binary file not shown.
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user