Skip to content

Commit cbf6599

Browse files
authored
Remove branch parameter from ConvertObjectType mutation (#6616)
1 parent b5276af commit cbf6599

File tree

5 files changed

+35
-35
lines changed

5 files changed

+35
-35
lines changed

backend/infrahub/core/convert_object_type/conversion.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ async def convert_object_type(
101101
deleted_node_out_rels_peer_ids = await get_out_rels_peers_ids(node=node, db=dbt)
102102
deleted_node_unidir_rels_peer_ids = await get_unidirectional_rels_peers_ids(node=node, db=dbt, branch=branch)
103103

104+
# Delete the node, so we delete relationships with peers as well, which might temporarily break cardinality constraints
105+
# but they should be restored when creating the new node.
104106
deleted_nodes = await NodeManager.delete(db=dbt, branch=branch, nodes=[node], cascade_delete=False)
105107
if len(deleted_nodes) != 1:
106108
raise ValueError(f"Deleted {len(deleted_nodes)} nodes instead of 1")

backend/infrahub/graphql/mutations/convert_object_type.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
from infrahub.core import registry
88
from infrahub.core.convert_object_type.conversion import InputForDestField, convert_object_type
9+
from infrahub.core.convert_object_type.schema_mapping import get_schema_mapping
910
from infrahub.core.manager import NodeManager
1011

1112
if TYPE_CHECKING:
@@ -16,7 +17,6 @@ class ConvertObjectTypeInput(InputObjectType):
1617
node_id = String(required=True)
1718
target_kind = String(required=True)
1819
fields_mapping = GenericScalar(required=True) # keys are destination attributes/relationships names.
19-
branch = String(required=True)
2020

2121

2222
class ConvertObjectType(Mutation):
@@ -37,17 +37,26 @@ async def mutate(
3737

3838
graphql_context: GraphqlContext = info.context
3939

40+
node_to_convert = await NodeManager.get_one(
41+
id=str(data.node_id), db=graphql_context.db, branch=graphql_context.branch
42+
)
43+
44+
source_schema = registry.get_node_schema(name=node_to_convert.get_kind(), branch=graphql_context.branch)
45+
target_schema = registry.get_node_schema(name=str(data.target_kind), branch=graphql_context.branch)
46+
4047
fields_mapping: dict[str, InputForDestField] = {}
4148
if not isinstance(data.fields_mapping, dict):
4249
raise ValueError(f"Expected `fields_mapping` to be a `dict`, got {type(fields_mapping)}")
4350

44-
for field, input_for_dest_field_str in data.fields_mapping.items():
45-
fields_mapping[field] = InputForDestField(**input_for_dest_field_str)
51+
for field_name, input_for_dest_field_str in data.fields_mapping.items():
52+
fields_mapping[field_name] = InputForDestField(**input_for_dest_field_str)
53+
54+
# Complete fields mapping with auto-mapping.
55+
mapping = get_schema_mapping(source_schema=source_schema, target_schema=target_schema)
56+
for field_name, mapping_value in mapping.items():
57+
if mapping_value.source_field_name is not None and field_name not in fields_mapping:
58+
fields_mapping[field_name] = InputForDestField(source_field=mapping_value.source_field_name)
4659

47-
node_to_convert = await NodeManager.get_one(
48-
id=str(data.node_id), db=graphql_context.db, branch=str(data.branch)
49-
)
50-
target_schema = registry.get_node_schema(name=str(data.target_kind), branch=data.branch)
5160
new_node = await convert_object_type(
5261
node=node_to_convert,
5362
target_schema=target_schema,

backend/infrahub/graphql/queries/convert_object_type_mapping.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,12 @@ class FieldsMapping(ObjectType):
1212

1313
async def fields_mapping_type_conversion_resolver(
1414
root: dict, # noqa: ARG001
15-
info: GraphQLResolveInfo, # noqa: ARG001
15+
info: GraphQLResolveInfo,
1616
source_kind: str,
1717
target_kind: str,
18-
branch: str,
1918
) -> dict:
20-
source_schema = registry.get_node_schema(name=source_kind, branch=branch)
21-
target_schema = registry.get_node_schema(name=target_kind, branch=branch)
19+
source_schema = registry.get_node_schema(name=source_kind, branch=info.context.branch)
20+
target_schema = registry.get_node_schema(name=target_kind, branch=info.context.branch)
2221

2322
mapping = get_schema_mapping(source_schema=source_schema, target_schema=target_schema)
2423
mapping_dict = {field_name: model.model_dump(mode="json") for field_name, model in mapping.items()}
@@ -29,7 +28,6 @@ async def fields_mapping_type_conversion_resolver(
2928
FieldsMapping,
3029
source_kind=String(),
3130
target_kind=String(),
32-
branch=String(),
3331
description="Retrieve fields mapping for converting object type",
3432
resolver=fields_mapping_type_conversion_resolver,
3533
required=True,

backend/tests/functional/convert_object_type/test_convert_object_type.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ async def test_get_fields_mapping(self, client: InfrahubClient, schemas_conversi
1414
response = await client.schema.load(schemas=[schemas_conversion])
1515
assert len(response.errors) == 0, response.errors
1616

17-
query = """ query($source_kind: String!, $target_kind: String!, $branch: String!) {
18-
FieldsMappingTypeConversion(source_kind: $source_kind, target_kind: $target_kind, branch: $branch) {
17+
query = """ query($source_kind: String!, $target_kind: String!) {
18+
FieldsMappingTypeConversion(source_kind: $source_kind, target_kind: $target_kind) {
1919
mapping
2020
}
2121
}
@@ -24,10 +24,10 @@ async def test_get_fields_mapping(self, client: InfrahubClient, schemas_conversi
2424
response = await client.execute_graphql(
2525
query=query,
2626
variables={
27-
"branch": "main",
2827
"source_kind": "TestconvPerson1",
2928
"target_kind": "TestconvPerson2",
3029
},
30+
branch_name="main",
3131
)
3232

3333
assert response == {
@@ -97,11 +97,10 @@ async def test_convert_object_type(self, client: InfrahubClient, schemas_convers
9797
await jack_1.save()
9898

9999
query = """
100-
mutation($node_id: String!, $target_kind: String!, $branch: String!, $fields_mapping: GenericScalar!) {
100+
mutation($node_id: String!, $target_kind: String!, $fields_mapping: GenericScalar!) {
101101
ConvertObjectType(data: {
102102
node_id: $node_id,
103103
target_kind: $target_kind,
104-
branch: $branch,
105104
fields_mapping: $fields_mapping
106105
}) {
107106
ok
@@ -112,9 +111,7 @@ async def test_convert_object_type(self, client: InfrahubClient, schemas_convers
112111

113112
mapping = {
114113
"name": InputForDestField(source_field="name"),
115-
"height": InputForDestField(source_field="height"),
116114
"age": InputForDestField(data=InputDataForDestField(attribute_value=25)),
117-
"favorite_car": InputForDestField(source_field="favorite_car"),
118115
"worst_car": InputForDestField(data=InputDataForDestField(peer_id=car_1.id)),
119116
"fastest_cars": InputForDestField(source_field="fastest_cars"),
120117
"slowest_cars": InputForDestField(data=InputDataForDestField(peers_ids=[car_1.id])),
@@ -126,11 +123,11 @@ async def test_convert_object_type(self, client: InfrahubClient, schemas_convers
126123
response = await client.execute_graphql(
127124
query=query,
128125
variables={
129-
"branch": "main",
130126
"node_id": str(jack_1.id),
131127
"fields_mapping": mapping_dict,
132128
"target_kind": "TestconvPerson2",
133129
},
130+
branch_name="main",
134131
)
135132
assert response["ConvertObjectType"]["ok"] is True
136133
res_node = response["ConvertObjectType"]["node"]

backend/tests/unit/core/convert_object_type/test_convert_object_type.py

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -144,46 +144,40 @@ async def test_convert_object_type(
144144
)
145145

146146
async def test_raise_on_break_mandatory_relationship(
147-
self, db: InfrahubDatabase, client: InfrahubClient, schema_conversion_mandatory_owner, default_branch
147+
self, db: InfrahubDatabase, client: InfrahubClient, schema_conversion_mandatory_owner, branch
148148
) -> None:
149149
# Add a mandatory relationship between TestPerson1 and TestCar, that would no longer exist after converting a TestPerson1 to a TestPerson2.
150-
res = await client.schema.load(schemas=[schema_conversion_mandatory_owner], branch=default_branch.name)
150+
res = await client.schema.load(schemas=[schema_conversion_mandatory_owner], branch=branch.name)
151151
assert len(res.errors) == 0, res.errors
152152

153153
jack_1 = await create_and_save(
154154
db=db,
155155
schema="TestmoPerson1",
156-
name=f"Jack-{default_branch.name}",
157-
branch=default_branch,
156+
name=f"Jack-{branch.name}",
157+
branch=branch,
158158
)
159159

160-
car_1 = await create_and_save(
161-
db=db, schema="TestmoCar", name="car_1", branch=default_branch, mandatory_owner=jack_1
162-
)
160+
car_1 = await create_and_save(db=db, schema="TestmoCar", name="car_1", branch=branch, mandatory_owner=jack_1)
163161

164162
# Refresh jack_1 now that we added a bag
165163
jack_1 = await NodeManager.get_one_by_id_or_default_filter(
166-
db=db, id=jack_1.id, kind="TestmoPerson1", prefetch_relationships=True, branch=default_branch
164+
db=db, id=jack_1.id, kind="TestmoPerson1", prefetch_relationships=True, branch=branch
167165
)
168166

169167
mapping = {
170168
"name": InputForDestField(source_field="name"),
171169
}
172170

173-
person_2_schema = registry.get_node_schema(name="TestmoPerson2", branch=default_branch)
171+
person_2_schema = registry.get_node_schema(name="TestmoPerson2", branch=branch)
174172
with pytest.raises(ValidationError, match=r"Too few relationships, min 1 at mandatory_owner"):
175-
await convert_object_type(
176-
node=jack_1, target_schema=person_2_schema, mapping=mapping, db=db, branch=default_branch
177-
)
173+
await convert_object_type(node=jack_1, target_schema=person_2_schema, mapping=mapping, db=db, branch=branch)
178174

179175
# And make sure it works when setting a new owner to the car
180176
mapping = {
181177
"name": InputForDestField(source_field="name"),
182178
"my_car": InputForDestField(data=InputDataForDestField(peer_id=car_1.id)),
183179
}
184-
await convert_object_type(
185-
node=jack_1, target_schema=person_2_schema, mapping=mapping, db=db, branch=default_branch
186-
)
180+
await convert_object_type(node=jack_1, target_schema=person_2_schema, mapping=mapping, db=db, branch=branch)
187181

188182
async def test_raise_on_break_mandatory_unidirectional_relationship(
189183
self,

0 commit comments

Comments
 (0)