Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

db:schema:load db:schema:dump: establish_connection(Rails.env) -> establ... #6736

Closed
wants to merge 1 commit into
from

Conversation

Projects
None yet
4 participants

clacke commented Jun 14, 2012

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:

...
<migrations>
...
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.

@clacke clacke db:schema:load db:schema:dump: establish_connection(Rails.env) -> est…
…ablish_connection'

etablish_connection(Rails.env) messes up when DATABASE_URL points to a valid db,
but Rails.env points to an invalid config in config/database.yml.

The behavior of using DATABASE_URL first matches that of db:migrate.
1c9d522
Contributor

wuputah commented Jun 22, 2012

Heroku's config/database.yml uses RAILS_ENV or RACK_ENV:
https://gist.github.com/2970179

Member

steveklabnik commented Sep 19, 2012

With #7521 merged, can you see if this is still an issue, @clacke ?

Member

steveklabnik commented Nov 17, 2012

Hey @clacke . I haven't heard from you in 5 months, and I'm pretty sure that #7521 fixes your issue. If you can demonstrate that this is still a problem, we can re-open, but I'm closing for now.

clacke commented Jan 31, 2013

Now, three months and several point releases later I can confirm that my issue is indeed fixed. :-)

Sorry about that.

@clacke clacke deleted the clacke:schema_dump_database_url_first branch Jan 31, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment