Commit 6c227a2c authored by Christos Stavrakakis's avatar Christos Stavrakakis
Browse files

cyclades: Remove custom ForUpdateManager

Remove custom manager that implemented select_for_update(), since this
is already implemented in Django 1.4.
parent d7abb2ab
......@@ -27,73 +27,11 @@
# those of the authors and should not be interpreted as representing official
# policies, either expressed or implied, of GRNET S.A.
from django.db import connections
from django.db.models import Manager
from django.db.models.query import QuerySet
class ForUpdateManager(Manager):
"""Model manager implementing SELECT .. FOR UPDATE statement
This manager implements select_for_update() method in order to use
row-level locking in the database and guarantee exclusive access, since
this method is only implemented in Django>=1.4.
Non-blocking reads are not implemented, and each query including a row
that is locked by another transaction will block until the lock is
released. Also care must be taken in order to avoid deadlocks or retry
transactions that abort due to deadlocks.
Example:
networks = Network.objects.select_for_update().filter(public=True)
"""
def select_for_update(self, *args, **kwargs):
return ForUpdateQuerySet(self.model, using=self.db)
class ForUpdateQuerySet(QuerySet):
"""QuerySet implmenting SELECT .. FOR UPDATE statement
This QuerySet overrides filter and get methods in order to implement
select_for_update() statement, by appending 'FOR UPDATE' to the end
for the SQL query.
"""
def filter(self, *args, **kwargs):
query = super(ForUpdateQuerySet, self).filter(*args, **kwargs)
return for_update(query)
def get(self, *args, **kwargs):
query = self.filter(*args, **kwargs)
query = list(query)
num = len(query)
if num == 1:
return query[0]
if not num:
raise self.model.DoesNotExist("%s matching query does not exist. "
"Lookup parameters were %s" %
(self.model._meta.object_name,
kwargs))
raise self.model.MultipleObjectsReturned(
"get() returned more than one %s -- it returned %s! "
"Lookup parameters were %s" %
(self.model._meta.object_name, num, kwargs))
def for_update(query):
"""Rewrite query using SELECT .. FOR UPDATE."""
if 'sqlite' in connections[query.db].settings_dict['ENGINE'].lower():
# SQLite does not support FOR UPDATE
return query
sql, params = query.query.get_compiler(query.db).as_sql()
return query.model._default_manager.raw(sql.rstrip() + ' FOR UPDATE',
params)
class ProtectedDeleteManager(ForUpdateManager):
class ProtectedDeleteManager(Manager):
"""Manager for protecting Backend deletion.
Call Backend delete() method in order to prevent deletion
......
......@@ -41,7 +41,7 @@ from snf_django.lib.api import faults
from django.conf import settings as snf_settings
from aes_encrypt import encrypt_db_charfield, decrypt_db_charfield
from synnefo.db.managers import ForUpdateManager, ProtectedDeleteManager
from synnefo.db.managers import ProtectedDeleteManager
from synnefo.db import pools, fields
from synnefo.logic.rapi_pool import (get_rapi_client,
......@@ -360,8 +360,6 @@ class VirtualMachine(models.Model):
task = models.CharField(max_length=64, null=True)
task_job_id = models.BigIntegerField(null=True)
objects = ForUpdateManager()
def get_client(self):
if self.backend:
return self.backend.get_client()
......@@ -530,8 +528,6 @@ class Network(models.Model):
serial = models.ForeignKey(QuotaHolderSerial, related_name='network',
null=True)
objects = ForUpdateManager()
def __unicode__(self):
return "<Network: %s>" % str(self.id)
......@@ -738,8 +734,6 @@ class FloatingIP(models.Model):
serial = models.ForeignKey(QuotaHolderSerial,
related_name="floating_ips", null=True)
objects = ForUpdateManager()
def __unicode__(self):
return "<FIP: %s@%s>" % (self.ipv4, self.network.id)
......@@ -759,8 +753,6 @@ class PoolTable(models.Model):
base = models.CharField(null=True, max_length=32)
offset = models.IntegerField(null=True)
objects = ForUpdateManager()
class Meta:
abstract = True
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment