Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
C
caosdb-pylib
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
caosdb
Software
caosdb-pylib
Commits
9410d638
Commit
9410d638
authored
2 years ago
by
Daniel Hornung
Browse files
Options
Downloads
Plain Diff
Merge branch 'f-merge-conflict-error' into 'dev'
F merge conflict error See merge request
!81
parents
31bea860
d575fc54
Branches
Branches containing commit
Tags
Tags containing commit
2 merge requests
!93
Release 0.11.0
,
!81
F merge conflict error
Pipeline
#30847
failed
2 years ago
Stage: code_style
Stage: linting
Stage: setup
Stage: test
Stage: deploy
Changes
3
Pipelines
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
CHANGELOG.md
+6
-0
6 additions, 0 deletions
CHANGELOG.md
src/caosdb/apiutils.py
+27
-13
27 additions, 13 deletions
src/caosdb/apiutils.py
unittests/test_apiutils.py
+13
-16
13 additions, 16 deletions
unittests/test_apiutils.py
with
46 additions
and
29 deletions
CHANGELOG.md
+
6
−
0
View file @
9410d638
...
...
@@ -9,8 +9,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added ###
*
`apiutils.EntityMergeConflictError`
class for unresesolvable merge conflicts
when merging two entities
### Changed ###
*
`apiutils.merge_entities`
now raises an
`EntityMergeConflictError`
in case of
unresolvable merge conflicts.
### Deprecated ###
### Removed ###
...
...
This diff is collapsed.
Click to expand it.
src/caosdb/apiutils.py
+
27
−
13
View file @
9410d638
...
...
@@ -27,12 +27,13 @@
Some simplified functions for generation of records etc.
"""
import
logging
import
sys
import
tempfile
import
warnings
from
collections.abc
import
Iterable
from
subprocess
import
call
from
typing
import
Optional
,
Any
,
Dict
,
List
from
caosdb.common.datatype
import
(
BOOLEAN
,
DATETIME
,
DOUBLE
,
FILE
,
INTEGER
,
...
...
@@ -40,8 +41,13 @@ from caosdb.common.datatype import (BOOLEAN, DATETIME, DOUBLE, FILE, INTEGER,
from
caosdb.common.models
import
(
Container
,
Entity
,
File
,
Property
,
Query
,
Record
,
RecordType
,
execute_query
,
get_config
,
SPECIAL_ATTRIBUTES
)
from
caosdb.exceptions
import
CaosDBException
import
logging
class
EntityMergeConflictError
(
CaosDBException
):
"""
An error that is raised in case of an unresolvable conflict when merging
two entities.
"""
def
new_record
(
record_type
,
name
=
None
,
description
=
None
,
...
...
@@ -365,14 +371,15 @@ 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
,
force
=
False
):
"""
Merge entity_b into entity_a such that they have the same parents and properties.
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.
datatype, unit, value, name and description will only be changed in entity_a if they
are None for entity_a and set for entity_b. If there is a corresponding value
for entity_a different from None a RuntimeError will be raised informing of an
unresolvable merge conflict.
datatype, unit, value, name and description will only be changed in entity_a
if they are None for entity_a and set for entity_b. If there is a
corresponding value for entity_a different from None, an
EntityMergeConflictError will be raised to inform about an unresolvable merge
conflict.
The merge operation is done in place.
...
...
@@ -392,13 +399,18 @@ def merge_entities(entity_a: Entity, entity_b: Entity, merge_references_with_emp
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
Runtime
Error is raised instead. Default is False.
If `False`, a
n EntityMergeConflict
Error is raised instead. Default is False.
Returns
-------
entity_a : Entity
The initial entity_a after the in-place merge
Raises
------
EntityMergeConflictError
In case of an unresolvable merge conflict.
"""
logging
.
warning
(
...
...
@@ -433,8 +445,8 @@ def merge_entities(entity_a: Entity, entity_b: Entity, merge_references_with_emp
setattr
(
entity_a
.
get_property
(
key
),
attribute
,
diff_r2
[
"
properties
"
][
key
][
attribute
])
else
:
raise
Ru
nti
me
Error
(
f
"
Merge conflict:
\n
Entity a (
{
entity_a
.
id
}
,
{
entity_a
.
name
}
)
"
raise
E
nti
tyMergeConflict
Error
(
f
"
Entity a (
{
entity_a
.
id
}
,
{
entity_a
.
name
}
)
"
f
"
has a Property
'
{
key
}
'
with
{
attribute
}
=
"
f
"
{
diff_r2
[
'
properties
'
][
key
][
attribute
]
}
\n
"
f
"
Entity b (
{
entity_b
.
id
}
,
{
entity_b
.
name
}
)
"
...
...
@@ -463,7 +475,9 @@ def merge_entities(entity_a: Entity, entity_b: Entity, merge_references_with_emp
# force overwrite
setattr
(
entity_a
,
special_attribute
,
sa_b
)
else
:
raise
RuntimeError
(
"
Merge conflict.
"
)
raise
EntityMergeConflictError
(
f
"
Conflict in special attribute
{
special_attribute
}
:
\n
"
f
"
A:
{
sa_a
}
\n
B:
{
sa_b
}
"
)
return
entity_a
...
...
This diff is collapsed.
Click to expand it.
unittests/test_apiutils.py
+
13
−
16
View file @
9410d638
...
...
@@ -30,7 +30,8 @@ import pytest
import
caosdb
as
db
import
caosdb.apiutils
from
caosdb.apiutils
import
(
apply_to_ids
,
compare_entities
,
create_id_query
,
empty_diff
,
resolve_reference
,
merge_entities
)
empty_diff
,
EntityMergeConflictError
,
resolve_reference
,
merge_entities
)
from
caosdb.common.models
import
SPECIAL_ATTRIBUTES
...
...
@@ -307,7 +308,7 @@ def test_merge_bug_conflict():
r3
=
db
.
Record
()
r3
.
add_property
(
name
=
"
C
"
,
value
=
4
,
datatype
=
"
INTEGER
"
)
with
pytest
.
raises
(
Ru
nti
meError
)
as
excinfo
:
with
pytest
.
raises
(
E
nti
tyMergeConflictError
)
:
merge_entities
(
r3
,
r2
)
...
...
@@ -402,15 +403,13 @@ def test_wrong_merge_conflict_reference():
rec_a
.
add_property
(
name
=
title_prop
.
name
,
value
=
"
Some dataset title
"
)
# this does not compare referenced records, so it will fail
with
pytest
.
raises
(
Ru
nti
meError
)
as
re
:
with
pytest
.
raises
(
E
nti
tyMergeConflictError
)
:
merge_entities
(
rec_a
,
rec_b
,
merge_references_with_empty_diffs
=
False
)
assert
"
Merge conflict
"
in
str
(
re
.
value
)
# ... as should this, of course
rec_b
.
get_property
(
license_rt
.
name
).
value
.
name
=
"
Another license
"
with
pytest
.
raises
(
Ru
nti
me
Error
)
as
re
:
with
pytest
.
raises
(
E
nti
tyMergeConflict
Error
)
as
re
:
merge_entities
(
rec_a
,
rec_b
)
assert
"
Merge conflict
"
in
str
(
re
.
value
)
def
test_empty_diff
():
...
...
@@ -484,9 +483,8 @@ def test_force_merge():
recA
=
db
.
Record
(
name
=
"
A
"
)
recB
=
db
.
Record
(
name
=
"
B
"
)
with
pytest
.
raises
(
Ru
nti
meError
)
as
re
:
with
pytest
.
raises
(
E
nti
tyMergeConflictError
)
:
merge_entities
(
recA
,
recB
)
assert
"
Merge conflict
"
in
str
(
re
.
value
)
merge_entities
(
recA
,
recB
,
force
=
True
)
assert
"
B
"
==
recA
.
name
...
...
@@ -499,9 +497,11 @@ def test_force_merge():
recB
=
db
.
Record
()
recB
.
description
=
"
something else
"
with
pytest
.
raises
(
Ru
nti
me
Error
)
as
r
e
:
with
pytest
.
raises
(
E
nti
tyMergeConflict
Error
)
as
emc
e
:
merge_entities
(
recA
,
recB
)
assert
"
Merge conflict
"
in
str
(
re
.
value
)
assert
str
(
emce
.
value
)
==
"""
Conflict in special attribute description:
A: something
B: something else
"""
merge_entities
(
recA
,
recB
,
force
=
True
)
assert
recA
.
description
==
"
something else
"
...
...
@@ -514,9 +514,8 @@ def test_force_merge():
recB
=
db
.
Record
()
recB
.
add_property
(
name
=
"
propA
"
,
value
=
"
something else
"
)
with
pytest
.
raises
(
Ru
nti
meError
)
as
re
:
with
pytest
.
raises
(
E
nti
tyMergeConflictError
)
:
merge_entities
(
recA
,
recB
)
assert
"
Merge conflict
"
in
str
(
re
.
value
)
merge_entities
(
recA
,
recB
,
force
=
True
)
assert
recA
.
get_property
(
"
propA
"
).
value
==
"
something else
"
...
...
@@ -540,9 +539,8 @@ def test_force_merge():
rtB
=
db
.
RecordType
()
rtB
.
add_property
(
name
=
"
propA
"
,
datatype
=
db
.
TEXT
)
with
pytest
.
raises
(
Ru
nti
meError
)
as
re
:
with
pytest
.
raises
(
E
nti
tyMergeConflictError
)
:
merge_entities
(
rtA
,
rtB
)
assert
"
Merge conflict
"
in
str
(
re
.
value
)
merge_entities
(
rtA
,
rtB
,
force
=
True
)
assert
rtA
.
get_property
(
"
propA
"
).
datatype
==
db
.
TEXT
...
...
@@ -555,9 +553,8 @@ def test_force_merge():
recB
=
db
.
Record
()
recB
.
add_property
(
name
=
"
propA
"
,
value
=
5
,
unit
=
"
cm
"
)
with
pytest
.
raises
(
Ru
nti
meError
)
as
re
:
with
pytest
.
raises
(
E
nti
tyMergeConflictError
)
:
merge_entities
(
recA
,
recB
)
assert
"
Merge conflict
"
in
str
(
re
.
value
)
merge_entities
(
recA
,
recB
,
force
=
True
)
assert
recA
.
get_property
(
"
propA
"
).
unit
==
"
cm
"
# unchanged
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment