From b8e6e1b43b0bccec178dcc6708c5c8295abfb2e5 Mon Sep 17 00:00:00 2001 From: Simon Charette Date: Thu, 23 Jun 2016 11:52:14 -0400 Subject: Fixed #26500 -- Added SKIP LOCKED support to select_for_update(). Thanks Tim for the review. --- django/db/models/sql/compiler.py | 11 +++++++---- django/db/models/sql/query.py | 2 ++ 2 files changed, 9 insertions(+), 4 deletions(-) (limited to 'django/db/models/sql') diff --git a/django/db/models/sql/compiler.py b/django/db/models/sql/compiler.py index cedb140143..5eaac1fc74 100644 --- a/django/db/models/sql/compiler.py +++ b/django/db/models/sql/compiler.py @@ -445,13 +445,16 @@ class SQLCompiler(object): "select_for_update cannot be used outside of a transaction." ) - # If we've been asked for a NOWAIT query but the backend does - # not support it, raise a DatabaseError otherwise we could get - # an unexpected deadlock. nowait = self.query.select_for_update_nowait + skip_locked = self.query.select_for_update_skip_locked + # If we've been asked for a NOWAIT/SKIP LOCKED query but the + # backend does not support it, raise a DatabaseError otherwise + # we could get an unexpected deadlock. if nowait and not self.connection.features.has_select_for_update_nowait: raise DatabaseError('NOWAIT is not supported on this database backend.') - result.append(self.connection.ops.for_update_sql(nowait=nowait)) + elif skip_locked and not self.connection.features.has_select_for_update_skip_locked: + raise DatabaseError('SKIP LOCKED is not supported on this database backend.') + result.append(self.connection.ops.for_update_sql(nowait=nowait, skip_locked=skip_locked)) return ' '.join(result), tuple(params) finally: diff --git a/django/db/models/sql/query.py b/django/db/models/sql/query.py index d182242ce4..426087f8af 100644 --- a/django/db/models/sql/query.py +++ b/django/db/models/sql/query.py @@ -167,6 +167,7 @@ class Query(object): self.distinct_fields = [] self.select_for_update = False self.select_for_update_nowait = False + self.select_for_update_skip_locked = False self.select_related = False # Arbitrary limit for select_related to prevents infinite recursion. @@ -286,6 +287,7 @@ class Query(object): obj.distinct_fields = self.distinct_fields[:] obj.select_for_update = self.select_for_update obj.select_for_update_nowait = self.select_for_update_nowait + obj.select_for_update_skip_locked = self.select_for_update_skip_locked obj.select_related = self.select_related obj.values_select = self.values_select[:] obj._annotations = self._annotations.copy() if self._annotations is not None else None -- cgit v1.3