aboutsummaryrefslogtreecommitdiff
path: root/fietsboek/updater/cli.py
blob: 38297d79f35364ffd6ec0be7e6689872ad1fd8f0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
"""Updater for Fietsboek.

While this script does not download and install the latest Fietsboek version
itself (as this step depends on where you got Fietsboek from), it takes care of
managing migrations between Fietsboek versions. In particular, the updater
takes care of running the database migrations, migrating the data directory and
migrating the configuration.
"""
import click

from pyramid.paster import setup_logging

from . import Updater


def user_confirm():
    click.secho("Warning:", fg="yellow")
    click.echo(
        "Updating *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.")
    click.confirm("Proceed?", abort=True)


@click.group(help=__doc__)
@click.option(
    "-c", "--config",
    type=click.Path(exists=True, dir_okay=False),
    required=True,
    help="Path to the Fietsboek configuration file",
)
@click.pass_context
def cli(ctx, config):
    ctx.ensure_object(dict)
    setup_logging(config)
    ctx.obj["INIFILE"] = config


@cli.command("update")
@click.option(
    "-f", "--force",
    is_flag=True,
    help="Skip the safety question and just run the update",
)
@click.argument("VERSION", required=False)
@click.pass_context
def update(ctx, version, force):
    """Run the update process.

    Make sure to consult the documentation and ensure that you have a backup
    before starting the update, to prevent any data loss!

    VERSION specifies the version you want to update to. Leave empty to choose
    the latest version.
    """
    updater = Updater(ctx.obj["INIFILE"])
    updater.load()
    if version and not updater.exists(version):
        click.secho("Revision not found", fg="red")
        return
    version_str = ", ".join(updater.current_versions())
    click.echo(f"Current version: {version_str}")
    if not version:
        heads = updater.heads()
        if len(heads) != 1:
            click.secho("Ambiguous heads, please specify the version to update to", fg="red")
            return
        version = heads[0]
    click.echo(f"Selected version: {version}")
    if not force:
        user_confirm()
    updater.upgrade(version)
    version_str = ", ".join(updater.current_versions())
    click.secho(f"Update succeeded, version: {version_str}", fg="green")


@cli.command("downgrade")
@click.option(
    "-f", "--force",
    is_flag=True,
    help="Skip the safety question and just run the downgrade",
)
@click.argument("VERSION")
@click.pass_context
def downgrade(ctx, version, force):
    """Run the downgrade process.

    Make sure to consult the documentation and ensure that you have a backup
    before starting the downgrade, to prevent any data loss!

    VERSION specifies the version you want to downgrade to.
    """
    updater = Updater(ctx.obj["INIFILE"])
    updater.load()
    if version and not updater.exists(version):
        click.secho("Revision not found", fg="red")
        return
    version_str = ", ".join(updater.current_versions())
    click.echo(f"Current version: {version_str}")
    click.echo(f"Downgrade to version {version}")
    if not force:
        user_confirm()
    updater.downgrade(version)
    version_str = ", ".join(updater.current_versions())
    click.secho(f"Downgrade succeeded, version: {version_str}", fg="green")


@cli.command("revision")
@click.argument("REVISION_ID", required=False)
@click.pass_context
def revision(ctx, revision_id):
    """Create a new revision.

    This automatically populates the revision dependencies and alembic
    versions, based on the current state.

    This command is useful for developers who work on Fietsboek.
    """
    updater = Updater(ctx.obj["INIFILE"])
    updater.load()
    current = updater.current_versions()
    heads = updater.heads()
    if not any(version in heads for version in current):
        click.secho("Warning:", fg="yellow")
        click.echo("Your current revision is not a head. This will create a branch!")
        click.echo(
            "If this is not what you intended, make sure to update to the latest "
            "version first before creating a new revision."
        )
        click.confirm("Proceed?", abort=True)
    filename = updater.new_revision(revision_id)
    click.echo(f"Revision saved to {filename}")


if __name__ == "__main__":
    cli()