Skip to content

Commit 53b923b

Browse files
committed
Add reversed option when listing history of revisions
1 parent c94816e commit 53b923b

File tree

4 files changed

+23
-4
lines changed

4 files changed

+23
-4
lines changed

alembic/command.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -561,6 +561,7 @@ def history(
561561
rev_range: Optional[str] = None,
562562
verbose: bool = False,
563563
indicate_current: bool = False,
564+
reversed: bool = False,
564565
) -> None:
565566
"""List changeset scripts in chronological order.
566567
@@ -592,7 +593,7 @@ def history(
592593

593594
def _display_history(config, script, base, head, currents=()):
594595
for sc in script.walk_revisions(
595-
base=base or "base", head=head or "heads"
596+
base=base or "base", head=head or "heads", reversed=reversed
596597
):
597598
if indicate_current:
598599
sc._db_current_indicator = sc.revision in currents

alembic/config.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -756,6 +756,14 @@ def __init__(self, prog: Optional[str] = None) -> None:
756756
"generating one",
757757
),
758758
),
759+
"reversed": (
760+
"-R",
761+
"--reversed",
762+
dict(
763+
action="store_true",
764+
help="Show the history in the reversed order",
765+
),
766+
),
759767
"version_path": (
760768
"--version-path",
761769
dict(

alembic/script/base.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ def _catch_revision_errors(
247247
raise util.CommandError(err.args[0]) from err
248248

249249
def walk_revisions(
250-
self, base: str = "base", head: str = "heads"
250+
self, base: str = "base", head: str = "heads", reversed: bool = False
251251
) -> Iterator[Script]:
252252
"""Iterate through all revisions.
253253
@@ -261,7 +261,11 @@ def walk_revisions(
261261
"""
262262
with self._catch_revision_errors(start=base, end=head):
263263
for rev in self.revision_map.iterate_revisions(
264-
head, base, inclusive=True, assert_relative_length=False
264+
head,
265+
base,
266+
inclusive=True,
267+
assert_relative_length=False,
268+
reversed=reversed,
265269
):
266270
yield cast(Script, rev)
267271

alembic/script/revision.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -793,6 +793,7 @@ def iterate_revisions(
793793
inclusive: bool = False,
794794
assert_relative_length: bool = True,
795795
select_for_downgrade: bool = False,
796+
reversed: bool = False,
796797
) -> Iterator[Revision]:
797798
"""Iterate through script revisions, starting at the given
798799
upper revision identifier and ending at the lower.
@@ -819,7 +820,12 @@ def iterate_revisions(
819820
assert_relative_length=assert_relative_length,
820821
)
821822

822-
for node in self._topological_sort(revisions, heads):
823+
topological_sort = (
824+
self._topological_sort(revisions, heads)[::-1]
825+
if reversed
826+
else self._topological_sort(revisions, heads)
827+
)
828+
for node in topological_sort:
823829
yield not_none(self.get_revision(node))
824830

825831
def _get_descendant_nodes(

0 commit comments

Comments
 (0)