From 3787cd88a3deb0f330ff337eb9c6cb363654442f Mon Sep 17 00:00:00 2001
From: florian <f.spreckelsen@inidscale.com>
Date: Tue, 1 Nov 2022 16:58:34 +0100
Subject: [PATCH] ENH: Implement force merge

---
 src/caosdb/apiutils.py | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)

diff --git a/src/caosdb/apiutils.py b/src/caosdb/apiutils.py
index 9d2f3c4c..74a627bf 100644
--- a/src/caosdb/apiutils.py
+++ b/src/caosdb/apiutils.py
@@ -361,7 +361,7 @@ def empty_diff(old_entity: Entity, new_entity: Entity, compare_referenced_record
     return True
 
 
-def merge_entities(entity_a: Entity, entity_b: Entity, merge_references_with_empty_diffs=True):
+def merge_entities(entity_a: Entity, entity_b: Entity, merge_references_with_empty_diffs=True, force=False):
     """
     Merge entity_b into entity_a such that they have the same parents and properties.
 
@@ -385,6 +385,10 @@ def merge_entities(entity_a: Entity, entity_b: Entity, merge_references_with_emp
        record(s) that may be different Python objects but have empty diffs. If
        set to `False` a merge conflict will be raised in this case
        instead. Default is True.
+    force : bool, optional
+       If True, in case `entity_a` and `entity_b` have the same properties, the
+       values of `entity_b` are sued in the merge. If `False`, a RuntimeError is
+       raised instead. Default is False.
 
     Returns
     -------
@@ -421,6 +425,9 @@ def merge_entities(entity_a: Entity, entity_b: Entity, merge_references_with_emp
                     if (diff_r1["properties"][key][attribute] is None):
                         setattr(entity_a.get_property(key), attribute,
                                 diff_r2["properties"][key][attribute])
+                    elif force:
+                        setattr(entity_a.get_property(key), attribute,
+                                diff_r2["properties"][key][attribute])
                     else:
                         raise RuntimeError(
                             f"Merge conflict:\nEntity a ({entity_a.id}, {entity_a.name}) "
@@ -448,6 +455,9 @@ def merge_entities(entity_a: Entity, entity_b: Entity, merge_references_with_emp
         if sa_a != sa_b:
             if sa_a is None:
                 setattr(entity_a, special_attribute, sa_b)
+            elif force:
+                # force overwrite
+                setattr(entity_a, special_attribute, sa_b)
             else:
                 raise RuntimeError("Merge conflict.")
     return entity_a
-- 
GitLab