diffsync icon indicating copy to clipboard operation
diffsync copied to clipboard

DiffSyncModel: Create/Update ordering and Delete ordering

Open FragmentedPacket opened this issue 2 years ago • 1 comments

This came up in internal conversations around usage and some potential improvements.

Potentially adding a flag that will set the create/update to process parents before children and then also the reverse, children are deleted before parents.

An example would be Nautobot and the dependencies of objects within Nautobot. Say you want to delete a site, but children objects exist such as devices, you need to delete the devices before deleting the site. This caused some intermittent and hard to troubleshoot scenarios.

It was brought up that some of these deletions would be deferred as well which may not be wanted.

FragmentedPacket avatar Feb 01 '22 15:02 FragmentedPacket

I had this scenario as well, for my use case (which might be generalizable) I have taken a approach similar to this:

class CustomerOrderingDiff(Diff):
    def get_children(self) -> Iterator["DiffElement"]:
        deferred_children = []

        for child in self.children["some_model_that_other_models_depend_on"].values():
            if child.action == DiffSyncActions.DELETE:
                deferred_children.append(child)
            else:
                yield child
        
        for group in self.groups():
            if group == "some_model_that_other_models_depend_on":
                continue
            order_method_name = f"order_children_{group}"
            if hasattr(self, order_method_name):
                order_method = getattr(self, order_method_name)
            else:
                order_method = getattr(self, order_default)

            yield from order_method(self.children[group])
        yield from deferred_children

I'll look into possibly contributing this once I have the approach fleshed out a little more.

Kircheneer avatar Mar 04 '22 11:03 Kircheneer