124 lines
3.7 KiB
Python

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
# 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
# First, import the Base class
from app.db.base_class import Base
# Then import the models to ensure they're registered with SQLAlchemy
import app.db.base # noqa: F401
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)
# First, import the Base class
from app.db.base_class import Base
# Then import the models to ensure they're registered with SQLAlchemy
import app.db.base # noqa: F401
# this is the Alembic Config object, which provides
# access to the values within the .ini file in use.
config = context.config
# Set up the database URL dynamically based on environment
# Check if we're running in the container environment at /app/repo
IN_CONTAINER = os.path.exists('/app/repo')
# Set the database directory path based on the environment
if IN_CONTAINER:
# Container path
DB_DIR = Path("/app/repo/storage/db")
else:
# Local development path - using either the project directory or fallback to /app
PROJECT_ROOT = Path("/projects/bloggingplatformapi-lncxqv")
if PROJECT_ROOT.exists():
DB_DIR = PROJECT_ROOT / "storage" / "db"
else:
DB_DIR = Path("/app/storage/db")
# Create the directory for the database if it doesn't exist
DB_DIR.mkdir(parents=True, exist_ok=True)
SQLALCHEMY_DATABASE_URL = f"sqlite:///{DB_DIR}/db.sqlite"
# Override the URL in the config with our dynamic URL
config.set_main_option("sqlalchemy.url", SQLALCHEMY_DATABASE_URL)
# Interpret the config file for Python logging.
# This line sets up loggers basically.
fileConfig(config.config_file_name)
# add your model's MetaData object here
# for 'autogenerate' support
target_metadata = Base.metadata
# other values from the config, defined by the needs of env.py,
# can be acquired:
# my_important_option = config.get_main_option("my_important_option")
# ... etc.
def run_migrations_offline():
"""Run migrations in 'offline' mode.
This configures the context with just a URL
and not an Engine, though an Engine is acceptable
here as well. By skipping the Engine creation
we don't even need a DBAPI to be available.
Calls to context.execute() here emit the given string to the
script output.
"""
url = config.get_main_option("sqlalchemy.url")
context.configure(
url=url,
target_metadata=target_metadata,
literal_binds=True,
dialect_opts={"paramstyle": "named"},
)
with context.begin_transaction():
context.run_migrations()
def run_migrations_online():
"""Run migrations in 'online' mode.
In this scenario we need to create an Engine
and associate a connection with the context.
"""
connectable = engine_from_config(
config.get_section(config.config_ini_section),
prefix="sqlalchemy.",
poolclass=pool.NullPool,
)
with connectable.connect() as connection:
is_sqlite = connection.dialect.name == 'sqlite'
context.configure(
connection=connection,
target_metadata=target_metadata,
render_as_batch=is_sqlite, # Key configuration for SQLite
compare_type=True,
)
with context.begin_transaction():
context.run_migrations()
if context.is_offline_mode():
run_migrations_offline()
else:
run_migrations_online()