Description
Hi guys,
I see the Document.save method takes a save_condition
dict. With a very minor change (basically a new subclass of Document and a convention) this could automatically enforce and verify that it is safe to save the document.
I put together an example, but it's not as a PR because I'm not sure the best way to fit it into your object hierarchy. Basically, Document does not allow subclassing so I'm going to attach the sample but you'll need to move it into the package if you like it.
I propose a new class derived from Document
, called OptimisticDocument
:
The idea is that in addition to
id = mongoengine.ObjectIdField()
It also has a field
_doc_ver = mongoengine.UUIDField(binary=False)
Then anytime you save an OptimisticDocument
, it uses that and updates it only if there are pending changes. Here's a possible implementation (not save_raw
is the current Document.save
)
def save(self, force_insert=False, validate=True, clean=True,
write_concern=None, cascade=None, cascade_kwargs=None,
_refs=None, save_condition=None, signal_kwargs=None, **kwargs):
if not hasattr(self, '_changed_fields') or not self._changed_fields:
return self._save_raw(force_insert, validate, clean,
write_concern, cascade, cascade_kwargs,
_refs, save_condition, signal_kwargs, **kwargs)
print(self._changed_fields)
op_save_condition = {'_doc_ver': str(self._doc_ver)}
self._doc_ver = str(uuid.uuid4())
print("Changed doc ver from {} to {}".format(op_save_condition.get('_doc_ver'), self._doc_ver))
if save_condition:
if not isinstance(save_condition, dict):
raise ValueError("save_condition must be empty or a dictionary")
save_condition.update(op_save_condition)
op_save_condition = save_condition
self._save_raw(force_insert, validate, clean,
write_concern, cascade, cascade_kwargs,
_refs, op_save_condition, signal_kwargs, **kwargs)
To see it in action, you can run these two attached files. But they are highly hacked due to the fact that I couldn't subclass Document
.
- Run the code straight through, and it works fine.
- Run the code, stop at the input, change the underlying
_doc_ver
directly in the db, continue the program, see the save is stopped.
optimistic_document_example.zip
Thanks,
Michael