Skip to content
Snippets Groups Projects
Commit 65de5439 authored by Timm Fitschen's avatar Timm Fitschen
Browse files

Merge branch 'f-check-datamodel' into 'dev'

f-check-datamodel -> dev

See merge request caosdb/caosdb-advanced-user-tools!52
parents fe47fac1 887d98a4
No related branches found
No related tags found
1 merge request!22Release 0.3
...@@ -19,6 +19,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ...@@ -19,6 +19,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Changed ### ### Changed ###
* `caosadvancedtools.serverside.helper.init_data_model` also checks the role
and data type of entities.
* The `caosadvancedtools.table_importer.date_converter` now actually returns * The `caosadvancedtools.table_importer.date_converter` now actually returns
`datetime.date` instance. A new `datetime.date` instance. A new
`caosadvancedtools.table_importer.datetime_converter` replaces the old `caosadvancedtools.table_importer.datetime_converter` replaces the old
......
...@@ -147,13 +147,13 @@ def print_error(text): ...@@ -147,13 +147,13 @@ def print_error(text):
class DataModelError(RuntimeError): class DataModelError(RuntimeError):
"""DataModelError indicates that the server-side script cannot work as """DataModelError indicates that the server-side script cannot work as
intended due to missing datat model entities or an otherwise incompatible intended due to missing data model entities or an otherwise incompatible
data model.""" data model."""
def __init__(self, rt): def __init__(self, rt, info=""):
super().__init__( super().__init__(
"This script expects certain RecordTypes and Properties to exist " "This script expects certain RecordTypes and Properties to exist "
"in the data model. There is a problem with {} .".format(rt)) "in the data model. There is a problem with {}. {}".format(rt, info))
def recordtype_is_child_of(rt, parent): def recordtype_is_child_of(rt, parent):
...@@ -183,7 +183,11 @@ def recordtype_is_child_of(rt, parent): ...@@ -183,7 +183,11 @@ def recordtype_is_child_of(rt, parent):
def init_data_model(entities): def init_data_model(entities):
"""Return True if all entities exist. """Return True iff all entities exist and their role and possibly their
data type is correct.
This implementation follows a fail-fast approach. The first entity with
problems will raise an exception.
Parameters Parameters
---------- ----------
...@@ -194,18 +198,29 @@ def init_data_model(entities): ...@@ -194,18 +198,29 @@ def init_data_model(entities):
Raises Raises
------ ------
DataModelError DataModelError
If any entity in `entities` does not exists. If any entity in `entities` does not exist or the role or data type is
not matching.
Returns Returns
------- -------
bool bool
True if all entities exist. True if all entities exist and their role and data type are matching.
""" """
try: try:
for e in entities: for e in entities:
local_datatype = e.datatype
local_role = e.role
e.retrieve() e.retrieve()
if local_datatype is not None and local_datatype != e.datatype:
info = ("The remote entity has a {} data type while it should "
"have a {}.".format(e.datatype, local_datatype))
raise DataModelError(e.name, info)
if local_role is not None and local_role != e.role:
info = ("The remote entity has is a {} while it should "
"be a {}.".format(e.role, local_role))
raise DataModelError(e.name, info)
except db.exceptions.EntityDoesNotExistError: except db.exceptions.EntityDoesNotExistError:
raise DataModelError(e.name) raise DataModelError(e.name, "This entity does not exist.")
return True return True
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment