Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auto Delete Empty Anchors and Sources in SQL Registry #1023

Merged
merged 1 commit into from
Feb 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions registry/sql-registry/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,12 @@ def delete_entity(entity: str):
entity_id = registry.get_entity_id(entity)
downstream_entities = registry.get_dependent_entities(entity_id)
if len(downstream_entities) > 0:
raise HTTPException(
status_code=412, detail=f"""Entity cannot be deleted as it has downstream/dependent entities.
Entities: {list([e.qualified_name for e in downstream_entities])}"""
)
registry.delete_empty_entities(downstream_entities)
if len(registry.get_dependent_entities(entity_id)) > 0:
raise HTTPException(
status_code=412, detail=f"""Entity cannot be deleted as it has downstream/dependent entities.
Entities: {list([e.qualified_name for e in downstream_entities])}"""
)
registry.delete_entity(entity_id)

@router.get("/projects/{project}/datasources")
Expand Down
22 changes: 22 additions & 0 deletions registry/sql-registry/registry/db_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,28 @@ def get_dependent_entities(self, entity_id: Union[str, UUID]) -> List[Entity]:
downstream_entities, _ = self._bfs(entity_id, RelationshipType.Produces)
return [e for e in downstream_entities if str(e.id) != str(entity_id)]

def delete_empty_entities(self, entities: List[Entity]):
"""
Given entity list, deleting all anchors that have no features and all sources that have no anchors.
"""
if len(entities) == 0:
return

# clean up empty anchors
for e in entities:
if e.entity_type == EntityType.Anchor:
downstream_entities, _ = self._bfs(e.id, RelationshipType.Contains)
if len(downstream_entities) == 1: # only anchor itself
self.delete_entity(e.id)
# clean up empty sources
for e in entities:
if e.entity_type == EntityType.Source:
downstream_entities, _ = self._bfs(e.id, RelationshipType.Produces)
if len(downstream_entities) == 1: # only source itself
xiaoyongzhu marked this conversation as resolved.
Show resolved Hide resolved
self.delete_entity(e.id)

return

def delete_entity(self, entity_id: Union[str, UUID]):
"""
Deletes given entity
Expand Down