rake railties:install:migrations respects the order of railties

This PR fixes #8930 and some stuff from #8985

I am not sure this is right way to do this but this PR has test cases to see the problem.

As @parndt did in #8985 can make this public and have code in `database.rake` 

cc/ @m1k3