Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix delete in transaction #333

Merged
merged 1 commit into from
Feb 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions google/cloud/ndb/_datastore_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -647,7 +647,8 @@ def put(self, entity_pb):
self.mutations.append(mutation)

# If we have an incomplete key, add the incomplete key to a batch for a
# call to AllocateIds
# call to AllocateIds, since the call to actually store the entity
# won't happen until the end of the transaction.
if not _complete(entity_pb.key):
# If this is the first key in the batch, we also need to
# schedule our idle handler to get called
Expand All @@ -657,12 +658,28 @@ def put(self, entity_pb):
self.incomplete_mutations.append(mutation)
self.incomplete_futures.append(future)

# Complete keys get passed back None
# Can't wait for result, since batch won't be sent until transaction
# has ended. Complete keys get passed back None.
else:
future.set_result(None)

return future

def delete(self, key):
"""Add a key to batch to be deleted.

Args:
entity_pb (datastore.Key): The entity's key to be deleted.

Returns:
tasklets.Future: Result will be :data:`None`, always.
"""
# Can't wait for result, since batch won't be sent until transaction
# has ended.
future = super(_TransactionalCommitBatch, self).delete(key)
future.set_result(None)
return future

def idle_callback(self):
"""Call AllocateIds on any incomplete keys in the batch."""
if not self.incomplete_mutations:
Expand Down
5 changes: 5 additions & 0 deletions google/cloud/ndb/_transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,14 @@
# limitations under the License.

import functools
import logging

from google.cloud.ndb import exceptions
from google.cloud.ndb import _retry
from google.cloud.ndb import tasklets

log = logging.getLogger(__name__)


def in_transaction():
"""Determine if there is a currently active transaction.
Expand Down Expand Up @@ -102,9 +105,11 @@ def _transaction_async(context, callback, read_only=False):
from google.cloud.ndb import _datastore_api

# Start the transaction
log.debug("Start transaction")
transaction_id = yield _datastore_api.begin_transaction(
read_only, retries=0
)
log.debug("Transaction Id: {}".format(transaction_id))

on_commit_callbacks = []
tx_context = context.new(
Expand Down
28 changes: 28 additions & 0 deletions tests/system/test_crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -1167,3 +1167,31 @@ class SomeKind(ndb.Model):

keys = SomeKind.allocate_ids(N)
assert len(keys) == N


@pytest.mark.usefixtures("client_context")
def test_delete_multi_with_transactional(dispose_of):
"""Regression test for issue #271

https://github.com/googleapis/python-ndb/issues/271
"""
N = 10

class SomeKind(ndb.Model):
foo = ndb.IntegerProperty()

@ndb.transactional()
def delete_them(entities):
ndb.delete_multi([entity.key for entity in entities])

foos = list(range(N))
entities = [SomeKind(foo=foo) for foo in foos]
keys = ndb.put_multi(entities)
dispose_of(*(key._key for key in keys))

entities = ndb.get_multi(keys)
assert [entity.foo for entity in entities] == foos

assert delete_them(entities) is None
entities = ndb.get_multi(keys)
assert entities == [None] * N
11 changes: 4 additions & 7 deletions tests/unit/test__datastore_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -777,18 +777,15 @@ def __init__(self, delete=None):
def __eq__(self, other):
return self.delete == other.delete

eventloop = mock.Mock(spec=("add_idle", "run"))
with in_context.new(
eventloop=eventloop, transaction=b"tx123"
).use() as context:
with in_context.new(transaction=b"tx123").use() as context:
datastore_pb2.Mutation = Mutation

key1 = key_module.Key("SomeKind", 1)._key
key2 = key_module.Key("SomeKind", 2)._key
key3 = key_module.Key("SomeKind", 3)._key
_api.delete(key1, _options.Options())
_api.delete(key2, _options.Options())
_api.delete(key3, _options.Options())
assert _api.delete(key1, _options.Options()).result() is None
assert _api.delete(key2, _options.Options()).result() is None
assert _api.delete(key3, _options.Options()).result() is None

batch = context.commit_batches[b"tx123"]
assert batch.mutations == [
Expand Down