""" migrations_tenant/env.py ------------------------ Standalone Alembic environment for running the EXISTING tenant migration chain (migrations/versions) against an ARBITRARY tenant database. Unlike migrations/env.py — which is Flask-Migrate-coupled and always targets the app's default database via current_app — this reads sqlalchemy.url straight from the Alembic config supplied per-tenant by control/tenant_migrate.py, and reuses the same version scripts through version_locations. No Flask import, so it can run for any tenant DB outside an app context. The normal `flask db ...` workflow is unaffected; it still uses migrations/env.py. """ from logging.config import fileConfig from sqlalchemy import create_engine, pool from alembic import context config = context.config # Only configure logging if an ini file was supplied (the runner builds the # Config programmatically, so this is typically skipped). if config.config_file_name is not None: try: fileConfig(config.config_file_name) except Exception: pass # Raw-SQL migrations with INFORMATION_SCHEMA guards — autogenerate is not used, # so no target metadata is required here. target_metadata = None def run_migrations_offline(): context.configure( url=config.get_main_option('sqlalchemy.url'), target_metadata=target_metadata, literal_binds=True, compare_type=False, ) with context.begin_transaction(): context.run_migrations() def run_migrations_online(): url = config.get_main_option('sqlalchemy.url') connectable = create_engine(url, poolclass=pool.NullPool, future=True) try: with connectable.connect() as connection: context.configure(connection=connection, target_metadata=target_metadata) with context.begin_transaction(): context.run_migrations() finally: connectable.dispose() if context.is_offline_mode(): run_migrations_offline() else: run_migrations_online()