From 53b923b3a47c35cf725d15d34fac2f4e61142a50 Mon Sep 17 00:00:00 2001 From: Aravinth BALAKRISHNAN Date: Wed, 17 Dec 2025 21:05:23 +0100 Subject: [PATCH] Add reversed option when listing history of revisions --- alembic/command.py | 3 ++- alembic/config.py | 8 ++++++++ alembic/script/base.py | 8 ++++++-- alembic/script/revision.py | 8 +++++++- 4 files changed, 23 insertions(+), 4 deletions(-) diff --git a/alembic/command.py b/alembic/command.py index 4897c0d9..39f45a70 100644 --- a/alembic/command.py +++ b/alembic/command.py @@ -561,6 +561,7 @@ def history( rev_range: Optional[str] = None, verbose: bool = False, indicate_current: bool = False, + reversed: bool = False, ) -> None: """List changeset scripts in chronological order. @@ -592,7 +593,7 @@ def history( def _display_history(config, script, base, head, currents=()): for sc in script.walk_revisions( - base=base or "base", head=head or "heads" + base=base or "base", head=head or "heads", reversed=reversed ): if indicate_current: sc._db_current_indicator = sc.revision in currents diff --git a/alembic/config.py b/alembic/config.py index 121a4459..06dd3d30 100644 --- a/alembic/config.py +++ b/alembic/config.py @@ -756,6 +756,14 @@ def __init__(self, prog: Optional[str] = None) -> None: "generating one", ), ), + "reversed": ( + "-R", + "--reversed", + dict( + action="store_true", + help="Show the history in the reversed order", + ), + ), "version_path": ( "--version-path", dict( diff --git a/alembic/script/base.py b/alembic/script/base.py index 6b9692c6..bbb805af 100644 --- a/alembic/script/base.py +++ b/alembic/script/base.py @@ -247,7 +247,7 @@ def _catch_revision_errors( raise util.CommandError(err.args[0]) from err def walk_revisions( - self, base: str = "base", head: str = "heads" + self, base: str = "base", head: str = "heads", reversed: bool = False ) -> Iterator[Script]: """Iterate through all revisions. @@ -261,7 +261,11 @@ def walk_revisions( """ with self._catch_revision_errors(start=base, end=head): for rev in self.revision_map.iterate_revisions( - head, base, inclusive=True, assert_relative_length=False + head, + base, + inclusive=True, + assert_relative_length=False, + reversed=reversed, ): yield cast(Script, rev) diff --git a/alembic/script/revision.py b/alembic/script/revision.py index 5825da34..0e76f4c2 100644 --- a/alembic/script/revision.py +++ b/alembic/script/revision.py @@ -793,6 +793,7 @@ def iterate_revisions( inclusive: bool = False, assert_relative_length: bool = True, select_for_downgrade: bool = False, + reversed: bool = False, ) -> Iterator[Revision]: """Iterate through script revisions, starting at the given upper revision identifier and ending at the lower. @@ -819,7 +820,12 @@ def iterate_revisions( assert_relative_length=assert_relative_length, ) - for node in self._topological_sort(revisions, heads): + topological_sort = ( + self._topological_sort(revisions, heads)[::-1] + if reversed + else self._topological_sort(revisions, heads) + ) + for node in topological_sort: yield not_none(self.get_revision(node)) def _get_descendant_nodes(