rails/rails — pull request #6735 by clacke (2012-06-14 12:35:22+00:00)
Title: Schema dump database url first

Fix `db:schema:load` + `:dump`: look first in DATABASE_URL, then database.yml.

Currently when I run `heroku run rake db:migrate` on Heroku with the official
Rails 3.2.6 I get:

...

...
database configuration does not specify adapter

... and then rake fails.

My findings:

This last line is printed by `db:schema:dump` called from within `db:migrate`.

`db:migrate` "itself" (`ActiveRecord::Migrator.migrate`) uses
`with_connection`, which succeeds, while `db:schema:dump` uses
`ActiveRecord::Base.establish_connect(Rails.env)` followed by
`ActiveRecord::Base.connection`, which fails.

The latter call unconditionally looks in config/database.yml for the database
settings, whereas the former uses whatever settings were initialized by Rails.

When Rails is initializing, `Railties::ActiveRecord` calls
ActiveRecord::Base.establish_connection without parameters.

Removing the `Rails.env` argument allows `establish_connect` to probe for the
correct settings itself, and then it picks `DATABASE_URL` first, regardless of
`RAILS_ENV` or `RACK_ENV`. This seems to me the more proper behaviour.

If I set `RAILS_ENV=production`, not only `RACK_ENV=production`, for some reason
that works. Possibly heroku's database.yml contains a `#{RAILS_ENV}:` line or
something along those lines, I haven't checked. Either way, it doesn't affect
the above reasoning for the proper behaviour of `db:schema:dump`.

This pull request also contains the equivalent fix for `db:schema:load`.