chore: Add test_read method to Database class for testing read access to the database and use it with the web UI

This commit is contained in:
Théophile Diot 2024-05-28 16:58:10 +01:00
parent 41e4834a06
commit 7f5d3cd937
No known key found for this signature in database
GPG key ID: 248FEA4BAE400D06
2 changed files with 11 additions and 0 deletions

View file

@ -215,6 +215,12 @@ class Database:
if self.sql_engine:
self.sql_engine.dispose()
def test_read(self):
"""Test the read access to the database"""
self.logger.debug("Testing read access to the database ...")
with self.__db_session() as session:
session.execute(text("SELECT 1"))
def test_write(self):
"""Test the write access to the database"""
self.logger.debug("Testing write access to the database ...")

View file

@ -475,6 +475,11 @@ def before_request():
except BaseException:
ui_data["READONLY_MODE"] = True
ui_data["LAST_DATABASE_RETRY"] = app.config["DB"].last_connection_retry.isoformat()
else:
try:
app.config["DB"].test_read()
except BaseException:
ui_data["LAST_DATABASE_RETRY"] = app.config["DB"].last_connection_retry.isoformat()
app.config["DB"].readonly = ui_data.get("READONLY_MODE", False)
with LOCK: