From: misebox Date: Wed, 7 Mar 2018 21:20:00 +0000 (-0500) Subject: Add indicate-current option into history command X-Git-Tag: rel_0_9_9~2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=22294fff73c017529d0c12bb212f789e600d9965;p=thirdparty%2Fsqlalchemy%2Falembic.git Add indicate-current option into history command Added new flag ``--indicate-current`` to the ``alembic history`` command. When listing versions, it will include the token "(current)" to indicate the given version is a current head in the target database. Pull request courtesy Kazutaka Mise. Fixes: #481 Change-Id: I7daa02b455aaba76c50d0e1febbdc6908693d4c9 Pull-request: https://bitbucket.org/zzzeek/alembic/pull-requests/77 --- diff --git a/alembic/command.py b/alembic/command.py index 86750051..cd61fd13 100644 --- a/alembic/command.py +++ b/alembic/command.py @@ -321,7 +321,7 @@ def show(config, rev): config.print_stdout(sc.log_entry) -def history(config, rev_range=None, verbose=False): +def history(config, rev_range=None, verbose=False, indicate_current=False): """List changeset scripts in chronological order. :param config: a :class:`.Config` instance. @@ -330,6 +330,10 @@ def history(config, rev_range=None, verbose=False): :param verbose: output in verbose mode. + :param indicate_current: indicate current revision. + + ..versionadded:: 0.9.9 + """ script = ScriptDirectory.from_config(config) @@ -344,25 +348,29 @@ def history(config, rev_range=None, verbose=False): environment = util.asbool( config.get_main_option("revision_environment") - ) + ) or indicate_current - def _display_history(config, script, base, head): + def _display_history(config, script, base, head, currents=()): for sc in script.walk_revisions( base=base or "base", head=head or "heads"): + + if indicate_current: + sc._db_current_indicator = sc.revision in currents + config.print_stdout( sc.cmd_format( verbose=verbose, include_branches=True, include_doc=True, include_parents=True)) - def _display_history_w_current(config, script, base=None, head=None): + def _display_history_w_current(config, script, base, head): def _display_current_history(rev, context): - if head is None: - _display_history(config, script, base, rev) - elif base is None: - _display_history(config, script, rev, head) + if head == 'current': + _display_history(config, script, base, rev, rev) + elif base == 'current': + _display_history(config, script, rev, head, rev) else: - _display_history(config, script, base, head) + _display_history(config, script, base, head, rev) return [] with EnvironmentContext( @@ -372,11 +380,7 @@ def history(config, rev_range=None, verbose=False): ): script.run_env() - if base == "current": - _display_history_w_current(config, script, head=head) - elif head == "current": - _display_history_w_current(config, script, base=base) - elif environment: + if base == "current" or head == "current" or environment: _display_history_w_current(config, script, base, head) else: _display_history(config, script, base, head) diff --git a/alembic/config.py b/alembic/config.py index 46ccb91a..0e8d9e43 100644 --- a/alembic/config.py +++ b/alembic/config.py @@ -388,6 +388,13 @@ class CommandLine(object): action="store", help="Specify a revision range; " "format is [start]:[end]") + ), + 'indicate_current': ( + "-i", "--indicate-current", + dict( + action="store_true", + help="Indicate the current revision" + ) ) } positional_help = { diff --git a/alembic/script/base.py b/alembic/script/base.py index 42dd469b..12e15108 100644 --- a/alembic/script/base.py +++ b/alembic/script/base.py @@ -635,6 +635,10 @@ class Script(revision.Revision): path = None """Filesystem path of the script.""" + _db_current_indicator = None + """Utility variable which when set will cause string output to indicate + this is a "current" version in some database""" + @property def doc(self): """Return the docstring given in the script.""" @@ -655,11 +659,12 @@ class Script(revision.Revision): @property def log_entry(self): - entry = "Rev: %s%s%s%s\n" % ( + entry = "Rev: %s%s%s%s%s\n" % ( self.revision, " (head)" if self.is_head else "", " (branchpoint)" if self.is_branch_point else "", " (mergepoint)" if self.is_merge_point else "", + " (current)" if self._db_current_indicator else "" ) if self.is_merge_point: entry += "Merges: %s\n" % (self._format_down_revision(), ) @@ -715,10 +720,11 @@ class Script(revision.Revision): if include_branches and self.branch_labels: text += " (%s)" % util.format_as_comma(self.branch_labels) if head_indicators or tree_indicators: - text += "%s%s" % ( + text += "%s%s%s" % ( " (head)" if self._is_real_head else "", " (effective head)" if self.is_head and - not self._is_real_head else "" + not self._is_real_head else "", + " (current)" if self._db_current_indicator else "" ) if tree_indicators: text += "%s%s" % ( diff --git a/docs/build/unreleased/481.rst b/docs/build/unreleased/481.rst new file mode 100644 index 00000000..b94a09c6 --- /dev/null +++ b/docs/build/unreleased/481.rst @@ -0,0 +1,8 @@ +.. change:: + :tags: feature, commands + :tickets: 481 + + Added new flag ``--indicate-current`` to the ``alembic history`` command. + When listing versions, it will include the token "(current)" to indicate + the given version is a current head in the target database. Pull request + courtesy Kazutaka Mise. diff --git a/tests/test_command.py b/tests/test_command.py index 995911f9..3f3daf50 100644 --- a/tests/test_command.py +++ b/tests/test_command.py @@ -65,16 +65,19 @@ finally: """) - def _eq_cmd_output(self, buf, expected, env_token=False): + def _eq_cmd_output(self, buf, expected, env_token=False, currents=()): script = ScriptDirectory.from_config(self.cfg) # test default encode/decode behavior as well, # rev B has a non-ascii char in it + a coding header. - assert_lines = [ - script.get_revision(rev).log_entry - for rev in expected - ] + assert_lines = [] + for _id in expected: + rev = script.get_revision(_id) + if _id in currents: + rev._db_current_indicator = True + assert_lines.append(rev.log_entry) + if env_token: assert_lines.insert(0, "environment included OK") @@ -155,6 +158,13 @@ finally: command.history(self.cfg, verbose=True) self._eq_cmd_output(buf, [self.c, self.b, self.a], env_token=True) + def test_history_indicate_current(self): + command.stamp(self.cfg, (self.b,)) + self.cfg.stdout = buf = self._buf_fixture() + command.history(self.cfg, indicate_current=True, verbose=True) + self._eq_cmd_output( + buf, [self.c, self.b, self.a], currents=(self.b,), env_token=True) + class CurrentTest(_BufMixin, TestBase):