Skip to content

Commit

Permalink
#241: Implement /ping on DB backend
Browse files Browse the repository at this point in the history
  • Loading branch information
MRichards99 committed Aug 13, 2021
1 parent 1cf4972 commit 7384a88
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion datagateway_api/common/database/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@
import logging
import uuid

from sqlalchemy import inspect
from sqlalchemy.exc import SQLAlchemyError

from datagateway_api.common.backend import Backend
from datagateway_api.common.constants import Constants
from datagateway_api.common.database.helpers import (
create_rows_from_json,
db,
delete_row_by_id,
get_facility_cycles_for_instrument,
get_facility_cycles_for_instrument_count,
Expand All @@ -20,7 +25,7 @@
update_row_from_id,
)
from datagateway_api.common.database.models import SESSION
from datagateway_api.common.exceptions import AuthenticationError
from datagateway_api.common.exceptions import AuthenticationError, DatabaseError
from datagateway_api.common.helpers import get_entity_object_from_name, queries_records


Expand All @@ -32,6 +37,19 @@ class DatabaseBackend(Backend):
Class that contains functions to access and modify data in an ICAT database directly
"""

def ping(self, **kwargs):
log.info("Pinging DB connection to ensure API is alive and well")

try:
inspector = inspect(db.engine)
print(type(inspector))
tables = inspector.get_table_names()
log.debug("Tables on ping: %s", tables)
except SQLAlchemyError as e:
raise DatabaseError(e)

return Constants.PING_OK_RESPONSE

def login(self, credentials, **kwargs):
if credentials["username"] == "user" and credentials["password"] == "password":
session_id = str(uuid.uuid1())
Expand Down

0 comments on commit 7384a88

Please sign in to comment.