Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Ensure DATABASE_URL is not ignored when database.yml isn't loaded #6833

Closed
wants to merge 2 commits into from

5 participants

@macksmind

Currently specifying an environment name when DATABASE_URL is present
prevents a sucessful connection. Passing a string such as "production"
causes DATABASE_URL to be ignored because
ActiveRecord::Base.establish_connection only uses it if no name is
passed, but database.yml isn't loaded by the initializer if DATABASE_URL
is defined.

This change causes the resolver to use DATABASE_URL instead of failing
on an unusable environment name without breaking the ability to
pass a valid url in a string.

Among other things, this fixes rake db:fixtures:load on Heroku Cedar.

@macksmind macksmind Ensure DATABASE_URL is not ignored when database.yml isn't loaded
Currently specifying an environment name when DATABASE_URL is present
prevents a sucessful connection. Passing a string such as "production"
causes DATABASE_URL to be ignored because
ActiveRecord::Base.establish_connection only uses it if no name is
passed, but database.yml isn't loaded by the initializer if DATABASE_URL
is defined.

This change causes the resolver to use DATABASE_URL instead of failing
on an unusable environment name without breaking the ability to
pass a valid url in a string.
3a9e25c
@dmathieu
Collaborator

Awesome ! :+1:

@schneems
Collaborator

@macksmind can you take a look at #7521 and tell me if this is still an issue?

@steveklabnik
Collaborator

@macksmind I'm pretty sure that #7521 fixes your issue, and we haven't heard from you in months. I'm giving this a close, and if you can demonstrate that it's still a problem, we can re-open.

@macksmind

@steveklabnik My use case was running rake db:fixtures:load on a new app at Heroku. That worked for me this morning, so I'm happy.

@steveklabnik
Collaborator

:metal: !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jun 22, 2012
  1. @macksmind

    Ensure DATABASE_URL is not ignored when database.yml isn't loaded

    macksmind authored
    Currently specifying an environment name when DATABASE_URL is present
    prevents a sucessful connection. Passing a string such as "production"
    causes DATABASE_URL to be ignored because
    ActiveRecord::Base.establish_connection only uses it if no name is
    passed, but database.yml isn't loaded by the initializer if DATABASE_URL
    is defined.
    
    This change causes the resolver to use DATABASE_URL instead of failing
    on an unusable environment name without breaking the ability to
    pass a valid url in a string.
Commits on Jun 24, 2012
  1. @macksmind

    Minor refactoring of new test

    macksmind authored
    Fix sloppiness from 3a9e25c
This page is out of date. Refresh to see the latest.
View
1  activerecord/lib/active_record/connection_adapters/abstract/connection_specification.rb
@@ -31,6 +31,7 @@ def spec
private
def resolve_string_connection(spec) # :nodoc:
hash = configurations.fetch(spec) do |k|
+ k = ENV['DATABASE_URL'] if ENV['DATABASE_URL'] && !k.include?(':')
connection_url_to_hash(k)
end
View
22 activerecord/test/cases/connection_specification/resolver_test.rb
@@ -8,6 +8,13 @@ def resolve(spec)
Resolver.new(spec, {}).spec.config
end
+ def with_database_url(new_url)
+ old_url, ENV['DATABASE_URL'] = ENV['DATABASE_URL'], new_url
+ yield
+ ensure
+ old_url ? ENV['DATABASE_URL'] = old_url : ENV.delete('DATABASE_URL')
+ end
+
def test_url_host_no_db
spec = resolve 'mysql://foo?encoding=utf8'
assert_equal({
@@ -35,6 +42,21 @@ def test_url_port
:host => "foo",
:encoding => "utf8" }, spec)
end
+
+ def test_url_no_database_yml
+ with_database_url 'sqlite3://localhost/db/production.sqlite3' do
+ spec = resolve ENV["DATABASE_URL"]
+ assert_equal({
+ :adapter => "sqlite3",
+ :database => "db/production.sqlite3",
+ :host => "localhost" }, spec)
+ spec = resolve 'production'
+ assert_equal({
+ :adapter => "sqlite3",
+ :database => "db/production.sqlite3",
+ :host => "localhost" }, spec)
+ end
+ end
end
end
end
Something went wrong with that request. Please try again.