Skip to content

Commit

Permalink
Fix the issue where the relationship data of children is not deleted …
Browse files Browse the repository at this point in the history
…during replacement.
  • Loading branch information
jieguangzhou committed Feb 19, 2025
1 parent a78124a commit 3436731
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 7 deletions.
14 changes: 8 additions & 6 deletions plugins/mongodb/superduper_mongodb/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ def delete_parent_child(self, parent: str, child: str) -> None:
"""
self.parent_child_mappings.delete_many(
{
'parent': parent,
'child': child,
'parent_id': parent,
'child_id': child,
}
)

Expand All @@ -95,8 +95,8 @@ def create_parent_child(self, parent: str, child: str) -> None:
"""
self.parent_child_mappings.insert_one(
{
'parent': parent,
'child': child,
'parent_id': parent,
'child_id': child,
}
)

Expand Down Expand Up @@ -275,7 +275,7 @@ def component_version_has_parents(
{'type_id': type_id, 'identifier': identifier, 'version': version},
{'uuid': 1, 'id': 1},
)['uuid']
doc = {'child': uuid}
doc = {'child_id': uuid}
return self.parent_child_mappings.count_documents(doc)

def delete_component_version(
Expand Down Expand Up @@ -358,7 +358,9 @@ def get_component_version_parents(self, uuid: str) -> t.List[str]:
:param uuid: unique identifier of component
"""
return [r['parent'] for r in self.parent_child_mappings.find({'child': uuid})]
return [
r['parent_id'] for r in self.parent_child_mappings.find({'child_id': uuid})
]

def _replace_object(
self,
Expand Down
3 changes: 3 additions & 0 deletions plugins/sqlalchemy/superduper_sqlalchemy/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,9 @@ def _init_tables(self):

self._table_mapping = {
'_artifact_relations': self.artifact_table,
'_parent_child': self.parent_child_association_table,
'_component': self.component_table,
'_job': self.job_table,
}

try:
Expand Down
9 changes: 9 additions & 0 deletions superduper/backends/base/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,15 @@ def get_artifact_relations(self, uuid=None, artifact_id=None):
ids = [relation['uuid'] for relation in relations]
return ids

def get_children_relations(self, parent: str):
"""
Get all children of a component.
:param parent: parent component
"""
relations = self._get_data('_parent_child', {'parent_id': parent})
return [relation['child_id'] for relation in relations]

# TODO: Refactor to use _create_data, _delete_data, _get_data
@abstractmethod
def _create_data(self, table_name, datas):
Expand Down
4 changes: 4 additions & 0 deletions superduper/base/apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,10 @@ def replace_existing(x):
# to understand if they are now referring to the "original"
# or the "new" version.
non_breaking_changes[object.uuid] = current.uuid

for event in create_events.values():
if isinstance(event, (Create, Update)) and event.parent == object.uuid:
event.parent = current.uuid

current.handle_update_or_same(object)

Expand Down
10 changes: 10 additions & 0 deletions superduper/base/datalayer.py
Original file line number Diff line number Diff line change
Expand Up @@ -668,6 +668,7 @@ def _remove_component_version(
children = component.get_children(deep=True)
children = component.sort_components(children)[::-1]
for c in children:
self.metadata.delete_parent_child(component.uuid, c.uuid)
assert isinstance(c.version, int)
try:
self._remove_component_version(
Expand Down Expand Up @@ -731,10 +732,19 @@ def _replace_fn(component):
version=object.version,
)
self.expire(old_uuid)
self._remove_old_children_relations(object)
else:
serialized = serialized.encode(keep_schema=False)
self.metadata.create_component(serialized)


def _remove_old_children_relations(self, object: Component):
exists_relations = self.metadata.get_children_relations(object.uuid)
now_relations = [c.uuid for c in object.get_children(deep=True)]
delete_relations = set(exists_relations) - set(now_relations)
for c in delete_relations:
self.metadata.delete_parent_child(object.uuid, c)

def expire(self, uuid):
"""Expire a component from the cache."""
self.cluster.cache.expire(uuid)
Expand Down
3 changes: 2 additions & 1 deletion superduper/rest/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ def _apply():
identifier = component['identifier']
if '_variables' in component:
variables = component['_variables']
# variables["application_identifier"] = identifier

component = Document.decode(component, db=db).unpack()
if variables:
Expand Down Expand Up @@ -472,7 +473,7 @@ def build_frontend(app: SuperDuperApp, host: str = 'localhost', port: int = 8000
if file.endswith('.js'):
with open(os.path.join(root, file), "r") as f:
content = f.read()
content = content.replace("localhost:8000", f"{ host }:{ port }")
# content = content.replace("localhost:8000", f"{ host }:{ port }")
with open(os.path.join(root, file), "w") as f:
f.write(content)

Expand Down

0 comments on commit 3436731

Please sign in to comment.