# Alembic env.py - Migration environment configuration from logging.config import fileConfig from sqlalchemy import create_engine from alembic import context # Add app to path import sys import os sys.path.insert(0, os.path.dirname(os.path.dirname(__file__))) # Load app settings from app.config import get_settings from app.database import Base # Import all models so they register with Base.metadata from app.auth import models # noqa: F401 from app.reports import models as report_models # noqa: F401 # this is the Alembic Config object config = context.config # Interpret the config file for Python logging if config.config_file_name is not None: fileConfig(config.config_file_name) # Set target metadata target_metadata = Base.metadata # Load database URL from app settings settings = get_settings() db_url = settings.database_url if db_url.startswith("sqlite+aiosqlite://"): db_url = db_url.replace("sqlite+aiosqlite://", "sqlite://") elif db_url.startswith("postgresql+asyncpg://"): db_url = db_url.replace("postgresql+asyncpg://", "postgresql://") config.set_main_option("sqlalchemy.url", db_url) def run_migrations_offline() -> None: """Run migrations in 'offline' mode.""" 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() -> None: """Run migrations in 'online' mode.""" connectable = create_engine(config.get_main_option("sqlalchemy.url")) with connectable.connect() as connection: context.configure( connection=connection, target_metadata=target_metadata ) with context.begin_transaction(): context.run_migrations() if context.is_offline_mode(): run_migrations_offline() else: run_migrations_online()