objects: added update_objects to OVO framework

This method allows to update matching resources with requested values
without doing full fetch/set/update cycle. This is also handy to "lock"
records in database with UPDATE WHERE.

Change-Id: I2347fedbecef823babe3d8038f5a74b21fc0a602
Partially-Implements: blueprint adopt-oslo-versioned-objects-for-db
This commit is contained in:
Ihar Hrachyshka
2017-03-31 17:17:47 +00:00
parent caf32b69cd
commit b309e12a5d
6 changed files with 169 additions and 17 deletions

View File

@@ -83,6 +83,23 @@ def delete_object(context, model, **kwargs):
context.session.delete(db_obj)
def update_objects(context, model, values, **kwargs):
'''Update matching objects, if any. Return number of updated objects.
This function does not raise exceptions if nothing matches.
:param model: SQL model
:param values: values to update in matching objects
:param kwargs: multiple filters defined by key=value pairs
:return: Number of entries updated
'''
with context.session.begin(subtransactions=True):
if not values:
return count(context, model, **kwargs)
q = _get_filter_query(context, model, **kwargs)
return q.update(values, synchronize_session=False)
def delete_objects(context, model, **kwargs):
'''Delete matching objects, if any. Return number of deleted objects.