""" control/migrations/env.py ------------------------- Standalone Alembic environment for the control plane. Unlike the data-plane env (which pulls its engine from the Flask app), this reads the URL directly from CONTROL_DATABASE_URL and targets ControlBase.metadata. No Flask import. """ import os import sys from logging.config import fileConfig from sqlalchemy import engine_from_config, pool from alembic import context # Make the repo root importable so `import control.*` resolves when Alembic # is invoked as `alembic -c control/migrations/alembic.ini ...`. sys.path.insert( 0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..')) ) from control.base import ControlBase, get_control_database_url # noqa: E402 import control.models # noqa: E402,F401 (registers tables on ControlBase.metadata) config = context.config if config.config_file_name is not None: fileConfig(config.config_file_name) # Inject the URL from the environment (escape % for ConfigParser). config.set_main_option( 'sqlalchemy.url', get_control_database_url().replace('%', '%%') ) target_metadata = ControlBase.metadata def run_migrations_offline(): context.configure( url=config.get_main_option('sqlalchemy.url'), target_metadata=target_metadata, literal_binds=True, compare_type=True, ) with context.begin_transaction(): context.run_migrations() def run_migrations_online(): connectable = engine_from_config( config.get_section(config.config_ini_section, {}), prefix='sqlalchemy.', poolclass=pool.NullPool, ) with connectable.connect() as connection: context.configure( connection=connection, target_metadata=target_metadata, compare_type=True, ) with context.begin_transaction(): context.run_migrations() if context.is_offline_mode(): run_migrations_offline() else: run_migrations_online()