databases.rake refactoring

`databases.rake` is not the best ruby code. This pull request contains some improvements, making the code more functional and using things like `Pathname#sub_ext` and `Array#grep` instead of crazy regular expressions and loops with temporary variables and nested conditionals.

I'm not sure if having such many commits in one PR is a good thing. I tried to make them granular and self-descriptive. If it is not ok, I can squash them together or send them via separate pull requests.