diff options
author | Daniel Schadt <kingdread@gmx.de> | 2022-09-29 20:53:27 +0200 |
---|---|---|
committer | Daniel Schadt <kingdread@gmx.de> | 2022-09-29 20:53:27 +0200 |
commit | 4a391609e8432782f3911464d303b2a62c7b6c2c (patch) | |
tree | 924194786d903301ff13d2ebc09ac719523d0ee7 | |
parent | a90d279ca27293efcb5dac244f4fdf3498d75529 (diff) | |
download | fietsboek-4a391609e8432782f3911464d303b2a62c7b6c2c.tar.gz fietsboek-4a391609e8432782f3911464d303b2a62c7b6c2c.tar.bz2 fietsboek-4a391609e8432782f3911464d303b2a62c7b6c2c.zip |
fietsupdtr: noop if the update is already applied
-rw-r--r-- | fietsboek/updater/__init__.py | 24 | ||||
-rw-r--r-- | 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") |