Skip to content

Commit

Permalink
ui,db: added neede files for schema upgrades, other improvements
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
gustavo-iniguez-goya committed Jul 30, 2023
1 parent ade3dc0 commit 331fa17
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
1 change: 1 addition & 0 deletions ui/MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
recursive-include opensnitch/res *
recursive-include opensnitch/i18n *.qm
recursive-include opensnitch/database/migrations *.sql
include LICENSE
16 changes: 10 additions & 6 deletions ui/opensnitch/database/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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):
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 331fa17

Please sign in to comment.