db_utils.py

About this file

This file contains utils functions for the database. The objects added to the database are instances of sqlalchemy.
The functions in the file pertain to adding an object to the database, updating an object, deleting an object, and getting an object.
However, upon adding an object, if the object is a Requests object, an elasticsearch doc is created and returned.

Few functions explanation:
'create_object(obj)'
It returns string representation of created object or None if creation failed.

'update_object(data, obj_type, obj_id, es_update=True)'
It returns whether the record updated successfully or not. The parameters used are a dictionary of attribute-value pairs, sqlalchemy model, id of record and an update of the elasticsearch index which is True.

'delete_object(obj)'
It returns whether database record is deleted successfully or not with parameter as object (instance of sqlalchemy model) to delete.

'bulk_delete(query)'
It deletes multiple database records via a bulk delete query. It returns the number of records deleted by passing Query object parameter.

Code Check Report


No problems to report
                        

Documentation drawn from source code


app.lib.db_utils
~~~~~~~~~~~~~~~~
synopsis: Handles the functions for database control


create_object(obj):

Add a database record and its elasticsearch counterpart.

If 'obj' is a Requests object, nothing will be added to
the es index since a UserRequests record is created after
its associated request and the es doc requires a
requester id. 'es_create' is called explicitly for a
Requests object in app.request.utils.

:param obj: object (instance of sqlalchemy model) to create

:return: string representation of created object
or None if creation failed

update_object(data, obj_type, obj_id, es_update=True):

Update a database record and its elasticsearch counterpart.

:param data: a dictionary of attribute-value pairs
:param obj_type: sqlalchemy model
:param obj_id: id of record
:param es_update: update the elasticsearch index

:return: was the record updated successfully?

delete_object(obj):

Delete a database record.

:param obj: object (instance of sqlalchemy model) to delete
:return: was the record deleted successfully?

bulk_delete(query):

Delete multiple database records via a bulk delete query.

http://docs.sqlalchemy.org/en/latest/orm/query.html#sqlalchemy.orm.query.Query.delete

:param query: Query object
:return: the number of records deleted

get_object(obj_type, obj_id):

Safely retrieve a database record by its id
and its sqlalchemy object type.

get_agency_choices():

Source code