]> git.ipfire.org Git - thirdparty/sqlalchemy/alembic.git/commitdiff
- add a test and changelog notes for the fact that a non-autogenerate
authorMike Bayer <mike_mp@zzzcomputing.com>
Fri, 21 Aug 2015 17:06:56 +0000 (13:06 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Fri, 21 Aug 2015 17:06:56 +0000 (13:06 -0400)
run can render Python code in upgradeops/downgradeops via the
process_revision_directives hook.

alembic/autogenerate/api.py
alembic/runtime/environment.py
docs/build/changelog.rst
tests/test_script_production.py

index 0ccaeb78c082de0bb74d63d91003520fab766b3e..0bdaa970f07f9ac526c59d924a9f30ea6e32641a 100644 (file)
@@ -372,7 +372,6 @@ class RevisionContext(object):
                 downgrade_token
             migration_script._needs_render = True
         else:
-            pass
             migration_script._upgrade_ops.append(
                 ops.UpgradeOps([], upgrade_token=upgrade_token)
             )
index 3b6252c1e256a5a4f7c2440bea518162b03b80bc..b2d0ba7bb981e87ff32167015e1cb2f4c933af54 100644 (file)
@@ -697,6 +697,15 @@ class EnvironmentContext(util.ModuleClsProxy):
 
          .. versionadded:: 0.8.0
 
+         .. versionchanged:: 0.8.1 - The
+            :paramref:`.EnvironmentContext.configure.process_revision_directives`
+            hook can append op directives into :class:`.UpgradeOps` and
+            :class:`.DowngradeOps` which will be rendered in Python regardless
+            of whether the ``--autogenerate`` option is in use or not;
+            the ``revision_environment`` configuration variable should be
+            set to "true" in the config to enable this.
+
+
          .. seealso::
 
              :ref:`customizing_revision`
index b1d72efc67424e6fcfcff2ee5c2e563990d6ede7..02a4e16a9bddd45f6ce4b070ab08b5780c2c3c3c 100644 (file)
@@ -6,6 +6,16 @@ Changelog
 .. changelog::
     :version: 0.8.1
 
+    .. change::
+      :tags: feature, autogenerate
+
+      A custom :paramref:`.EnvironmentContext.configure.process_revision_directives`
+      hook can now generate op directives within the :class:`.UpgradeOps`
+      and :class:`.DowngradeOps` containers that will be generated as Python
+      code even when the ``--autogenerate`` flag is False; provided that
+      ``revision_environment=True``, the full render operation will be run
+      even in "offline" mode.
+
     .. change::
       :tags: bug, autogenerate
 
index c2d180ac75a883388681a767962ec0896b31801d..d2e397f1dca3ef59919e3cd2da974eca1eecf2a3 100644 (file)
@@ -329,6 +329,26 @@ def downgrade():
             )
             assert os.path.exists(rev_script.path)
 
+    def test_renders_added_directives_no_autogen(self):
+        m = sa.MetaData()
+
+        def process_revision_directives(context, rev, generate_revisions):
+            generate_revisions[0].upgrade_ops.ops.append(
+                ops.CreateIndexOp("some_index", "some_table", ["a", "b"])
+            )
+
+        with self._env_fixture(process_revision_directives, m):
+            rev = command.revision(
+                self.cfg, message="some message", head="model1@head", sql=True)
+
+        with mock.patch.object(rev.module, "op") as op_mock:
+            rev.module.upgrade()
+        eq_(
+            op_mock.mock_calls,
+            [mock.call.create_index(
+                'some_index', 'some_table', ['a', 'b'], unique=False)]
+        )
+
     def test_autogen(self):
         m = sa.MetaData()
         sa.Table('t', m, sa.Column('x', sa.Integer))