Database connection parameters are tied to the filesystem

HI Guys,

I recently tried VERY hard to override the database configuration bassed on
ENV vars or actual API calls. This task is almost next to impossible.

I know that the database.yml file is parsed as ERB but that is sane for
simple ENV replacement if you want to actually change which database adaptor
it is (requires extra config/params) it becomes messy.

I found https://github.com/rails/rails/issues/5297 which explains the
issue/solution far better then I could.

Active record supports this natively, I can't see why rails would force the
requirement of a database.yml and for it to have a "production" section. see
http://apidock.com/rails/ActiveRecord/Base/establish_connection/class URL
section.

As I mentioned on the ticket, I would rather have ENV["DATABASE_URL"]
supported as well, but I can't see how it's "VERY hard to override".
See https://github.com/rails/rails/issues/5297#issuecomment-7037942

I've been using variations of that for a long time, and it works pretty well.

Cheers,
-foca

3.2+ should have support for a DATABASE_URL environment variable:

  https://github.com/rails/rails/blob/master/activerecord/lib/active_record/connection_handling.rb#L39

Which you should be able to use like this:

  $ DATABASE_URL='sqlite3:///db/development.sqlite3' rake do:whatever

But that doesn't help the problem in #5297. The problem in #5297 is
that Heroku calculates it's own parameters to pass in for the URL.
AFAIK, there is no way to augment the connection parameters that Heroku
sends. But this seems like an issue with Heroku rather than Rails.

If you find entry points where the environment variable *isn't* used,
then we should consider it a bug. For example, I just discovered that
`rake db:schema:dump` will not use the environment variable:

  https://github.com/rails/rails/blob/master/activerecord/lib/active_record/railties/databases.rake#L229

So someone should send a patch for that. :slight_smile:

If you find entry points where the environment variable *isn't* used,
then we should consider it a bug. For example, I just discovered that
`rake db:schema:dump` will not use the environment variable:
  https://github.com/rails/rails/blob/master/activerecord/lib/active_record/railties/databases.rake#L229

So someone should send a patch for that. :slight_smile:

Is it simple as:

- ActiveRecord::Base.establish_connection(Rails.env)
+ ActiveRecord::Base.establish_connection(ENV["DATABASE_URL"]

Rails.env)

wherever I see it not being used? Also inside namespace :test? Are
there tests for rake tasks?

Yes, but I think it would be better if we change the definition of
`establish_connection` in connection_handling.rb. Like this:

diff --git a/activerecord/lib/active_record/connection_handling.rb b/activerecord/lib/active_record/connection_handling.rb
index bda41df..ce1f84f 100644
--- a/activerecord/lib/active_record/connection_handling.rb
+++ b/activerecord/lib/active_record/connection_handling.rb
@@ -36,7 +36,7 @@ module ActiveRecord