From: Mike Bayer Date: Fri, 21 Aug 2015 17:06:56 +0000 (-0400) Subject: - add a test and changelog notes for the fact that a non-autogenerate X-Git-Tag: rel_0_8_1~2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=ec7c2b721f73f2770e2ed9ac770bc873065c1e60;p=thirdparty%2Fsqlalchemy%2Falembic.git - add a test and changelog notes for the fact that a non-autogenerate run can render Python code in upgradeops/downgradeops via the process_revision_directives hook. --- diff --git a/alembic/autogenerate/api.py b/alembic/autogenerate/api.py index 0ccaeb78..0bdaa970 100644 --- a/alembic/autogenerate/api.py +++ b/alembic/autogenerate/api.py @@ -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) ) diff --git a/alembic/runtime/environment.py b/alembic/runtime/environment.py index 3b6252c1..b2d0ba7b 100644 --- a/alembic/runtime/environment.py +++ b/alembic/runtime/environment.py @@ -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` diff --git a/docs/build/changelog.rst b/docs/build/changelog.rst index b1d72efc..02a4e16a 100644 --- a/docs/build/changelog.rst +++ b/docs/build/changelog.rst @@ -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 diff --git a/tests/test_script_production.py b/tests/test_script_production.py index c2d180ac..d2e397f1 100644 --- a/tests/test_script_production.py +++ b/tests/test_script_production.py @@ -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))