]> git.ipfire.org Git - thirdparty/sqlalchemy/alembic.git/commitdiff
Add EnvironmentContext and Config to MigrationContext
authorMarc Abramowitz <marc@marc-abramowitz.com>
Sat, 17 May 2014 18:14:38 +0000 (11:14 -0700)
committerMarc Abramowitz <marc@marc-abramowitz.com>
Mon, 19 May 2014 20:15:24 +0000 (13:15 -0700)
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
alembic/migration.py

index f8875a2ad69864a04781d4547b0a494fe0cc4e24..c3e7a38da4be51a07d3a54062ca08cc12daf3072 100644 (file)
@@ -661,6 +661,7 @@ class EnvironmentContext(object):
             connection=connection,
             url=url,
             dialect_name=dialect_name,
+            environment_context=self,
             opts=opts
         )
 
index e554515b7a6be9fb549ac181a1d880100e670dba..0fffbe5bbb3d063e7a2557c5f33c47b4f85f7a7d 100644 (file)
@@ -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