From 30e77ddc3e6617a3fbe09a9f9e886f5a4ef010cb Mon Sep 17 00:00:00 2001 From: Arjuna Del Toso Date: Wed, 26 Jul 2023 10:36:50 -0700 Subject: [PATCH 1/3] Avoid unnecessary saves. Avoid savind a primary object if none of its fields have been updated. Avoid updating a primary object field if the value is identical to alias. --- django_super_deduper/merge.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/django_super_deduper/merge.py b/django_super_deduper/merge.py index 185ab24..b364af6 100644 --- a/django_super_deduper/merge.py +++ b/django_super_deduper/merge.py @@ -147,20 +147,25 @@ def merge(self, alias_object: Model): elif related_field.many_to_many: self._handle_m2m_related_field(related_field, alias_object) + primary_field_has_changed = False if self.merge_field_values: # This step can lead to validation errors if `field` has a `unique or` `unique_together` constraint. for field in model_meta.editable_fields: primary_value = getattr(primary_object, field.name) alias_value = getattr(alias_object, field.name) - + if primary_value == alias_value: + continue + logger.debug(f'Primary {field.name} has value: {primary_value}, ' f'Alias {field.name} has value: {alias_value}') if primary_value in field.empty_values and alias_value not in field.empty_values: logger.debug(f'Setting primary {field.name} to alias value: {alias_value}') setattr(primary_object, field.name, alias_value) + primary_field_has_changed = True if not self.keep_old: logger.debug(f'Deleting alias object {self.model_meta.model_name}[pk={alias_object.pk}]') alias_object.delete() - primary_object.save() + if primary_field_has_changed: + primary_object.save() From 0413c4ea500a44999d58001ab58a94fbc8ae533a Mon Sep 17 00:00:00 2001 From: Arjuna Del Toso Date: Wed, 26 Jul 2023 15:05:15 -0400 Subject: [PATCH 2/3] Remove redundant check (#2) We decided this is redundant, only advantage is to not have the logging but it's too little for keeping it. --- django_super_deduper/merge.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/django_super_deduper/merge.py b/django_super_deduper/merge.py index b364af6..adf65f6 100644 --- a/django_super_deduper/merge.py +++ b/django_super_deduper/merge.py @@ -153,8 +153,6 @@ def merge(self, alias_object: Model): for field in model_meta.editable_fields: primary_value = getattr(primary_object, field.name) alias_value = getattr(alias_object, field.name) - if primary_value == alias_value: - continue logger.debug(f'Primary {field.name} has value: {primary_value}, ' f'Alias {field.name} has value: {alias_value}') From 7cd965ad7ecbc8e277da9563f6379c7fc9c041bb Mon Sep 17 00:00:00 2001 From: Arjuna Del Toso Date: Wed, 26 Jul 2023 15:07:00 -0400 Subject: [PATCH 3/3] Update merge.py (#3) --- django_super_deduper/merge.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/django_super_deduper/merge.py b/django_super_deduper/merge.py index adf65f6..eddb5dd 100644 --- a/django_super_deduper/merge.py +++ b/django_super_deduper/merge.py @@ -153,7 +153,7 @@ def merge(self, alias_object: Model): for field in model_meta.editable_fields: primary_value = getattr(primary_object, field.name) alias_value = getattr(alias_object, field.name) - + logger.debug(f'Primary {field.name} has value: {primary_value}, ' f'Alias {field.name} has value: {alias_value}') if primary_value in field.empty_values and alias_value not in field.empty_values: