Skip to content

Commit

Permalink
Fixed database version can_upgrade detection
Browse files Browse the repository at this point in the history
  • Loading branch information
qstokkink committed Nov 11, 2024
1 parent d963b26 commit 473dccf
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/tribler/core/versioning/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,9 @@ def can_upgrade(self) -> str | bool:
if FROM not in self.get_versions():
return False # We can't upgrade from this version.

return FROM if (self.get_current_version() in [None, TO]) else False # Always allow upgrades to git (None).
# Always allow upgrades to git (None).
current_version = self.get_current_version()
return FROM if (current_version is None or Version(TO) <= Version(current_version)) else False

def perform_upgrade(self) -> None:
"""
Expand Down
17 changes: 17 additions & 0 deletions src/tribler/test_unit/core/versioning/test_manager.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from importlib.metadata import PackageNotFoundError
from unittest.mock import AsyncMock, Mock, patch

from packaging.version import Version

from ipv8.taskmanager import TaskManager
from ipv8.test.base import TestBase

Expand Down Expand Up @@ -173,6 +175,21 @@ def test_can_upgrade_to_current(self) -> None:
with patch("os.path.isfile", lambda _: False):
self.assertEqual(FROM, self.manager.can_upgrade())

def test_can_upgrade_to_current_soft(self) -> None:
"""
Check if we can upgrade to the currently supported soft version.
For example, the database directory may be (hard) version ``8.0`` and the actual (soft) version ``8.0.3``.
"""
self.manager.get_versions = Mock(return_value=[FROM])
db_version = Version(TO)
self.manager.get_current_version = Mock(
return_value=f"{db_version.major}.{db_version.minor}.{db_version.micro + 1}"
)

with patch("os.path.isfile", lambda _: False):
self.assertEqual(FROM, self.manager.can_upgrade())

def test_can_upgrade_to_git(self) -> None:
"""
Check if we can upgrade to the git version.
Expand Down

0 comments on commit 473dccf

Please sign in to comment.