Skip to content

Commit

Permalink
Merge pull request #7685 from drew2a/fix/7601
Browse files Browse the repository at this point in the history
Handle an absent `db_version` for Upgrader.
  • Loading branch information
drew2a committed Nov 13, 2023
2 parents e235b76 + 070e9e2 commit 5074a49
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/tribler/core/upgrade/db8_to_db10.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,5 +245,8 @@ def get_db_version(db_path):
with contextlib.closing(sqlite3.connect(db_path)) as connection, connection:
cursor = connection.cursor()
cursor.execute('SELECT value FROM MiscData WHERE name == "db_version"')
version = int(cursor.fetchone()[0])
query_results = cursor.fetchone()
if not query_results:
return 0
version = int(query_results[0])
return version
11 changes: 11 additions & 0 deletions src/tribler/core/upgrade/tests/test_db8_to_db10.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from unittest.mock import MagicMock, Mock, patch

from tribler.core.upgrade.db8_to_db10 import get_db_version
from tribler.core.utilities.path_util import Path


def test_get_db_version_no_db_version():
""" Test that get_db_version returns 0 if the database version is not found."""
cursor = Mock(return_value=Mock(fetchone=Mock(return_value=None)))
with patch('tribler.core.upgrade.db8_to_db10.sqlite3.connect', return_value=MagicMock(cursor=cursor)):
assert get_db_version(Path()) == 0

0 comments on commit 5074a49

Please sign in to comment.