From 6d5dbb915f1c86e27a1d3bc65b66a83b5f643b6d Mon Sep 17 00:00:00 2001 From: "V.G. Bulavintsev" Date: Fri, 14 Feb 2020 17:55:42 +0100 Subject: [PATCH] Fix version manager --- Tribler/Core/Upgrade/version_manager.py | 27 ++++++++++++++----- .../Test/Core/Upgrade/test_version_manager.py | 10 +++++++ 2 files changed, 31 insertions(+), 6 deletions(-) diff --git a/Tribler/Core/Upgrade/version_manager.py b/Tribler/Core/Upgrade/version_manager.py index db36f10baac..72fe3a9f4a9 100644 --- a/Tribler/Core/Upgrade/version_manager.py +++ b/Tribler/Core/Upgrade/version_manager.py @@ -68,15 +68,30 @@ def setup_state_directory_for_upgrade(self, version_id=None): return code_version = version_id or tribler_version.version_id + # To check if Tribler directory for the running version already exists, we must check for its *contents* + # because the directory itself is already created earlier + # FIXME: reposition upgrader so it will start *before* creating the state directory + code_version_directory_exists = (self.get_state_directory(code_version) / 'triblerd.conf').exists() + last_usage_version = self.version_history.get("last_version", None) - code_version_directory_exists = self.get_state_directory(code_version).exists() - upgrade_possible = last_usage_version and LooseVersion(last_usage_version) < LooseVersion(code_version) + last_usage_version_directory_exists = self.get_state_directory(last_usage_version).exists() + + upgrade_possible = False + # Pre-requisites for upgrading: + # the old directory should exist and the newer version should be higher then the old one + if last_usage_version_directory_exists and not code_version_directory_exists: + if last_usage_version is not None: + # Normal upgrade, e.g. 7.4.1->7.4.2 + upgrade_possible = LooseVersion(last_usage_version) < LooseVersion(code_version) + else: + # Legacy upgrade + upgrade_possible = True + + # If upgrade is possible, fork the state directory for the new code version + if upgrade_possible: - # If there is no state directory for the code version and - # the last used state directory versions is lower then the current version - # then fork the state directory for the new code version and update the history file. - if not code_version_directory_exists and upgrade_possible: self.fork_state_directory(code_version, self.get_state_directory(last_usage_version)) + # Update the history file if last_usage_version != code_version: self.update_version_history(code_version) diff --git a/Tribler/Test/Core/Upgrade/test_version_manager.py b/Tribler/Test/Core/Upgrade/test_version_manager.py index 7357c8a6cde..91187f92180 100644 --- a/Tribler/Test/Core/Upgrade/test_version_manager.py +++ b/Tribler/Test/Core/Upgrade/test_version_manager.py @@ -1,6 +1,8 @@ from __future__ import absolute_import +import filecmp import os +from pathlib import Path from configobj import ConfigObj @@ -87,6 +89,14 @@ def test_setup_state_directory_for_upgrade(self): version_state_dir = self.version_manager.get_state_directory(current_version) self.assertTrue(os.path.exists(version_state_dir)) + + post_upgrade_state_dir = self.config.get_state_dir() + # Make sure the directories before and after upgrade are different + self.assertFalse(version_state_dir == post_upgrade_state_dir) + # Make sure the contents in the before and after upgrade directories are the same + self.assertTrue(filecmp.cmp(Path(version_state_dir)/'ec_multichain.pem', + Path(post_upgrade_state_dir)/'ec_multichain.pem')) + version_state_sub_dirs = os.listdir(version_state_dir) backup_dirs = [STATEDIR_DB_DIR, STATEDIR_CHECKPOINT_DIR, STATEDIR_WALLET_DIR, STATEDIR_CHANNELS_DIR] for backup_dir in backup_dirs: