On Operational Exceptions we create the table and try again

this could potentially result in a endless recursion when the
table isnt writeable and a lot other funny things happen
but normally and in 99,999999999% of our cases this
is totally fine
pull/4/head
Darksider3 5 years ago
parent a1563116f6
commit d517db3b2a

@ -55,6 +55,9 @@ class SQLitedb:
self.cursor.execute(qq) self.cursor.execute(qq)
self.last_result = self.cursor.fetchall() self.last_result = self.cursor.fetchall()
self.connection.commit() self.connection.commit()
except sqlite3.OperationalError:
self._createTable()
return self.query(qq)
except sqlite3.Error as e: except sqlite3.Error as e:
print("Couldn't execute query %s, exception: %s" % (qq, e), file=stderr) print("Couldn't execute query %s, exception: %s" % (qq, e), file=stderr)
self.last_result = [] self.last_result = []
@ -88,6 +91,9 @@ class SQLitedb:
except TypeError as e: except TypeError as e:
print("Types in given tuple doesnt match to execute query \"%s\": %s" % (qq, e), file=stderr) print("Types in given tuple doesnt match to execute query \"%s\": %s" % (qq, e), file=stderr)
self.last_result = [] self.last_result = []
except sqlite3.OperationalError as e:
self._createTable()
return self.safequery(qq, deliver)
return self.last_result return self.last_result
def removeApplicantFromDB(self, userid: int) -> bool: def removeApplicantFromDB(self, userid: int) -> bool:
@ -104,6 +110,10 @@ class SQLitedb:
except sqlite3.Error as e: except sqlite3.Error as e:
print(f"Could not delete user with id: {userid}, exception in DB: {e}") # @TODO LOGGING FFS print(f"Could not delete user with id: {userid}, exception in DB: {e}") # @TODO LOGGING FFS
return False return False
except sqlite3.OperationalError:
print("The database has probably not yet seen any users, so it didnt create your table yet. Come back"
"when a user tried to register")
return False
return True return True
def removeApplicantFromDBperUsername(self, username: str) -> bool: def removeApplicantFromDBperUsername(self, username: str) -> bool:
@ -120,8 +130,27 @@ class SQLitedb:
except sqlite3.Error as e: except sqlite3.Error as e:
print(f"Could not delete user {username}, exception in DB: {e}") # @TODO LOGGING print(f"Could not delete user {username}, exception in DB: {e}") # @TODO LOGGING
return False return False
except sqlite3.OperationalError:
print("The database has probably not yet seen any users, so it didnt create your table yet. Come back"
"when a user tried to register")
return False
return True return True
def _createTable(self):
try:
self.cursor.execute(
"CREATE TABLE IF NOT EXISTS applications("
"id INTEGER PRIMARY KEY AUTOINCREMENT,"
"username TEXT NOT NULL, email TEXT NOT NULL,"
"name TEXT NOT NULL, pubkey TEXT NOT NULL,"
"timestamp DATETIME DEFAULT CURRENT_TIMESTAMP CONSTRAINT "
"timestamp_valid CHECK( timestamp IS strftime('%Y-%m-%d %H:%M:%S', timestamp))"
",status INTEGER NOT NULL DEFAULT 0);")
self.connection.commit()
except sqlite3.Error as e:
print(f"The database probably doesn't exist yet, but read the message: {e}")
print("The database table didn't exist yet; created it successfully!")
if __name__ == "__main__": if __name__ == "__main__":
try: try:

Loading…
Cancel
Save