from __future__ import with_statement import os import logging import importlib.util from alembic import context from sqlalchemy import engine_from_config, pool from logging.config import fileConfig from flask import current_app from app import db # ----------------------------- # 🔍 Automatically import all plugin models # ----------------------------- import glob import importlib.util plugin_model_paths = glob.glob(os.path.join("plugins", "*", "models.py")) for path in plugin_model_paths: module_name = path.replace("/", ".").replace(".py", "") try: spec = importlib.util.spec_from_file_location(module_name, path) module = importlib.util.module_from_spec(spec) spec.loader.exec_module(module) print(f"✅ Loaded: {module_name}") except Exception as e: print(f"❌ Failed to load {module_name}: {e}") # ----------------------------- config = context.config fileConfig(config.config_file_name) logger = logging.getLogger('alembic.env') target_metadata = db.metadata def run_migrations_offline(): context.configure( url=current_app.config.get("SQLALCHEMY_DATABASE_URI"), target_metadata=target_metadata, literal_binds=True, dialect_opts={"paramstyle": "named"}, ) with context.begin_transaction(): context.run_migrations() print("🧠 Alembic sees these tables:") print(sorted(db.metadata.tables.keys())) def run_migrations_online(): connectable = db.engine 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()