Skip to content
Snippets Groups Projects

F force merge

Merged Florian Spreckelsen requested to merge f-force-merge into dev
3 files
+ 105
5
Compare changes
  • Side-by-side
  • Inline
Files
3
+ 11
1
@@ -361,7 +361,7 @@ def empty_diff(old_entity: Entity, new_entity: Entity, compare_referenced_record
@@ -361,7 +361,7 @@ def empty_diff(old_entity: Entity, new_entity: Entity, compare_referenced_record
return True
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.
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
@@ -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
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
set to `False` a merge conflict will be raised in this case
instead. Default is True.
instead. Default is True.
 
force : bool, optional
 
If True, in case `entity_a` and `entity_b` have the same properties, the
 
values of `entity_a` are replaced by those of `entity_b` in the merge.
 
If `False`, a RuntimeError is raised instead. Default is False.
Returns
Returns
-------
-------
@@ -421,6 +425,9 @@ def merge_entities(entity_a: Entity, entity_b: Entity, merge_references_with_emp
@@ -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):
if (diff_r1["properties"][key][attribute] is None):
setattr(entity_a.get_property(key), attribute,
setattr(entity_a.get_property(key), attribute,
diff_r2["properties"][key][attribute])
diff_r2["properties"][key][attribute])
 
elif force:
 
setattr(entity_a.get_property(key), attribute,
 
diff_r2["properties"][key][attribute])
else:
else:
raise RuntimeError(
raise RuntimeError(
f"Merge conflict:\nEntity a ({entity_a.id}, {entity_a.name}) "
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
@@ -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 != sa_b:
if sa_a is None:
if sa_a is None:
setattr(entity_a, special_attribute, sa_b)
setattr(entity_a, special_attribute, sa_b)
 
elif force:
 
# force overwrite
 
setattr(entity_a, special_attribute, sa_b)
else:
else:
raise RuntimeError("Merge conflict.")
raise RuntimeError("Merge conflict.")
return entity_a
return entity_a
Loading