From 4a391609e8432782f3911464d303b2a62c7b6c2c Mon Sep 17 00:00:00 2001 From: Daniel Schadt Date: Thu, 29 Sep 2022 20:53:27 +0200 Subject: fietsupdtr: noop if the update is already applied --- fietsboek/updater/__init__.py | 24 ++++++++++++++++++++++++ fietsboek/updater/cli.py | 22 ++++++++++++++++++---- 2 files changed, 42 insertions(+), 4 deletions(-) diff --git a/fietsboek/updater/__init__.py b/fietsboek/updater/__init__.py index 724defa..1c26715 100644 --- a/fietsboek/updater/__init__.py +++ b/fietsboek/updater/__init__.py @@ -258,6 +258,30 @@ class Updater: """ return [rev_id for (rev_id, deps) in self.backward_dependencies.items() if not deps] + def has_applied(self, revision_id, backward=False): + """Checks whether the given revision is applied. + + By default, this checks if a given update is applied, i.e. the current + version is greater-or-equal to the given revision ID. If ``backward`` + is ``True``, we instead check if the current version is lower-or-equal + to the given revision ID. + + Note that this function does not raise an error if the given revision + ID cannot be found and instead simply returns ``False``. Use + :meth:`exists` to check whether the revision actually exists. + + :param revision_id: The revision to check. + :type revision_id: str + :param backward: Whether to switch the comparison direction. + :type backward: bool + :return: ``True`` if the current version at least matches the asked + revision ID. + :rtype: bool + """ + if not backward: + return revision_id in self._transitive_versions() + return revision_id in self._reverse_versions() | set(self.current_versions()) + class UpdateScript: """Represents an update script.""" diff --git a/fietsboek/updater/cli.py b/fietsboek/updater/cli.py index 45655d4..c71ec17 100644 --- a/fietsboek/updater/cli.py +++ b/fietsboek/updater/cli.py @@ -27,10 +27,17 @@ config_option = click.option( ) -def user_confirm(): +def user_confirm(verb): + """Ask the user for confirmation before proceeding. + + Aborts the program if no confirmation is given. + + :param verb: The verb to use in the text. + :type verb: str + """ click.secho("Warning:", fg="yellow") click.echo( - "Updating *may* cause data loss. Make sure to have a backup of the " + f"{verb} *may* cause data loss. Make sure to have a backup of the " "database and the data directory!" ) click.echo("For more information, please consult the documentation.") @@ -79,8 +86,12 @@ def update(ctx, config, version, force): version = heads[0] click.echo(f"Selected version: {version}") + if updater.has_applied(version): + click.secho("Nothing to do", fg="green") + ctx.exit() + if not force: - user_confirm() + user_confirm("Updating") updater.upgrade(version) version_str = ", ".join(updater.current_versions()) click.secho(f"Update succeeded, version: {version_str}", fg="green") @@ -111,8 +122,11 @@ def downgrade(ctx, config, version, force): version_str = ", ".join(updater.current_versions()) click.echo(f"Current version: {version_str}") click.echo(f"Downgrade to version {version}") + if updater.has_applied(version, backward=True): + click.secho("Nothing to do", fg="green") + ctx.exit() if not force: - user_confirm() + user_confirm("Downgrading") updater.downgrade(version) version_str = ", ".join(updater.current_versions()) click.secho(f"Downgrade succeeded, version: {version_str}", fg="green") -- cgit v1.2.3