Skip to content
Snippets Groups Projects
Commit 1a466d5c authored by Michael Thies's avatar Michael Thies
Browse files

backends.couchdb: Improve discard(): Use HEAD request to fetch current revision

parent 88e6ac02
No related branches found
No related tags found
1 merge request!50Feature/backend couchdb
Pipeline #354289 passed
...@@ -342,7 +342,6 @@ class CouchDBObjectStore(model.AbstractObjectStore): ...@@ -342,7 +342,6 @@ class CouchDBObjectStore(model.AbstractObjectStore):
:raises CouchDBError: If error occur during the request to the CouchDB server (see `_do_request()` for details) :raises CouchDBError: If error occur during the request to the CouchDB server (see `_do_request()` for details)
""" """
logger.debug("Deleting object %s from CouchDB database ...", repr(x)) logger.debug("Deleting object %s from CouchDB database ...", repr(x))
# If x is not a CouchDBIdentifiable, retrieve x from the database to get the current couchdb_revision
rev = get_couchdb_revision("{}/{}/{}".format(self.url, rev = get_couchdb_revision("{}/{}/{}".format(self.url,
self.database_name, self.database_name,
self._transform_id(x.identification))) self._transform_id(x.identification)))
...@@ -352,18 +351,22 @@ class CouchDBObjectStore(model.AbstractObjectStore): ...@@ -352,18 +351,22 @@ class CouchDBObjectStore(model.AbstractObjectStore):
elif safe_delete: elif safe_delete:
raise CouchDBConflictError("No CouchDBRevision found for the object") raise CouchDBConflictError("No CouchDBRevision found for the object")
else: else:
# If not safe_delete, fetch the current document revision from the database using a HEAD request and the
# ETag response header
try: try:
logger.debug("fetching the current object revision for deletion ...") logger.debug("fetching the current object revision for deletion ...")
request = urllib.request.Request( request = urllib.request.Request(
"{}/{}/{}".format(self.url, self.database_name, self._transform_id(x.identification)), "{}/{}/{}".format(self.url, self.database_name, self._transform_id(x.identification)),
headers={'Accept': 'application/json'}) headers={'Accept': 'application/json'},
current_data = CouchDBBackend.do_request(request) method='HEAD')
except CouchDBServerError as e: opener = urllib.request.build_opener(urllib.request.HTTPBasicAuthHandler(_credentials_store))
response = opener.open(request)
rev = response.getheader('ETag')[1:-1]
except urllib.error.HTTPError as e:
if e.code == 404: if e.code == 404:
raise KeyError("No AAS object with id {} exists in CouchDB database".format(x.identification))\ raise KeyError("No AAS object with id {} exists in CouchDB database".format(x.identification))\
from e from e
raise raise
rev = current_data['_rev']
request = urllib.request.Request( request = urllib.request.Request(
"{}/{}/{}?rev={}".format(self.url, self.database_name, self._transform_id(x.identification), rev), "{}/{}/{}?rev={}".format(self.url, self.database_name, self._transform_id(x.identification), rev),
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment