From a4c11c2643b64ee7b83ce1e67406738b8ea39857 Mon Sep 17 00:00:00 2001 From: Marc Abramowitz Date: Sat, 17 May 2014 11:14:38 -0700 Subject: [PATCH] Add EnvironmentContext and Config to MigrationContext This lets migrations do stuff like: op.get_context().config.get_main_option('schema') where `schema` is a custom option that I added to alembic.ini --- alembic/environment.py | 1 + alembic/migration.py | 14 ++++++++++++-- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/alembic/environment.py b/alembic/environment.py index f8875a2a..c3e7a38d 100644 --- a/alembic/environment.py +++ b/alembic/environment.py @@ -661,6 +661,7 @@ class EnvironmentContext(object): connection=connection, url=url, dialect_name=dialect_name, + environment_context=self, opts=opts ) diff --git a/alembic/migration.py b/alembic/migration.py index e554515b..0fffbe5b 100644 --- a/alembic/migration.py +++ b/alembic/migration.py @@ -58,7 +58,8 @@ class MigrationContext(object): op.alter_column("mytable", "somecolumn", nullable=True) """ - def __init__(self, dialect, connection, opts): + def __init__(self, dialect, connection, opts, environment_context=None): + self.environment_context = environment_context self.opts = opts self.dialect = dialect self.script = opts.get('script') @@ -115,6 +116,7 @@ class MigrationContext(object): connection=None, url=None, dialect_name=None, + environment_context=None, opts={}, ): """Create a new :class:`.MigrationContext`. @@ -148,7 +150,7 @@ class MigrationContext(object): else: raise Exception("Connection, url, or dialect_name is required.") - return MigrationContext(dialect, connection, opts) + return MigrationContext(dialect, connection, opts, environment_context) def begin_transaction(self, _per_migration=False): @@ -305,6 +307,14 @@ class MigrationContext(object): """ return self.connection + @property + def config(self): + """Return the :class:`.Config` used by the current environment, if any.""" + if self.environment_context: + return self.environment_context.config + else: + return None + def _compare_type(self, inspector_column, metadata_column): if self._user_compare_type is False: return False -- 2.47.2