From 331fa177d85bba6ed9f5d9594ff45ebcb6d481b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustavo=20I=C3=B1iguez=20Goia?= Date: Mon, 31 Jul 2023 00:19:39 +0200 Subject: [PATCH] ui,db: added neede files for schema upgrades, other improvements - Added needed files to upgrade DB schema. - Use datetime.strptime instead of fromisoformat, to support python3.6 - More debug logs to better analyze problems. Related: #988 --- ui/MANIFEST.in | 1 + ui/opensnitch/database/__init__.py | 16 ++++++++++------ 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/ui/MANIFEST.in b/ui/MANIFEST.in index 932b95e7ef..4d02baf90a 100644 --- a/ui/MANIFEST.in +++ b/ui/MANIFEST.in @@ -1,3 +1,4 @@ recursive-include opensnitch/res * recursive-include opensnitch/i18n *.qm +recursive-include opensnitch/database/migrations *.sql include LICENSE diff --git a/ui/opensnitch/database/__init__.py b/ui/opensnitch/database/__init__.py index 86df19e0f3..3243b7ff2a 100644 --- a/ui/opensnitch/database/__init__.py +++ b/ui/opensnitch/database/__init__.py @@ -197,7 +197,8 @@ def get_schema_version(self): def set_schema_version(self, version): print("setting schema version to:", version) q = QSqlQuery("PRAGMA user_version = {0}".format(version), self.db) - q.exec_() + if q.exec_() == False: + print("Error updating updating schema version:", q.lastError().text()) def _upgrade_db_schema(self): migrations_path = os.path.dirname(os.path.realpath(__file__)) + "/migrations" @@ -210,8 +211,9 @@ def _upgrade_db_schema(self): try: print("applying schema upgrade:", schema_version) self._apply_db_upgrade("{0}/upgrade_{1}.sql".format(migrations_path, schema_version)) - except Exception: - print("Not applying upgrade_{0}.sql".format(schema_version)) + except Exception as e: + print("Not applying upgrade_{0}.sql:".format(schema_version), e) + return self.set_schema_version(schema_version) def _apply_db_upgrade(self, file): @@ -292,8 +294,8 @@ def purge_oldest(self, max_days_to_keep): if oldt == None or newt == None or oldt == 0 or newt == 0: return -1 - oldest = datetime.fromisoformat(oldt) - newest = datetime.fromisoformat(newt) + oldest = datetime.strptime(oldt, "%Y-%m-%d %H:%M:%S.%f") + newest = datetime.strptime(newt, "%Y-%m-%d %H:%M:%S.%f") diff = newest - oldest date_to_purge = datetime.now() - timedelta(days=max_days_to_keep) @@ -403,8 +405,10 @@ def _insert_batch(self, query_str, fields, values): q.addBindValue(fields) q.addBindValue(values) if not q.execBatch(): - print("_insert_batch() error", query_str) + print("_insert_batch() db error:", query_str) print(q.lastError().driverText()) + print("\t", fields) + print("\t", values) result=False except Exception as e: