db: Migrate "share metadata" APIs to enginefacade

Signed-off-by: Stephen Finucane <stephenfin@redhat.com>
Change-Id: Ied3fb602aa690ee16c7e4a9744aa053e89dd9ae8
This commit is contained in:
Stephen Finucane 2023-04-11 13:32:27 +02:00
parent 60c64d4d95
commit e53a461c51

View File

@ -3938,19 +3938,34 @@ def share_snapshot_instance_export_locations_update(
################################# #################################
def _share_metadata_get_query(context, share_id):
return model_query(
context, models.ShareMetadata, read_deleted="no",
).filter_by(share_id=share_id).options(joinedload('share'))
@require_context @require_context
@require_share_exists @require_share_exists
@context_manager.reader
def share_metadata_get(context, share_id): def share_metadata_get(context, share_id):
return _share_metadata_get(context, share_id) return _share_metadata_get(context, share_id)
def _share_metadata_get(context, share_id):
rows = _share_metadata_get_query(context, share_id).all()
result = {}
for row in rows:
result[row['key']] = row['value']
return result
@require_context @require_context
@require_share_exists @require_share_exists
def share_metadata_get_item(context, share_id, key, session=None): @context_manager.reader
session = session or get_session() def share_metadata_get_item(context, share_id, key):
try: try:
row = _share_metadata_get_item(context, share_id, key, row = _share_metadata_get_item(context, share_id, key)
session=session)
except exception.MetadataItemNotFound: except exception.MetadataItemNotFound:
raise exception.MetadataItemNotFound() raise exception.MetadataItemNotFound()
@ -3962,57 +3977,39 @@ def share_metadata_get_item(context, share_id, key, session=None):
@require_context @require_context
@require_share_exists @require_share_exists
@context_manager.writer
def share_metadata_delete(context, share_id, key): def share_metadata_delete(context, share_id, key):
(_share_metadata_get_query(context, share_id). _share_metadata_get_query(
filter_by(key=key).soft_delete()) context, share_id,
).filter_by(key=key).soft_delete()
@require_context @require_context
@require_share_exists @require_share_exists
@context_manager.writer
def share_metadata_update(context, share_id, metadata, delete): def share_metadata_update(context, share_id, metadata, delete):
return _share_metadata_update(context, share_id, metadata, delete) return _share_metadata_update(context, share_id, metadata, delete)
@require_context @require_context
@require_share_exists @require_share_exists
def share_metadata_update_item(context, share_id, item, session=None): @context_manager.writer
def share_metadata_update_item(context, share_id, item):
return _share_metadata_update(context, share_id, item, delete=False) return _share_metadata_update(context, share_id, item, delete=False)
def _share_metadata_get_query(context, share_id, session=None):
return (model_query(context, models.ShareMetadata, session=session,
read_deleted="no").
filter_by(share_id=share_id).
options(joinedload('share')))
def _share_metadata_get(context, share_id, session=None):
rows = _share_metadata_get_query(context, share_id,
session=session).all()
result = {}
for row in rows:
result[row['key']] = row['value']
return result
@oslo_db_api.wrap_db_retry(max_retries=5, retry_on_deadlock=True) @oslo_db_api.wrap_db_retry(max_retries=5, retry_on_deadlock=True)
def _share_metadata_update(context, share_id, metadata, delete, session=None): def _share_metadata_update(context, share_id, metadata, delete):
if not session:
session = get_session()
with session.begin():
# Set existing metadata to deleted if delete argument is True # Set existing metadata to deleted if delete argument is True
delete = strutils.bool_from_string(delete) delete = strutils.bool_from_string(delete)
if delete: if delete:
original_metadata = _share_metadata_get(context, share_id, original_metadata = _share_metadata_get(context, share_id)
session=session)
for meta_key, meta_value in original_metadata.items(): for meta_key, meta_value in original_metadata.items():
if meta_key not in metadata: if meta_key not in metadata:
meta_ref = _share_metadata_get_item(context, share_id, meta_ref = _share_metadata_get_item(
meta_key, context, share_id, meta_key,
session=session) )
meta_ref.soft_delete(session=session) meta_ref.soft_delete(session=context.session)
meta_ref = None meta_ref = None
@ -4024,23 +4021,23 @@ def _share_metadata_update(context, share_id, metadata, delete, session=None):
item = {"value": meta_value} item = {"value": meta_value}
try: try:
meta_ref = _share_metadata_get_item(context, share_id, meta_ref = _share_metadata_get_item(
meta_key, context, share_id, meta_key,
session=session) )
except exception.MetadataItemNotFound: except exception.MetadataItemNotFound:
meta_ref = models.ShareMetadata() meta_ref = models.ShareMetadata()
item.update({"key": meta_key, "share_id": share_id}) item.update({"key": meta_key, "share_id": share_id})
meta_ref.update(item) meta_ref.update(item)
meta_ref.save(session=session) meta_ref.save(session=context.session)
return metadata return metadata
def _share_metadata_get_item(context, share_id, key, session=None): def _share_metadata_get_item(context, share_id, key):
result = (_share_metadata_get_query(context, share_id, session=session). result = _share_metadata_get_query(
filter_by(key=key). context, share_id,
first()) ).filter_by(key=key).first()
if not result: if not result:
raise exception.MetadataItemNotFound() raise exception.MetadataItemNotFound()