diff --git a/alembic/command.py b/alembic/command.py index 530af17..5ba6d6a 100644 --- a/alembic/command.py +++ b/alembic/command.py @@ -330,9 +330,6 @@ def stamp(config, revision, sql=False, tag=None): if not sql: raise util.CommandError("Range revision not allowed") starting_rev, revision = revision.split(':', 2) - starting_rev = script.get_revision(starting_rev) - if starting_rev is not None: - starting_rev = starting_rev.revision def do_stamp(rev, context): return script._stamp_revs(revision, rev) @@ -347,5 +344,3 @@ def stamp(config, revision, sql=False, tag=None): tag=tag ): script.run_env() - - diff --git a/alembic/migration.py b/alembic/migration.py index 098cf69..a2241fd 100644 --- a/alembic/migration.py +++ b/alembic/migration.py @@ -64,7 +64,6 @@ class MigrationContext(object): self.opts = opts self.dialect = dialect self.script = opts.get('script') - as_sql = opts.get('as_sql', False) transactional_ddl = opts.get("transactional_ddl") @@ -229,7 +228,12 @@ class MigrationContext(object): """ if self.as_sql: - return util.to_tuple(self._start_from_rev, default=()) + start_from_rev = self._start_from_rev + if start_from_rev is not None and self.script: + start_from_rev = \ + self.script.get_revision(start_from_rev).revision + + return util.to_tuple(start_from_rev, default=()) else: if self._start_from_rev: raise util.CommandError( diff --git a/docs/build/changelog.rst b/docs/build/changelog.rst index 225db44..d8d2fa3 100644 --- a/docs/build/changelog.rst +++ b/docs/build/changelog.rst @@ -6,6 +6,24 @@ Changelog .. changelog:: :version: 0.7.5 + .. change:: + :tags: bug, commands + :tickets: 269 + + Fixed bug where using a partial revision identifier as the + "starting revision" in ``--sql`` mode in a downgrade operation + would fail to resolve properly. + + As a side effect of this change, the + :meth:`.EnvironmentContext.get_starting_revision_argument` + method will return the "starting" revision in its originally- + given "partial" form in all cases, whereas previously when + running within the :meth:`.command.stamp` command, it would have + been resolved to a full number before passing it to the + :class:`.EnvironmentContext`. The resolution of this value to + a real revision number has basically been moved to a more fundamental + level within the offline migration process. + .. change:: :tags: feature, commands diff --git a/tests/test_offline_environment.py b/tests/test_offline_environment.py index 684e350..02e592f 100644 --- a/tests/test_offline_environment.py +++ b/tests/test_offline_environment.py @@ -80,6 +80,7 @@ assert context.get_revision_argument() == '%s' command.stamp(self.cfg, b, sql=True) command.downgrade(self.cfg, "%s:%s" % (c, b), sql=True) + def test_destination_rev_post_context(self): env_file_fixture(""" context.configure(dialect_name='sqlite') @@ -175,3 +176,36 @@ assert not context.requires_connection() command.upgrade(self.cfg, "%s:%s" % (a, d.revision), sql=True) assert not re.match(r".*-- .*and multiline", buf.getvalue(), re.S | re.M) + + def test_starting_rev_pre_context_abbreviated(self): + env_file_fixture(""" +assert context.get_starting_revision_argument() == '%s' +""" % b[0:4]) + command.upgrade(self.cfg, "%s:%s" % (b[0:4], c), sql=True) + command.stamp(self.cfg, "%s:%s" % (b[0:4], c), sql=True) + command.downgrade(self.cfg, "%s:%s" % (b[0:4], a), sql=True) + + def test_destination_rev_pre_context_abbreviated(self): + env_file_fixture(""" +assert context.get_revision_argument() == '%s' +""" % b[0:4]) + command.upgrade(self.cfg, "%s:%s" % (a, b[0:4]), sql=True) + command.stamp(self.cfg, b[0:4], sql=True) + command.downgrade(self.cfg, "%s:%s" % (c, b[0:4]), sql=True) + + def test_starting_rev_context_runs_abbreviated(self): + env_file_fixture(""" +context.configure(dialect_name='sqlite') +context.run_migrations() +""") + command.upgrade(self.cfg, "%s:%s" % (b[0:4], c), sql=True) + command.downgrade(self.cfg, "%s:%s" % (b[0:4], a), sql=True) + + def test_destination_rev_context_runs_abbreviated(self): + env_file_fixture(""" +context.configure(dialect_name='sqlite') +context.run_migrations() +""") + command.upgrade(self.cfg, "%s:%s" % (a, b[0:4]), sql=True) + command.stamp(self.cfg, b[0:4], sql=True) + command.downgrade(self.cfg, "%s:%s" % (c, b[0:4]), sql=True)