cloudant / python-cloudant

A Python library for Cloudant and CouchDB
Apache License 2.0
163 stars 55 forks source link

Document.save on create/change only #429

Closed aogier closed 5 years ago

aogier commented 5 years ago

Checklist

Description

Trivial change that commit to remote db only when creating or actually changing local data.

Current behaviour:

doc = Document(self.db, 'http://example.com')
doc.save()
doc.save()
# doc _rev is now 2-

New behaviour:

doc = Document(self.db, 'http://example.com')
doc.save()
doc.save()
# doc _rev is still 1-

Approach

Implemented a simple boolean check that flip upon Document's dict update methods calls. Document will still be created even if empty, but save will only happen when an actual change is made, minimizing data transfers.

Schema & API Changes

Security and Privacy

Testing

Monitoring and Logging

aogier commented 5 years ago

Ok I think I'm ready for a code review. Tests are passing and all seems promising but I'd definitely like another couple of eyes :)

aogier commented 5 years ago

meh, I forgot setdefault...

aogier commented 5 years ago

added setdefault

aogier commented 5 years ago

I've reintroduced _doc_id logic on some dict methods I'll fix it asap

aogier commented 5 years ago

_document_id logic re-removed from code

smithsz commented 5 years ago

Thanks for all your recent work. It is really appreciated. We’ve discussed this change as a team and feel it’s adding too much complexity to the Document class. I’ve tried a different implementation; instead I compute/compare a hash of the document on each save (here). This change is less invasive but likely a more expensive solution. We’ve decided the library shouldn't offer this feature. If a user is calling .save() multiple times without updating the document then that’s their decision. Thanks again and have a good weekend.

aogier commented 5 years ago

Hi, it seems a good conclusion to me and as a couchdb noob I'll take this design choice as an implementation hint especially on where to put my ctx manager. Thank you for your time and keep up the good work :)

ciao