-
Notifications
You must be signed in to change notification settings - Fork 451
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
95 additions
and
0 deletions.
There are no files selected for viewing
32 changes: 32 additions & 0 deletions
32
src/tribler/core/upgrade/knowledge_to_triblerdb/migration.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 Codacy Production / Codacy Static Code Analysissrc/tribler/core/upgrade/knowledge_to_triblerdb/migration.py#L24
|
||
# 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 |
56 changes: 56 additions & 0 deletions
56
src/tribler/core/upgrade/knowledge_to_triblerdb/tests/test_migration.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters