Skip to content

Commit

Permalink
support onconflict=skip option for entity insert (#163)
Browse files Browse the repository at this point in the history
  • Loading branch information
robes committed Oct 17, 2022
1 parent 596df41 commit e30c4ea
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
6 changes: 5 additions & 1 deletion deriva/core/datapath.py
Original file line number Diff line number Diff line change
Expand Up @@ -797,13 +797,14 @@ def groupby(self, *keys):
"""
return _AttributeGroup(self, self._query, keys)

def insert(self, entities, defaults=set(), nondefaults=set(), add_system_defaults=True):
def insert(self, entities, defaults=set(), nondefaults=set(), add_system_defaults=True, on_conflict_skip=False):
"""Inserts entities into the table.
:param entities: an iterable collection of entities (i.e., rows) to be inserted into the table.
:param defaults: optional, set of column names to be assigned the default expression value.
:param nondefaults: optional, set of columns names to override implicit system defaults
:param add_system_defaults: flag to add system columns to the set of default columns.
:param on_conflict_skip: flag to skip entities that violate uniqueness constraints.
:return a collection of newly created entities.
"""
# empty entities will be accepted but results are therefore an empty entity set
Expand All @@ -812,6 +813,9 @@ def insert(self, entities, defaults=set(), nondefaults=set(), add_system_default

options = []

if on_conflict_skip:
options.append('onconflict=skip')

if defaults or add_system_defaults:
defaults_enc = {urlquote(cname) for cname in defaults}
if add_system_defaults:
Expand Down
16 changes: 16 additions & 0 deletions tests/deriva/core/test_datapath.py
Original file line number Diff line number Diff line change
Expand Up @@ -703,6 +703,22 @@ def test_insert(self):
results = self.experiment_copy.insert(_generate_experiment_entities(self.types, 10))
self.assertEqual(len(results), 10)

def test_insert_on_conflict_raise(self):
entities = _generate_experiment_entities(self.types, 2)
first = entities[0:1]
results = self.experiment_copy.insert(first)
self.assertEqual(len(results), 1)
with self.assertRaises(DataPathException):
self.experiment_copy.insert(entities)

def test_insert_on_conflict_skip(self):
entities = _generate_experiment_entities(self.types, 2)
first = entities[0:1]
results = self.experiment_copy.insert(first)
self.assertEqual(len(results), 1)
results = self.experiment_copy.insert(entities, on_conflict_skip=True)
self.assertEqual(len(results), 1)

def test_update(self):
inserted = self.experiment_copy.insert(_generate_experiment_entities(self.types, 10))
self.assertEqual(len(inserted), 10)
Expand Down

0 comments on commit e30c4ea

Please sign in to comment.