From 64aa0f1d4543f61cabbea54cc6a5b7b7c950b13e Mon Sep 17 00:00:00 2001 From: drew2a Date: Wed, 27 Sep 2023 12:38:26 +0200 Subject: [PATCH] Add migration --- .../knowledge_to_triblerdb/migration.py | 32 +++++++++++ .../tests/test_migration.py | 56 +++++++++++++++++++ src/tribler/core/upgrade/upgrade.py | 7 +++ 3 files changed, 95 insertions(+) create mode 100644 src/tribler/core/upgrade/knowledge_to_triblerdb/migration.py create mode 100644 src/tribler/core/upgrade/knowledge_to_triblerdb/tests/test_migration.py diff --git a/src/tribler/core/upgrade/knowledge_to_triblerdb/migration.py b/src/tribler/core/upgrade/knowledge_to_triblerdb/migration.py new file mode 100644 index 00000000000..b00703e0ea8 --- /dev/null +++ b/src/tribler/core/upgrade/knowledge_to_triblerdb/migration.py @@ -0,0 +1,32 @@ +import logging +import shutil + +from tribler.core.utilities.path_util import Path +from tribler.core.utilities.simpledefs import STATEDIR_DB_DIR + + +class MigrationKnowledgeToTriblerDB: + def __init__(self, state_dir: Path): + self.logger = logging.getLogger(self.__class__.__name__) + self.state_dir = state_dir + + self.knowledge_db_path = self.state_dir / STATEDIR_DB_DIR / 'knowledge.db' + self.tribler_db_path = self.state_dir / STATEDIR_DB_DIR / 'tribler.db' + + self.logger.info(f'Knowledge DB path: {self.knowledge_db_path}') + self.logger.info(f'Tribler DB path: {self.tribler_db_path}') + + def run(self) -> bool: + if not self.knowledge_db_path.exists(): + self.logger.info("Knowledge DB doesn't exist. Stop procedure.") + return False + + try: + # move self.knowledge_db_path to self.tribler_db_path + shutil.move(str(self.knowledge_db_path), str(self.tribler_db_path)) + except OSError as e: + self.logger.error(f"Failed to move the file: {e}") + return False + else: + self.logger.info("File moved successfully.") + return True diff --git a/src/tribler/core/upgrade/knowledge_to_triblerdb/tests/test_migration.py b/src/tribler/core/upgrade/knowledge_to_triblerdb/tests/test_migration.py new file mode 100644 index 00000000000..61a524026d7 --- /dev/null +++ b/src/tribler/core/upgrade/knowledge_to_triblerdb/tests/test_migration.py @@ -0,0 +1,56 @@ +from unittest.mock import Mock, patch + +import pytest + +from tribler.core.upgrade.knowledge_to_triblerdb.migration import MigrationKnowledgeToTriblerDB +from tribler.core.upgrade.tags_to_knowledge.previous_dbs.knowledge_db import KnowledgeDatabase +from tribler.core.utilities.path_util import Path +from tribler.core.utilities.simpledefs import STATEDIR_DB_DIR + + +# pylint: disable=redefined-outer-name +@pytest.fixture +def migration(tmp_path: Path): + db_dir = tmp_path / STATEDIR_DB_DIR + db_dir.mkdir() + migration = MigrationKnowledgeToTriblerDB(tmp_path) + return migration + + +def test_no_knowledge_db(migration: MigrationKnowledgeToTriblerDB): + # test that in the case of missed `knowledge.db`, migration.run() returns False + assert not migration.run() + assert not migration.knowledge_db_path.exists() + assert not migration.tribler_db_path.exists() + + +def test_move_file(migration: MigrationKnowledgeToTriblerDB): + # Test that the migration moves the `knowledge.db` to `tribler.db` + + # create DB file + KnowledgeDatabase(str(migration.knowledge_db_path)).shutdown() + + assert migration.knowledge_db_path.exists() + assert not migration.tribler_db_path.exists() + + # run migration + assert migration.run() + assert not migration.knowledge_db_path.exists() + assert migration.tribler_db_path.exists() + + +@patch('tribler.core.upgrade.knowledge_to_triblerdb.migration.shutil.move', Mock(side_effect=FileNotFoundError)) +def test_exception(migration: MigrationKnowledgeToTriblerDB): + # Test that the migration doesn't move the `knowledge.db` to `tribler.db` after unsuccessful migration procedure. + + # create DB file + KnowledgeDatabase(str(migration.knowledge_db_path)).shutdown() + + assert migration.knowledge_db_path.exists() + assert not migration.tribler_db_path.exists() + + # run migration + assert not migration.run() + + assert migration.knowledge_db_path.exists() + assert not migration.tribler_db_path.exists() diff --git a/src/tribler/core/upgrade/upgrade.py b/src/tribler/core/upgrade/upgrade.py index e871237fe6f..682f9157836 100644 --- a/src/tribler/core/upgrade/upgrade.py +++ b/src/tribler/core/upgrade/upgrade.py @@ -19,6 +19,7 @@ ) from tribler.core.upgrade.config_converter import convert_config_to_tribler76 from tribler.core.upgrade.db8_to_db10 import PonyToPonyMigration, get_db_version +from tribler.core.upgrade.knowledge_to_triblerdb.migration import MigrationKnowledgeToTriblerDB from tribler.core.upgrade.tags_to_knowledge.migration import MigrationTagsToKnowledge from tribler.core.upgrade.tags_to_knowledge.previous_dbs.tags_db import TagDatabase from tribler.core.utilities.configparser import CallbackConfigParser @@ -104,6 +105,7 @@ def run(self): self.upgrade_tags_to_knowledge() self.remove_old_logs() self.upgrade_pony_db_14to15() + self.upgrade_knowledge_to_tribler_db() def remove_old_logs(self) -> Tuple[List[Path], List[Path]]: self._logger.info(f'Remove old logs') @@ -417,3 +419,8 @@ def update_status(self, status_text): self._logger.info(status_text) if self._update_status_callback: self._update_status_callback(status_text) + + def upgrade_knowledge_to_tribler_db(self): + self._logger.info('Upgrade knowledge to tribler.db') + migration = MigrationKnowledgeToTriblerDB(self.state_dir) + migration.run()