Skip to content

Commit

Permalink
Add migration
Browse files Browse the repository at this point in the history
  • Loading branch information
drew2a committed Sep 27, 2023
1 parent 33d59bb commit 64aa0f1
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 0 deletions.
32 changes: 32 additions & 0 deletions src/tribler/core/upgrade/knowledge_to_triblerdb/migration.py
Original file line number Diff line number Diff line change
@@ -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:

Check notice on line 24 in src/tribler/core/upgrade/knowledge_to_triblerdb/migration.py

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/tribler/core/upgrade/knowledge_to_triblerdb/migration.py#L24

Unnecessary "else" after "return", remove the "else" and de-indent the code inside it
# 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
Original file line number Diff line number Diff line change
@@ -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()
7 changes: 7 additions & 0 deletions src/tribler/core/upgrade/upgrade.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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()

0 comments on commit 64aa0f1

Please sign in to comment.