From da64778b0d73ae9a3b41f92800f62538a7deced7 Mon Sep 17 00:00:00 2001 From: Alexandre Aubin Date: Sun, 4 Aug 2019 00:57:00 +0200 Subject: [PATCH] Migrate the old migration json to the new yaml format --- src/yunohost/tools.py | 37 ++++++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/src/yunohost/tools.py b/src/yunohost/tools.py index f44e98a8d..a15b5593e 100644 --- a/src/yunohost/tools.py +++ b/src/yunohost/tools.py @@ -53,7 +53,6 @@ from yunohost.log import is_unit_operation, OperationLogger # FIXME this is a duplicate from apps.py APPS_SETTING_PATH = '/etc/yunohost/apps/' MIGRATIONS_STATE_PATH = "/etc/yunohost/migrations.yaml" -OLD_MIGRATIONS_STATE_PATH = "/etc/yunohost/migrations_state.json" logger = getActionLogger('yunohost.tools') @@ -1160,12 +1159,48 @@ def tools_migrations_state(): """ Show current migration state """ + if os.path.exists("/etc/yunohost/migrations_state.json"): + _migrate_legacy_migration_json() + if not os.path.exists(MIGRATIONS_STATE_PATH): return {"migrations": {}} return read_yaml(MIGRATIONS_STATE_PATH) +def _migrate_legacy_migration_json(): + + from moulinette.utils.filesystem import read_json + + logger.debug("Migrating legacy migration state json to yaml...") + + # We fetch the old state containing the last run migration + old_state = read_json("/etc/yunohost/migrations_state.json")["last_run_migration"] + last_run_migration_id = str(old_state["number"]) + "_" + old_state["name"] + + # Extract the list of migration ids + try: + from . import data_migrations + except ImportError: + # Well ugh what are we supposed to do if we can't do this >.>... + pass + migrations_path = data_migrations.__path__[0] + migration_files = filter(lambda x: re.match("^\d+_[a-zA-Z0-9_]+\.py$", x), os.listdir(migrations_path)) + # (here we remove the .py extension and make sure the ids are sorted) + migration_ids = sorted([f.rsplit(".", 1)[0] for f in migration_files]) + + # So now build the new dict for every id up to the last run migration + migrations = {} + for migration_id in migration_ids: + migrations[migration_id] = "done" + if last_run_migration_id in migration_id: + break + + # Write the new file and rename the old one + write_to_yaml(MIGRATIONS_STATE_PATH, {"migrations": migrations}) + os.rename("/etc/yunohost/migrations_state.json", "/etc/yunohost/migrations_state.json.old") + + def _write_migration_state(migration_id, state): current_states = tools_migrations_state()