Skip to content

Commit

Permalink
mathandpencil#17 - removed only from bulk_sync
Browse files Browse the repository at this point in the history
  • Loading branch information
voron3x committed Aug 17, 2020
1 parent 3f8bc66 commit b6969c2
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 12 deletions.
22 changes: 10 additions & 12 deletions bulk_sync/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
logger = logging.getLogger(__name__)


def _get_key(obj, key_fields):
"""Build key for object."""
return tuple(getattr(obj, k) for k in key_fields)


def bulk_sync(
new_models,
key_fields,
Expand Down Expand Up @@ -40,20 +45,16 @@ def bulk_sync(

using = router.db_for_write(db_class)
with transaction.atomic(using=using):
objs = db_class.objects.all()
objs = db_class.objects.select_for_update().all()
if filters:
objs = objs.filter(filters)
objs = objs.only("pk", *key_fields).select_for_update()

def get_key(obj):
return tuple(getattr(obj, k) for k in key_fields)

obj_dict = {get_key(obj): obj for obj in objs}
obj_dict = {_get_key(obj, key_fields): obj for obj in objs}

new_objs = []
existing_objs = []
for new_obj in new_models:
old_obj = obj_dict.pop(get_key(new_obj), None)
old_obj = obj_dict.pop(_get_key(new_obj, key_fields), None)
if old_obj is None:
# This is a new object, so create it.
new_objs.append(new_obj)
Expand Down Expand Up @@ -105,18 +106,15 @@ def bulk_compare(old_models, new_models, key_fields, ignore_fields=None):
"""

def get_key(obj):
return tuple(getattr(obj, k) for k in key_fields)

old_obj_dict = OrderedDict((get_key(obj), obj) for obj in old_models)
old_obj_dict = OrderedDict((_get_key(obj, key_fields), obj) for obj in old_models)

new_objs = []
change_details = {}
updated_objs = []
unchanged_objs = []

for new_obj in new_models:
old_obj = old_obj_dict.pop(get_key(new_obj), None)
old_obj = old_obj_dict.pop(_get_key(new_obj, key_fields), None)
if old_obj is None:
# This is a new object, so create it.
new_objs.append(new_obj)
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
Django>=2.2
dj-database-url>=0.5.0

0 comments on commit b6969c2

Please sign in to comment.