fix to get soft-deleted objects on db model query

read_deleted option
'yes' indicates deleted records are visible,
'only' indicates that *only* deleted records are visible.

Change-Id: I9be0f8127fac6c2294891d209195d9d0711a907b
Closes-Bug: #2015094
This commit is contained in:
Maurice Escher 2023-04-03 17:39:35 +02:00
parent f3ec5e738c
commit 99b00c9825
No known key found for this signature in database
GPG Key ID: CC56DEC23EE46750
3 changed files with 32 additions and 1 deletions

View File

@ -289,8 +289,10 @@ def model_query(context, model, *args, **kwargs):
kwargs['project_id'] = context.project_id
if read_deleted in ('no', 'n', False):
kwargs['deleted'] = False
elif read_deleted in ('yes', 'y', True):
elif read_deleted == 'only':
kwargs['deleted'] = True
elif read_deleted in ('yes', 'y', True):
pass
return db_utils.model_query(
model=model,

View File

@ -339,6 +339,29 @@ class ShareDatabaseAPITestCase(test.TestCase):
super(ShareDatabaseAPITestCase, self).setUp()
self.ctxt = context.get_admin_context()
@ddt.data('yes', 'no', 'only')
def test_share_read_deleted(self, read_deleted):
share = db_utils.create_share()
test_ctxt = context.get_admin_context(read_deleted=read_deleted)
admin_ctxt = context.get_admin_context(read_deleted='yes')
if read_deleted in ('yes', 'no'):
self.assertIsNotNone(db_api.share_get(test_ctxt, share['id']))
elif read_deleted == 'only':
self.assertRaises(exception.NotFound, db_api.share_get,
test_ctxt, share['id'])
# we don't use the to be tested context here and
# we need to delete the share instance before we can delete the share
db_api.share_instance_delete(admin_ctxt, share['instance']['id'])
db_api.share_delete(admin_ctxt, share['id'])
if read_deleted in ('yes', 'only'):
self.assertIsNotNone(db_api.share_get(test_ctxt, share['id']))
elif read_deleted == 'no':
self.assertRaises(exception.NotFound, db_api.share_get,
test_ctxt, share['id'])
def test_share_filter_by_host_with_pools(self):
share_instances = [[
db_api.share_create(self.ctxt, {'host': value}).instance

View File

@ -0,0 +1,6 @@
---
fixes:
- |
Internal sqlalchemy model_query has been fixed to honor the options of the
`read_deleted` parameter. For more details, please refer to
`launchpad bug #2015094 <https://bugs.launchpad.net/manila/+bug/2015094>`_