Skip to content

Commit

Permalink
Ensure that deleted nodes are removed from the schema_branch
Browse files Browse the repository at this point in the history
Fixes #4836
  • Loading branch information
ogenstad committed Dec 10, 2024
1 parent e19331e commit a579bf6
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
8 changes: 6 additions & 2 deletions backend/infrahub/core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ def __str__(self) -> str:
class SchemaBranchDiff(BaseModel):
nodes: list[str] = Field(default_factory=list)
generics: list[str] = Field(default_factory=list)
removed_nodes: list[str] = Field(default_factory=list)
removed_generics: list[str] = Field(default_factory=list)

def to_string(self) -> str:
return ", ".join(self.nodes + self.generics)
Expand All @@ -40,7 +42,7 @@ def to_list(self) -> list[str]:

@property
def has_diff(self) -> bool:
if self.nodes or self.generics:
if self.nodes or self.generics or self.removed_nodes or self.removed_generics:
return True
return False

Expand All @@ -50,15 +52,17 @@ class SchemaBranchHash(BaseModel):
nodes: dict[str, str] = Field(default_factory=dict)
generics: dict[str, str] = Field(default_factory=dict)

def compare(self, other: SchemaBranchHash) -> Optional[SchemaBranchDiff]:
def compare(self, other: SchemaBranchHash) -> SchemaBranchDiff | None:
if other.main == self.main:
return None

return SchemaBranchDiff(
nodes=[key for key, value in other.nodes.items() if key not in self.nodes or self.nodes[key] != value],
removed_nodes=[key for key in self.nodes if key not in other.nodes],
generics=[
key for key, value in other.generics.items() if key not in self.generics or self.generics[key] != value
],
removed_generics=[key for key in self.generics if key not in other.generics],
)


Expand Down
6 changes: 6 additions & 0 deletions backend/infrahub/core/schema/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -636,6 +636,12 @@ async def load_schema_from_db(
if filter_value["namespace__values"]:
filters[node_type] = filter_value
has_filters = True
for removed_generic in schema_diff.removed_generics:
if removed_generic in schema.generic_names:
schema.delete(name=removed_generic)
for removed_node in schema_diff.removed_nodes:
if removed_node in schema.node_names:
schema.delete(name=removed_node)

if not has_filters or filters["generics"]:
generic_schema = self.get(name="SchemaGeneric", branch=branch)
Expand Down
1 change: 1 addition & 0 deletions changelog/4836.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Ensure that deleted schema nodes are removed from all workers and that the schema is in sync without having to restart

0 comments on commit a579bf6

Please sign in to comment.