Prevent possible recursion limit issue in migration planning

This switches out the recursive depth first search for stack based approach.

Additionally adds test that replicates original potential problem.