11from __future__ import annotations
22
33import contextlib
4+ import logging
45from typing import Any
56from typing import Dict
67from typing import Iterator
1718from . import render
1819from .. import util
1920from ..operations import ops
21+ from ..runtime .plugins import Plugin
2022from ..util import sqla_compat
2123
22- """Provide the 'autogenerate' feature which can produce migration operations
23- automatically."""
24-
2524if TYPE_CHECKING :
2625 from sqlalchemy .engine import Connection
2726 from sqlalchemy .engine import Dialect
4241 from ..script .base import Script
4342 from ..script .base import ScriptDirectory
4443 from ..script .revision import _GetRevArg
44+ from ..util import PriorityDispatcher
45+
46+
47+ log = logging .getLogger (__name__ )
4548
4649
4750def compare_metadata (context : MigrationContext , metadata : MetaData ) -> Any :
@@ -304,7 +307,7 @@ class AutogenContext:
304307
305308 """
306309
307- dialect : Optional [ Dialect ] = None
310+ dialect : Dialect
308311 """The :class:`~sqlalchemy.engine.Dialect` object currently in use.
309312
310313 This is normally obtained from the
@@ -326,9 +329,11 @@ class AutogenContext:
326329
327330 """
328331
329- migration_context : MigrationContext = None # type: ignore[assignment]
332+ migration_context : MigrationContext
330333 """The :class:`.MigrationContext` established by the ``env.py`` script."""
331334
335+ comparators : PriorityDispatcher
336+
332337 def __init__ (
333338 self ,
334339 migration_context : MigrationContext ,
@@ -346,6 +351,19 @@ def __init__(
346351 "the database for schema information"
347352 )
348353
354+ # branch off from the "global" comparators. This collection
355+ # is empty in Alembic except that it is populated by third party
356+ # extensions that don't use the plugin system. so we will build
357+ # off of whatever is in there.
358+ if autogenerate :
359+ self .comparators = compare .comparators .branch ()
360+ Plugin .populate_autogenerate_priority_dispatch (
361+ self .comparators ,
362+ include_plugins = migration_context .opts .get (
363+ "autogenerate_plugins" , ["alembic.autogenerate.*" ]
364+ ),
365+ )
366+
349367 if opts is None :
350368 opts = migration_context .opts
351369
@@ -380,9 +398,8 @@ def __init__(
380398 self ._name_filters = name_filters
381399
382400 self .migration_context = migration_context
383- if self .migration_context is not None :
384- self .connection = self .migration_context .bind
385- self .dialect = self .migration_context .dialect
401+ self .connection = self .migration_context .bind
402+ self .dialect = self .migration_context .dialect
386403
387404 self .imports = set ()
388405 self .opts : Dict [str , Any ] = opts
0 commit comments