Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.Sign up
Optimistic locking #658
I'm not sure what is the correct way to implement support optimistic locking. As usual, I want to hold a version column that I increment on updates, while also ensuring that I am updating the row with the current version.
I assume I could implement this with something like:
Layer.update(name=self.name, label=self.label, version=self.version + 1).where((id=self.id) & (version=self.version))
But here, I need to explicitly list all the fields/values in
It sounds like there's no builtin support for this. Would you implement it as I sketched above or do you have a more sophisticated approach?
@coleifer I finally have to implement this. If I made a PR for it, would consider merging it into peewee? It could work like:
Or it could be:
I don't have strong opinion on the API, I just wish it was included in peewee and would not break during version upgrades. This touches part of core functionalities (
It shouldn't need too many modifications to
Maybe something more sophisticated than
I am personally doing locking based on integer version field, but it could be timestamp, hash, or anything.
What do you think?
That's pretty cool, but honestly I think that is just enough beyond the
There is enough complexity there that I think I could get bogged down
Personally I would love to see this as part of peewee, as it's something I found useful when I was using Ruby's ActiveRecord. Until now I wasn't doing optimistic locking in my peewee based projects, just because I was lazy to implement it myself...
@tuukkamustonen if you end up implementing it in a separate lib, please post the link here.
I am not running it in production, nor do I have extensive test suite for it. It's just something I threw in for now, and I actually had it back in my head to ask for comments at some point.
The big drawback is that this code is pretty much bound to a specific peewee version, because it re-writes the whole
A model is defined by:
It only supports numeric locking field.
And the actual implementation (my only additions are the two blocks with