Skip to content

Commit

Permalink
Merge pull request #5128 from opsmill/ajtm-12022024-schema-rename-dif…
Browse files Browse the repository at this point in the history
…f-fix

fix SchemaNotFoundError for renamed schema during diff enrichment
  • Loading branch information
ajtmccarty authored Dec 2, 2024
2 parents 3edde6f + 5fe1f98 commit ee01a64
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
14 changes: 11 additions & 3 deletions backend/infrahub/core/diff/payload_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from infrahub.core.constants import DiffAction, RelationshipCardinality
from infrahub.core.manager import NodeManager
from infrahub.core.registry import registry
from infrahub.exceptions import SchemaNotFoundError
from infrahub.log import get_logger

from .model.diff import (
Expand Down Expand Up @@ -42,12 +43,17 @@


async def get_display_labels_per_kind(
kind: str, ids: list[str], branch_name: str, db: InfrahubDatabase
kind: str, ids: list[str], branch_name: str, db: InfrahubDatabase, skip_missing_schema: bool = False
) -> dict[str, str]:
"""Return the display_labels of a list of nodes of a specific kind."""
branch = await registry.get_branch(branch=branch_name, db=db)
schema_branch = db.schema.get_schema_branch(name=branch.name)
fields = schema_branch.generate_fields_for_display_label(name=kind)
try:
fields = schema_branch.generate_fields_for_display_label(name=kind)
except SchemaNotFoundError:
if skip_missing_schema:
return {}
raise
nodes = await NodeManager.get_many(ids=ids, fields=fields, db=db, branch=branch)
return {node_id: await node.render_display_label(db=db) for node_id, node in nodes.items()}

Expand All @@ -59,7 +65,9 @@ async def get_display_labels(nodes: dict[str, dict[str, list[str]]], db: Infrahu
if branch_name not in response:
response[branch_name] = {}
for kind, ids in items.items():
labels = await get_display_labels_per_kind(kind=kind, ids=ids, db=db, branch_name=branch_name)
labels = await get_display_labels_per_kind(
kind=kind, ids=ids, db=db, branch_name=branch_name, skip_missing_schema=True
)
response[branch_name].update(labels)

return response
Expand Down
1 change: 1 addition & 0 deletions changelog/+schema-rename-diff.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix issue that could cause diff generation to crash if a schema was renamed

0 comments on commit ee01a64

Please sign in to comment.