summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarc Abramowitz <marc@marc-abramowitz.com>2014-05-17 11:14:38 -0700
committerMarc Abramowitz <marc@marc-abramowitz.com>2014-05-19 13:15:24 -0700
commita4c11c2643b64ee7b83ce1e67406738b8ea39857 (patch)
tree099f56871597574c0a8ab7224a7ff9d1da7f6e3e
parentc384dd6e145d6dc3d178bea9fa08f0ce9b135e6e (diff)
downloadalembic-a4c11c2643b64ee7b83ce1e67406738b8ea39857.tar.gz
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
-rw-r--r--alembic/environment.py1
-rw-r--r--alembic/migration.py14
2 files changed, 13 insertions, 2 deletions
diff --git a/alembic/environment.py b/alembic/environment.py
index f8875a2..c3e7a38 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 e554515..0fffbe5 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