From 58f3175668f67bc9cfc9ba523c896a0220ca5a32 Mon Sep 17 00:00:00 2001 From: SoniEx2 Date: Fri, 3 May 2019 20:19:24 -0300 Subject: Trivial migrations implementation --- ganarchy.py | 49 ++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 40 insertions(+), 9 deletions(-) diff --git a/ganarchy.py b/ganarchy.py index a5d68bc..8279681 100755 --- a/ganarchy.py +++ b/ganarchy.py @@ -66,6 +66,10 @@ TEMPLATE = """ """ +MIGRATIONS = { + "test": ("-- apply", "-- revert", "does nothing") + } + try: data_home = os.environ['XDG_DATA_HOME'] except KeyError: @@ -170,19 +174,46 @@ def remove(url): conn.commit() conn.close() -@ganarchy.group() def migrations(): - """Modifies the DB to work with a newer/older version. + @ganarchy.group() + def migrations(): + """Modifies the DB to work with a newer/older version. + + WARNING: THIS COMMAND CAN BE EXTREMELY DESTRUCTIVE!""" + + @migrations.command() + @click.argument('migration') + def apply(migration): + """Applies the migration with the given name.""" + conn = sqlite3.connect(data_home + "/ganarchy.db") + c = conn.cursor() + click.echo(MIGRATIONS[migration][0]) + c.execute(MIGRATIONS[migration][0]) + conn.commit() + conn.close() - WARNING: THIS COMMAND CAN BE EXTREMELY DESTRUCTIVE!""" + @click.argument('migration') + @migrations.command() + def revert(migration): + """Reverts the migration with the given name.""" + conn = sqlite3.connect(data_home + "/ganarchy.db") + c = conn.cursor() + click.echo(MIGRATIONS[migration][1]) + c.execute(MIGRATIONS[migration][1]) + conn.commit() + conn.close() -@migrations.command() -def apply(): - """ NYI """ + @click.argument('migration', required=False) + @migrations.command() + def info(migration): + """Shows information about the migration with the given name.""" + if not migration: + # TODO could be improved + click.echo(MIGRATIONS.keys()) + else: + click.echo(MIGRATIONS[migration][2]) -@migrations.command() -def revert(): - """ NYI """ +migrations() @ganarchy.command() def cron_target(): -- cgit 1.4.1