From d933029217d10f5a2849677590a8ae18d9c9db86 Mon Sep 17 00:00:00 2001 From: Lukas Holecek Date: Thu, 18 May 2023 12:17:22 +0200 Subject: [PATCH] Fix initializing session in database (#74) --- resultsdb/__init__.py | 16 ++++++++++++++-- resultsdb/config.py | 1 + 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/resultsdb/__init__.py b/resultsdb/__init__.py index aff2589..0b74260 100644 --- a/resultsdb/__init__.py +++ b/resultsdb/__init__.py @@ -106,8 +106,7 @@ def create_app(config_obj=None): db.init_app(app) - app.config["SESSION_SQLALCHEMY"] = db - app.server_session = Session(app) + init_session(app) register_handlers(app) @@ -187,6 +186,19 @@ def not_found(error): return jsonify({"message": "Not found"}), 404 +def init_session(app): + app.config["SESSION_SQLALCHEMY"] = db + app.server_session = Session(app) + if app.config["SESSION_TYPE"] == "sqlalchemy": + import sqlalchemy + + with app.app_context(): + inspect = sqlalchemy.inspect(db.engine) + table = app.config["SESSION_SQLALCHEMY_TABLE"] + if not inspect.has_table(table): + db.create_all() + + def enable_oidc(app): with open(app.config["OIDC_CLIENT_SECRETS"]) as client_secrets_file: client_secrets = json.load(client_secrets_file) diff --git a/resultsdb/config.py b/resultsdb/config.py index 910d842..b0419ec 100644 --- a/resultsdb/config.py +++ b/resultsdb/config.py @@ -90,6 +90,7 @@ class Config(object): PERMANENT_SESSION_LIFETIME = 300 SESSION_TYPE = "sqlalchemy" + SESSION_SQLALCHEMY_TABLE = "sessions" SESSION_PERMANENT = True SESSION_USE_SIGNER = True SESSION_COOKIE_SECURE = True