Giter VIP home page Giter VIP logo

django-optimistic-lock's Introduction

django-optimistic-lock

image

Implements an offline optimistic lock1 for Django models. Requires Django 1.6, because model saving was recently refactored to allow access to the update query2.

Usage

Add a VersionField and inherit from VersionedMixin.

from ool import VersionField, VersionedMixin

class MyModel(VersionedMixin, models.Model):
    version = VersionField()

Whenever MyModel is saved, the version will be checked to ensure the instance has not changed since it was last fetched. If there is a conflict, a ConcurrentUpdate exception will be raised.

Implementation

A VersionField is just an integer that increments itself every time its model is saved. VersionedMixin overrides _do_update (which is called by save to actually do the update) to add an extra condition to the update query -- that the version in the database is the same as the model's version. If they match, there have been no concurrent modifications. If they don't match, the UPDATE statement will not update any rows, and we know that someone else saved first.

This produces SQL that looks something like:

UPDATE mymodel SET version = version + 1, ... WHERE id = %s AND version = %s

When no rows were updated, we know someone else won and we need to raise a ConcurrentUpdate.

Comparison to django-concurrency

django-concurrency before version 0.7 used SELECT FOR UPDATE to implement the version checking. I wanted to avoid database-level locking, so django-optimistic-lock adds a version filter to the update statement, as described by Martin Fowler3.

Additionally, ool takes a more minimalistic approach than django-concurrency by only doing one thing -- optimistic locking --without any monkey-patching, middleware, settings variables, admin classes, or form fields. django-concurrency would probably make more sense if you're looking for something that will attempt to accommodate every situation out of the box. Use ool if you just want a straightforward model implementation and need to handle the UI and surrounding architecture yourself.

Running the tests

make test

  1. http://martinfowler.com/eaaCatalog/optimisticOfflineLock.html

  2. https://code.djangoproject.com/ticket/16649

  3. http://martinfowler.com/eaaCatalog/optimisticOfflineLock.html

django-optimistic-lock's People

Contributors

andybak avatar gavinwahl avatar knaperek avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.