From 6113eceda5e27950ff089e51485d9cb6c868be93 Mon Sep 17 00:00:00 2001 From: Alex Bridge Date: Mon, 17 Jun 2024 13:00:21 +0100 Subject: [PATCH] Handle CircularDependencyException at the topmost level of operation ordering --- wagtail_transfer/operations.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/wagtail_transfer/operations.py b/wagtail_transfer/operations.py index 68acb2c..4b960eb 100644 --- a/wagtail_transfer/operations.py +++ b/wagtail_transfer/operations.py @@ -467,8 +467,20 @@ def run(self): # arrange operations into an order that satisfies dependencies operation_order = [] - for operation in satisfiable_operations: - self._add_to_operation_order(operation, operation_order, [operation]) + while satisfiable_operations: + held_operations = [] + for operation in satisfiable_operations: + try: + self._add_to_operation_order(operation, operation_order, [operation]) + except CircularDependencyException as e: + # if the exception has propagated to this level it ought to be a soft dependency, + # as otherwise it should have been caught by _check_satisfiable. + # Hold it back to retry adding later + held_operations.append(operation) + if len(held_operations) == len(satisfiable_operations): + # Not managed to add any of the operations - can't proceed + raise CircularDependencyException("Unsatisfiable circular dependency found") + satisfiable_operations = held_operations # run operations in order with transaction.atomic():