Skip to content

Commit 1c1a372

Browse files
committed
fix(backend): fix resource pool allocation in update mutation
Signed-off-by: Fatih Acar <[email protected]>
1 parent 953f447 commit 1c1a372

File tree

3 files changed

+38
-8
lines changed

3 files changed

+38
-8
lines changed

backend/infrahub/core/attribute.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -578,7 +578,12 @@ def _filter_sensitive(self, value: str, filter_sensitive: bool) -> str:
578578

579579
return value
580580

581-
async def from_graphql(self, data: dict, db: InfrahubDatabase) -> bool:
581+
async def from_graphql(
582+
self,
583+
data: dict,
584+
db: InfrahubDatabase,
585+
process_pools: bool = True,
586+
) -> bool:
582587
"""Update attr from GraphQL payload"""
583588

584589
changed = False
@@ -592,7 +597,8 @@ async def from_graphql(self, data: dict, db: InfrahubDatabase) -> bool:
592597
changed = True
593598
elif "from_pool" in data:
594599
self.from_pool = data["from_pool"]
595-
await self.node.handle_pool(db=db, attribute=self, errors=[])
600+
if process_pools:
601+
await self.node.handle_pool(db=db, attribute=self, errors=[])
596602
changed = True
597603

598604
if changed and self.is_from_profile:

backend/infrahub/core/node/__init__.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -963,15 +963,29 @@ async def to_graphql(
963963

964964
return response
965965

966-
async def from_graphql(self, data: dict, db: InfrahubDatabase) -> bool:
967-
"""Update object from a GraphQL payload."""
966+
async def from_graphql(
967+
self,
968+
data: dict,
969+
db: InfrahubDatabase,
970+
process_pools: bool = True,
971+
) -> bool:
972+
"""Update object from a GraphQL payload.
973+
974+
Args:
975+
data: GraphQL payload to apply.
976+
db: Database connection used for related lookups.
977+
process_pools: Whether resource pool allocations should be performed.
978+
979+
Returns:
980+
True if any field changed, otherwise False.
981+
"""
968982

969983
changed = False
970984

971985
for key, value in data.items():
972986
if key in self._attributes and isinstance(value, dict):
973987
attribute = getattr(self, key)
974-
changed |= await attribute.from_graphql(data=value, db=db)
988+
changed |= await attribute.from_graphql(data=value, db=db, process_pools=process_pools)
975989

976990
if key in self._relationships:
977991
rel: RelationshipManager = getattr(self, key)

backend/infrahub/graphql/mutations/main.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -211,16 +211,26 @@ async def _call_mutate_update(
211211
Wrapper around mutate_update to potentially activate locking and call it within a database transaction.
212212
"""
213213
before_mutate_profile_ids = await get_profile_ids(db=db, obj=obj)
214-
await obj.from_graphql(db=db, data=data)
215214
fields_to_validate = list(data)
216215
fields = list(data.keys())
217216

218217
for field_to_remove in ("id", "hfid"):
219218
if field_to_remove in fields:
220219
fields.remove(field_to_remove)
221220

221+
# Prepare a clone to compute locks without triggering pool allocations
222+
preview_obj = await NodeManager.get_one_by_id_or_default_filter(
223+
db=db,
224+
kind=obj.get_kind(),
225+
id=obj.get_id(),
226+
branch=branch,
227+
include_owner=True,
228+
include_source=True,
229+
)
230+
await preview_obj.from_graphql(db=db, data=data, process_pools=False)
231+
222232
schema_branch = db.schema.get_schema_branch(name=branch.name)
223-
lock_names = get_lock_names_on_object_mutation(node=obj, branch=branch, schema_branch=schema_branch)
233+
lock_names = get_lock_names_on_object_mutation(node=preview_obj, branch=branch, schema_branch=schema_branch)
224234

225235
async def _mutate(current_db: InfrahubDatabase) -> tuple[Node, Self]:
226236
updated_obj = await cls.mutate_update_object(
@@ -235,7 +245,7 @@ async def _mutate(current_db: InfrahubDatabase) -> tuple[Node, Self]:
235245
previous_profile_ids=before_mutate_profile_ids,
236246
lock_names=lock_names,
237247
manage_lock=False,
238-
apply_data=False,
248+
apply_data=True,
239249
)
240250
result = await cls.mutate_update_to_graphql(db=current_db, info=info, obj=updated_obj)
241251
return updated_obj, result

0 commit comments

Comments
 (0)