Added documentation. Changed .transaction() to .iff(), removed
TransactionException and replaced with LWTException
This commit is contained in:
@@ -5,7 +5,7 @@
|
|||||||
from collections import namedtuple
|
from collections import namedtuple
|
||||||
from cassandra.cluster import Cluster, NoHostAvailable
|
from cassandra.cluster import Cluster, NoHostAvailable
|
||||||
from cassandra.query import SimpleStatement, Statement
|
from cassandra.query import SimpleStatement, Statement
|
||||||
from cqlengine.exceptions import TransactionException
|
from cqlengine.exceptions import LWTException
|
||||||
import six
|
import six
|
||||||
|
|
||||||
try:
|
try:
|
||||||
@@ -112,12 +112,6 @@ def execute(query, params=None, consistency_level=None):
|
|||||||
params = params or {}
|
params = params or {}
|
||||||
result = session.execute(query, params)
|
result = session.execute(query, params)
|
||||||
|
|
||||||
if result and result[0].get('[applied]', True) is False:
|
|
||||||
result[0].pop('[applied]')
|
|
||||||
expected = ', '.join('{0}={1}'.format(t.field, t.value) for t in statement.transactions)
|
|
||||||
actual = ', '.join('{0}={1}'.format(f, v) for f, v in result[0].items())
|
|
||||||
message = 'Transaction statement failed: Expected: {0} Actual: {1}'.format(expected, actual)
|
|
||||||
raise TransactionException(message)
|
|
||||||
return result
|
return result
|
||||||
|
|
||||||
def get_session():
|
def get_session():
|
||||||
|
|||||||
@@ -2,7 +2,6 @@
|
|||||||
class CQLEngineException(Exception): pass
|
class CQLEngineException(Exception): pass
|
||||||
class ModelException(CQLEngineException): pass
|
class ModelException(CQLEngineException): pass
|
||||||
class ValidationError(CQLEngineException): pass
|
class ValidationError(CQLEngineException): pass
|
||||||
class TransactionException(CQLEngineException): pass
|
|
||||||
|
|
||||||
class UndefinedKeyspaceException(CQLEngineException): pass
|
class UndefinedKeyspaceException(CQLEngineException): pass
|
||||||
class LWTException(CQLEngineException): pass
|
class LWTException(CQLEngineException): pass
|
||||||
|
|||||||
@@ -266,7 +266,7 @@ class BaseModel(object):
|
|||||||
objects = QuerySetDescriptor()
|
objects = QuerySetDescriptor()
|
||||||
ttl = TTLDescriptor()
|
ttl = TTLDescriptor()
|
||||||
consistency = ConsistencyDescriptor()
|
consistency = ConsistencyDescriptor()
|
||||||
transaction = TransactionDescriptor()
|
iff = TransactionDescriptor()
|
||||||
|
|
||||||
# custom timestamps, see USING TIMESTAMP X
|
# custom timestamps, see USING TIMESTAMP X
|
||||||
timestamp = TimestampDescriptor()
|
timestamp = TimestampDescriptor()
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ from cqlengine import BaseContainerColumn, Map, columns
|
|||||||
from cqlengine.columns import Counter, List, Set
|
from cqlengine.columns import Counter, List, Set
|
||||||
|
|
||||||
from cqlengine.connection import execute
|
from cqlengine.connection import execute
|
||||||
from cqlengine.exceptions import CQLEngineException, ValidationError, TransactionException, LWTException
|
from cqlengine.exceptions import CQLEngineException, ValidationError, LWTException
|
||||||
from cqlengine.functions import Token, BaseQueryFunction, QueryValue, UnicodeMixin
|
from cqlengine.functions import Token, BaseQueryFunction, QueryValue, UnicodeMixin
|
||||||
|
|
||||||
#CQL 3 reference:
|
#CQL 3 reference:
|
||||||
@@ -849,14 +849,8 @@ class DMLQuery(object):
|
|||||||
return self._batch.add_query(q)
|
return self._batch.add_query(q)
|
||||||
else:
|
else:
|
||||||
tmp = execute(q, consistency_level=self._consistency)
|
tmp = execute(q, consistency_level=self._consistency)
|
||||||
if self._if_not_exists:
|
if self._if_not_exists or self._transaction:
|
||||||
check_applied(tmp)
|
check_applied(tmp)
|
||||||
if self._transaction and tmp[0].get('[applied]', True) is False:
|
|
||||||
tmp[0].pop('[applied]')
|
|
||||||
expected = ', '.join('{0}={1}'.format(t.field, t.value) for t in q.transactions)
|
|
||||||
actual = ', '.join('{0}={1}'.format(f, v) for f, v in tmp[0].items())
|
|
||||||
message = 'Transaction statement failed: Expected: {0} Actual: {1}'.format(expected, actual)
|
|
||||||
raise TransactionException(message)
|
|
||||||
return tmp
|
return tmp
|
||||||
|
|
||||||
def batch(self, batch_obj):
|
def batch(self, batch_obj):
|
||||||
@@ -966,7 +960,7 @@ class DMLQuery(object):
|
|||||||
if self.instance._has_counter or self.instance._can_update():
|
if self.instance._has_counter or self.instance._can_update():
|
||||||
return self.update()
|
return self.update()
|
||||||
else:
|
else:
|
||||||
insert = InsertStatement(self.column_family_name, ttl=self._ttl, timestamp=self._timestamp, if_not_exists=self._if_not_exists, transactions=self._transaction)
|
insert = InsertStatement(self.column_family_name, ttl=self._ttl, timestamp=self._timestamp, if_not_exists=self._if_not_exists)
|
||||||
for name, col in self.instance._columns.items():
|
for name, col in self.instance._columns.items():
|
||||||
val = getattr(self.instance, name, None)
|
val = getattr(self.instance, name, None)
|
||||||
if col._val_is_null(val):
|
if col._val_is_null(val):
|
||||||
|
|||||||
@@ -3,8 +3,11 @@ import os
|
|||||||
import sys
|
import sys
|
||||||
import six
|
import six
|
||||||
from cqlengine.connection import get_session
|
from cqlengine.connection import get_session
|
||||||
|
from cqlengine import connection
|
||||||
|
|
||||||
CASSANDRA_VERSION = int(os.environ['CASSANDRA_VERSION'])
|
CASSANDRA_VERSION = 20 #int(os.environ['CASSANDRA_VERSION'])
|
||||||
|
|
||||||
|
connection.setup(['192.168.56.103'], 'cqlengine_test')
|
||||||
|
|
||||||
class BaseCassEngTestCase(TestCase):
|
class BaseCassEngTestCase(TestCase):
|
||||||
|
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ __author__ = 'Tim Martin'
|
|||||||
from cqlengine.management import sync_table, drop_table
|
from cqlengine.management import sync_table, drop_table
|
||||||
from cqlengine.tests.base import BaseCassEngTestCase
|
from cqlengine.tests.base import BaseCassEngTestCase
|
||||||
from cqlengine.models import Model
|
from cqlengine.models import Model
|
||||||
from cqlengine.exceptions import TransactionException
|
from cqlengine.exceptions import LWTException
|
||||||
from uuid import uuid4
|
from uuid import uuid4
|
||||||
from cqlengine import columns
|
from cqlengine import columns
|
||||||
import mock
|
import mock
|
||||||
@@ -41,7 +41,7 @@ class TestTransaction(BaseCassEngTestCase):
|
|||||||
t = TestTransactionModel.create(text='blah blah')
|
t = TestTransactionModel.create(text='blah blah')
|
||||||
t.text = 'new blah'
|
t.text = 'new blah'
|
||||||
t = t.iff(text='something wrong')
|
t = t.iff(text='something wrong')
|
||||||
self.assertRaises(TransactionException, t.save)
|
self.assertRaises(LWTException, t.save)
|
||||||
|
|
||||||
def test_blind_update(self):
|
def test_blind_update(self):
|
||||||
t = TestTransactionModel.create(text='blah blah')
|
t = TestTransactionModel.create(text='blah blah')
|
||||||
@@ -59,4 +59,4 @@ class TestTransaction(BaseCassEngTestCase):
|
|||||||
t.text = 'something else'
|
t.text = 'something else'
|
||||||
uid = t.id
|
uid = t.id
|
||||||
qs = TestTransactionModel.objects(id=uid).iff(text='Not dis!')
|
qs = TestTransactionModel.objects(id=uid).iff(text='Not dis!')
|
||||||
self.assertRaises(TransactionException, qs.update, text='this will never work')
|
self.assertRaises(LWTException, qs.update, text='this will never work')
|
||||||
@@ -207,6 +207,14 @@ Model Methods
|
|||||||
|
|
||||||
This method is supported on Cassandra 2.0 or later.
|
This method is supported on Cassandra 2.0 or later.
|
||||||
|
|
||||||
|
.. method:: iff(**values)
|
||||||
|
|
||||||
|
Checks to ensure that the values specified are correct on the Cassandra cluster.
|
||||||
|
Simply specify the column(s) and the expected value(s). As with if_not_exists,
|
||||||
|
this incurs a performance cost.
|
||||||
|
|
||||||
|
If the insertion isn't applied, a LWTException is raised
|
||||||
|
|
||||||
.. method:: update(**values)
|
.. method:: update(**values)
|
||||||
|
|
||||||
Performs an update on the model instance. You can pass in values to set on the model
|
Performs an update on the model instance. You can pass in values to set on the model
|
||||||
|
|||||||
Reference in New Issue
Block a user