]> git.ipfire.org Git - thirdparty/sqlalchemy/alembic.git/commitdiff
Add indicate-current option into history command
authormisebox <misebox@gmail.com>
Wed, 7 Mar 2018 21:20:00 +0000 (16:20 -0500)
committerMike Bayer <mike_mp@zzzcomputing.com>
Wed, 7 Mar 2018 22:09:12 +0000 (17:09 -0500)
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

alembic/command.py
alembic/config.py
alembic/script/base.py
docs/build/unreleased/481.rst [new file with mode: 0644]
tests/test_command.py

index 867500518ea8bd4428bba847d1973588889ca195..cd61fd1314a83146238988b9e52a24a9242f3e77 100644 (file)
@@ -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)
index 46ccb91a4e76db63da207b63afca8a51b7264836..0e8d9e436e7e2618258c9572d0f30c9cf97b3a5b 100644 (file)
@@ -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 = {
index 42dd469b5e29a64832a8a8d1cf4c40de909c8877..12e15108a0b140177965a7a6a30e8048d28a207d 100644 (file)
@@ -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 (file)
index 0000000..b94a09c
--- /dev/null
@@ -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.
index 995911f979e2d3cfdca536b5b2f21c83aef3d1ac..3f3daf50f7b079ea4abace83363f89dec2678a97 100644 (file)
@@ -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):