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.