Ensure internal migrations are done first so that __first__ always works

Fixes #22861