Fixed #23474 - Schema migrations can inadvertently destroy data

My usage of `get_migration_by_prefix` looks a bit funky, I'm not sure if there's a better way.

I haven't run the tests, or written any new tests yet.
It would be more helpful to describe the test setup (the migration dependencies and what's expected to happen)