Skip to content

Commit 871d26a

Browse files
ogenstadajtmccarty
andauthored
Generate uniqueness_constraints for schema node if hfid exists (#4344)
* Generate uniqueness_constraints for schema node if hfid exists Fixes #4186 * one more unit test --------- Co-authored-by: Aaron McCarty <[email protected]>
1 parent 75d19fe commit 871d26a

File tree

3 files changed

+13
-15
lines changed

3 files changed

+13
-15
lines changed

backend/infrahub/core/schema_manager.py

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -778,7 +778,6 @@ def validate_human_friendly_id(self) -> None:
778778
if not node_schema.human_friendly_id:
779779
continue
780780

781-
has_unique_item = False
782781
allowed_types = SchemaElementPathType.ATTR | SchemaElementPathType.REL_ONE_ATTR
783782
for item in node_schema.human_friendly_id:
784783
schema_path = self.validate_schema_path(
@@ -788,9 +787,6 @@ def validate_human_friendly_id(self) -> None:
788787
element_name="human_friendly_id",
789788
)
790789

791-
if schema_path.attribute_schema.unique:
792-
has_unique_item = True
793-
794790
if schema_path.is_type_attribute:
795791
required_properties = tuple(schema_path.attribute_schema.get_class().get_allowed_property_in_path())
796792
if schema_path.attribute_property_name not in required_properties:
@@ -812,11 +808,6 @@ def validate_human_friendly_id(self) -> None:
812808
f"{node_schema.kind}. ({item})"
813809
)
814810

815-
if not has_unique_item:
816-
raise ValueError(
817-
f"At least one attribute must be unique in the human_friendly_id for {node_schema.kind}."
818-
)
819-
820811
def validate_required_relationships(self) -> None:
821812
reverse_dependency_map: dict[str, set[str]] = {}
822813
for name in self.node_names + self.generic_names:
@@ -1100,7 +1091,7 @@ def process_human_friendly_id(self) -> None:
11001091
self.set(name=node.kind, schema=node)
11011092
break
11021093

1103-
if node.human_friendly_id and not node.unique_attributes and not node.uniqueness_constraints:
1094+
if node.human_friendly_id and not node.uniqueness_constraints:
11041095
uniqueness_constraints: list[str] = []
11051096
for item in node.human_friendly_id:
11061097
schema_attribute_path = node.parse_schema_path(path=item, schema=self)

backend/tests/unit/core/schema_manager/test_manager_schema.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -192,19 +192,22 @@ async def test_schema_branch_process_inheritance_update_inherited_elements(anima
192192
([["breed__value"]], [], ["name__value"]),
193193
(None, ["breed"], ["name__value"]),
194194
([["name__value", "breed__value"]], ["breed"], ["name__value"]),
195+
(None, ["name"], ["name__value"]),
196+
(None, [], ["name__value", "breed__value"]),
195197
],
196198
)
197-
async def test_validate_human_friendly_id_uniqueness_failure(
199+
async def test_validate_human_friendly_id_assign_uniquess_constraints(
198200
uniqueness_constraints: list[list[str]] | None,
199201
unique_attributes: list[str],
200202
human_friendly_id: list[str] | None,
201203
animal_person_schema_dict,
202204
):
203205
schema = SchemaBranch(cache={}, name="test")
204-
for node_schema in animal_person_schema_dict["nodes"]:
206+
for node_schema in animal_person_schema_dict["generics"]:
205207
if node_schema["name"] == "Animal" and node_schema["namespace"] == "Test":
206208
node_schema["uniqueness_constraints"] = None
207209
node_schema["human_friendly_id"] = None
210+
for node_schema in animal_person_schema_dict["nodes"]:
208211
if node_schema["name"] == "Dog" and node_schema["namespace"] == "Test":
209212
node_schema["uniqueness_constraints"] = uniqueness_constraints
210213
node_schema["human_friendly_id"] = human_friendly_id
@@ -213,9 +216,12 @@ async def test_validate_human_friendly_id_uniqueness_failure(
213216
schema.load_schema(schema=SchemaRoot(**animal_person_schema_dict))
214217

215218
schema.process_inheritance()
216-
schema.sync_uniqueness_constraints_and_unique_attributes()
217-
with pytest.raises(ValueError, match=r"At least one attribute must be unique in the human_friendly_id"):
218-
schema.validate_human_friendly_id()
219+
schema.validate_human_friendly_id()
220+
schema.process_human_friendly_id()
221+
222+
dog_node = schema.get("TestDog")
223+
expected_uniqueness_constraints = uniqueness_constraints or [human_friendly_id]
224+
assert dog_node.uniqueness_constraints == expected_uniqueness_constraints
219225

220226

221227
@pytest.mark.parametrize(

changelog/4186.fixed.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Ensure schema uniqueness_constraint are created if they are missing and human_friendly_id has been specified for the node

0 commit comments

Comments
 (0)