Skip to content

Commit

Permalink
fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisjsewell committed Oct 16, 2021
1 parent 264c61b commit 346a6b8
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 7 deletions.
2 changes: 1 addition & 1 deletion aiida/orm/implementation/django/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ def bulk_update(self, entity_type: EntityTypes, rows: List[dict]) -> None:

def delete_nodes_and_connections(self, pks_to_delete: Sequence[int]) -> None:
if not self.in_transaction:
raise AssertionError('Cannot delete nodes outside a transaction')
raise AssertionError('Cannot delete nodes and links outside a transaction')
# Delete all links pointing to or from a given node
dbm.DbLink.objects.filter(models.Q(input__in=pks_to_delete) | models.Q(output__in=pks_to_delete)).delete()
# now delete nodes
Expand Down
12 changes: 8 additions & 4 deletions aiida/orm/implementation/sqlalchemy/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
###########################################################################
"""SqlAlchemy implementation of `aiida.orm.implementation.backends.Backend`."""
# pylint: disable=missing-function-docstring
from contextlib import contextmanager
from contextlib import contextmanager, nullcontext
import functools
from typing import Iterator, List, Sequence

Expand Down Expand Up @@ -153,7 +153,9 @@ def bulk_insert(self, entity_type: EntityTypes, rows: List[dict], allow_defaults
# note for postgresql+psycopg2 we could also use `save_all` + `flush` with minimal performance degradation, see
# https://docs.sqlalchemy.org/en/14/changelog/migration_14.html#orm-batch-inserts-with-psycopg2-now-batch-statements-with-returning-in-most-cases
# by contrast, in sqlite, bulk_insert is faster: https://docs.sqlalchemy.org/en/14/faq/performance.html
self.get_session().bulk_insert_mappings(mapper, rows, render_nulls=True, return_defaults=True)
session = self.get_session()
with (nullcontext() if self.in_transaction else self.transaction()): # type: ignore[attr-defined]
session.bulk_insert_mappings(mapper, rows, render_nulls=True, return_defaults=True)
return [row['id'] for row in rows]

def bulk_update(self, entity_type: EntityTypes, rows: List[dict]) -> None: # pylint: disable=no-self-use
Expand All @@ -165,15 +167,17 @@ def bulk_update(self, entity_type: EntityTypes, rows: List[dict]) -> None: # py
raise IntegrityError(f"'id' field not given for {entity_type}: {set(row)}")
if not keys.issuperset(row):
raise IntegrityError(f'Incorrect fields given for {entity_type}: {set(row)} not subset of {keys}')
self.get_session().bulk_update_mappings(mapper, rows)
session = self.get_session()
with (nullcontext() if self.in_transaction else self.transaction()): # type: ignore[attr-defined]
session.bulk_update_mappings(mapper, rows)

def delete_nodes_and_connections(self, pks_to_delete: Sequence[int]) -> None: # pylint: disable=no-self-use
# pylint: disable=no-value-for-parameter
from aiida.backends.sqlalchemy.models.group import DbGroupNode
from aiida.backends.sqlalchemy.models.node import DbLink, DbNode

if not self.in_transaction:
raise AssertionError('Cannot delete nodes outside a transaction')
raise AssertionError('Cannot delete nodes and links outside a transaction')

session = self.get_session()
# Delete the membership of these nodes to groups.
Expand Down
4 changes: 2 additions & 2 deletions tests/orm/node/test_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -820,8 +820,8 @@ def test_delete_through_backend(self):
assert len(Log.objects.get_logs_for(data_two)) == 1
assert Log.objects.get_logs_for(data_two)[0].pk == log_two.pk

with backend.transaction() as transaction:
backend.delete_nodes_and_connections([data_two.pk], transaction)
with backend.transaction():
backend.delete_nodes_and_connections([data_two.pk])

assert len(Log.objects.get_logs_for(data_one)) == 1
assert Log.objects.get_logs_for(data_one)[0].pk == log_one.pk
Expand Down

0 comments on commit 346a6b8

Please sign in to comment.