Skip to content

Commit

Permalink
Merge pull request #204 from ral-facilities/bugfix/improve-db-sessions
Browse files Browse the repository at this point in the history
Bugfix/improve db sessions
  • Loading branch information
louise-davies authored Feb 18, 2021
2 parents 339174f + 9e8bed2 commit 856c31e
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 73 deletions.
16 changes: 11 additions & 5 deletions datagateway_api/common/database/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from functools import wraps
import logging

from flask_sqlalchemy import SQLAlchemy
from sqlalchemy.orm import aliased

from datagateway_api.common.database.filters import (
Expand All @@ -17,7 +18,6 @@
INVESTIGATIONINSTRUMENT,
SESSION,
)
from datagateway_api.common.database.session_manager import session_manager
from datagateway_api.common.exceptions import (
AuthenticationError,
BadRequestError,
Expand All @@ -28,6 +28,8 @@

log = logging.getLogger()

db = SQLAlchemy()


def requires_session_id(method):
"""
Expand All @@ -41,12 +43,11 @@ def requires_session_id(method):
@wraps(method)
def wrapper_requires_session(*args, **kwargs):
log.info(" Authenticating consumer")
session = session_manager.get_icat_db_session()
session = db.session
query = session.query(SESSION).filter(SESSION.ID == args[1]).first()
if query is not None:
log.info(" Closing DB session")
session.close()
session.close()
log.info(" Consumer authenticated")
return method(*args, **kwargs)
else:
Expand All @@ -66,7 +67,7 @@ class Query(ABC):

@abstractmethod
def __init__(self, table):
self.session = session_manager.get_icat_db_session()
self.session = db.session
self.table = table
self.base_query = self.session.query(table)

Expand All @@ -86,7 +87,12 @@ def commit_changes(self):
Commits all changes to the database and closes the session
"""
log.info(" Committing changes to %s", self.table)
self.session.commit()
try:
self.session.commit()
except Exception as e:
log.error("Error whilst committing changes to %s, rolling back", self.table)
self.session.rollback()
raise e


class CountQuery(Query):
Expand Down
26 changes: 0 additions & 26 deletions datagateway_api/common/database/session_manager.py

This file was deleted.

13 changes: 13 additions & 0 deletions datagateway_api/src/api_start_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

from datagateway_api.common.backends import create_backend
from datagateway_api.common.config import config
from datagateway_api.common.constants import Constants
from datagateway_api.common.database.helpers import db
from datagateway_api.src.resources.entities.entity_endpoint import (
get_count_endpoint,
get_endpoint,
Expand Down Expand Up @@ -58,6 +60,17 @@ def create_app_infrastructure(flask_app):
flask_app.url_map.strict_slashes = False
api = CustomErrorHandledApi(flask_app)

try:
backend_type = flask_app.config["TEST_BACKEND"]
config.set_backend_type(backend_type)
except KeyError:
backend_type = config.get_backend_type()

if backend_type == "db":
flask_app.config["SQLALCHEMY_DATABASE_URI"] = Constants.DATABASE_URL
flask_app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
db.init_app(flask_app)

initialise_spec(spec)

return (api, spec)
Expand Down
98 changes: 58 additions & 40 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ flask-swagger-ui = "3.25.0"
PyYAML = "5.3.1"
python-icat = "0.17.0"
suds-community = "^0.8.4"
Flask-SQLAlchemy = "^2.4.4"

[tool.poetry.dev-dependencies]
pip-tools = "5.3.1"
Expand Down
Loading

0 comments on commit 856c31e

Please sign in to comment.