WIP: Backport migration fixes from #6650

(backports to stable6, despite branch name)

Still WIP