def upgrade_database(to, confirm): if to > 1: version, changes = current_version() print("Current version: {} {}".format(version, changes)) if version != to - 1: print("Cannot upgrade to version {}".format(to)) return with open("migrations/v{}.sql".format(to), "r") as f: sql = f.read() print("Will upgrade to v{} by executing following sql: {}".format(to, sql)) if not confirm: print("Append `confirm` to confirm") else: write_to_tmp_file("undo", get_sql_dump()) result = engine.execute(sql) print("Upgrade complete") print("Result:", result)
def current_version(): query = "select id, changes from __version order by id desc limit 1" result = engine.execute(query) row = result.first() return row[0], row[1]