Skip to content

Commit

Permalink
Plugin should not us outdated DB
Browse files Browse the repository at this point in the history
  • Loading branch information
ma3a committed Aug 31, 2023
1 parent c3d4925 commit 65fed47
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .pylintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[MAIN]
disable=
C0114, # missing-module-docstring
C0115, # missing-class-docstring
C0116 # missing-function-docstring
6 changes: 6 additions & 0 deletions defaults/python/db/migration.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ def _current_migration_version(self):

def _migration(self, migration: Migration):
version = self._current_migration_version()
latest_version_in_migration = max(_migrations, key=lambda m: m.version).version

if (latest_version_in_migration < version):
raise Exception(
"Database have been updated with latest version. Please update plugin")

if migration.version > version:
with self.db.transactional() as con:
for stm in migration.statements:
Expand Down
4 changes: 2 additions & 2 deletions defaults/python/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ def parse_date(date_str: str) -> date:
return datetime.strptime(date_str, DATE_FORMAT).date()


def format_date(datetime: datetime) -> str:
return datetime.strftime(DATE_FORMAT)
def format_date(dt: datetime) -> str:
return dt.strftime(DATE_FORMAT)


def end_of_day(day_to_end: datetime) -> datetime:
Expand Down
16 changes: 16 additions & 0 deletions defaults/python/tests/db/migration_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,19 @@ def _get_table_meta(self, table: str):
return connection.execute(
f"PRAGMA table_xinfo({table})"
).fetchall()

def test_should_fail_migration_if_application_older_then_database(self):
with sqlite3.connect(self.database_file) as connection:
connection.execute(
"CREATE TABLE IF NOT EXISTS migration (id INT PRIMARY KEY)"
)
connection.execute(
"INSERT INTO MIGRATION (id) VALUES (999999)"
)
try:
self.get_migration().migrate()
except Exception as e:
self.assertEqual(
str(e),
"Database have been updated with latest version. Please update plugin"
)
3 changes: 2 additions & 1 deletion defaults/python/tests/time_tracking_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def test_should_split_interval_in_two_day_in_case_night_session(self):
}
])

def test_should_sum_totalTime_per_day(self):
def test_should_sum_total_time_per_day(self):
now = datetime(2022, 1, 1, 9, 0)
self.time_tracking.add_time(
now.timestamp(), (now + timedelta(hours=1)).timestamp(), "100", "Zelda BOTW")
Expand Down Expand Up @@ -166,6 +166,7 @@ def test_return_only_data_in_requested_interval_without_gaps(self):

result = self.playtime_statistics.daily_statistics_for_period(
date_02.date(), date_08.date())
# pylint: disable=C0103
self.maxDiff = None
self.assertEqual([dataclasses.asdict(r) for r in result.data], [
{
Expand Down

0 comments on commit 65fed47

Please sign in to comment.