aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorRafael Mendonça França <rafaelmfranca@gmail.com>2014-04-07 19:05:50 -0300
committerMatthew Draper <matthew@trebex.net>2014-04-08 15:14:58 +0930
commite533826f483d6a1f45d044a2017ff4e9d19a154e (patch)
treeb36a1aa2faa1b7f476344749daf35cd8060714a2 /activerecord
parent2b817cde622e625f08a638d909f3e9b12f0b3066 (diff)
downloadrails-e533826f483d6a1f45d044a2017ff4e9d19a154e.tar.gz
rails-e533826f483d6a1f45d044a2017ff4e9d19a154e.tar.bz2
rails-e533826f483d6a1f45d044a2017ff4e9d19a154e.zip
Make sure DEFAULT_URL only override current environment database
configuration
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/lib/active_record/connection_handling.rb2
-rw-r--r--activerecord/test/cases/connection_adapters/connection_handler_test.rb38
2 files changed, 30 insertions, 10 deletions
diff --git a/activerecord/lib/active_record/connection_handling.rb b/activerecord/lib/active_record/connection_handling.rb
index 5a83797ee5..567d121091 100644
--- a/activerecord/lib/active_record/connection_handling.rb
+++ b/activerecord/lib/active_record/connection_handling.rb
@@ -78,7 +78,7 @@ module ActiveRecord
end
env_url ||= ENV["DATABASE_URL_#{key.upcase}"]
entry ||= {} if env_url
- entry.merge!("url" => env_url) { |h, v1, v2| v1 || v2 } if entry.is_a?(Hash)
+ entry.merge!("url" => env_url) { |h, v1, v2| v1 || v2 } if entry.is_a?(Hash) && env_url
hash[key] = entry if entry
end
diff --git a/activerecord/test/cases/connection_adapters/connection_handler_test.rb b/activerecord/test/cases/connection_adapters/connection_handler_test.rb
index ed7188e2e0..aec70fac79 100644
--- a/activerecord/test/cases/connection_adapters/connection_handler_test.rb
+++ b/activerecord/test/cases/connection_adapters/connection_handler_test.rb
@@ -11,10 +11,14 @@ module ActiveRecord
def setup
@previous_database_url = ENV.delete("DATABASE_URL")
+ @previous_database_url_default_env = ENV.delete("DATABASE_URL_DEFAULT_ENV")
+ @previous_database_url_production = ENV.delete("DATABASE_URL_PRODUCTION")
end
teardown do
ENV["DATABASE_URL"] = @previous_database_url
+ ENV["DATABASE_URL_DEFAULT_ENV"] = @previous_database_url_default_env
+ ENV["DATABASE_URL_PRODUCTION"] = @previous_database_url_production
end
def resolve(spec, config)
@@ -25,15 +29,23 @@ module ActiveRecord
ConnectionSpecification::Resolver.new(klass.new(config).resolve).spec(spec)
end
- def test_resolver_with_database_uri_and_known_key
+ def test_resolver_with_database_uri_and_current_env_symbol_key
ENV['DATABASE_URL'] = "postgres://localhost/foo"
+ config = { "not_production" => { "adapter" => "not_postgres", "database" => "not_foo" } }
+ actual = resolve(:default_env, config)
+ expected = { "adapter"=>"postgresql", "database"=>"foo", "host"=>"localhost" }
+ assert_equal expected, actual
+ end
+
+ def test_resolver_with_environment_database_uri_and_current_env_symbol_key
+ ENV['DATABASE_URL_DEFAULT_ENV'] = "postgres://localhost/foo"
config = { "default_env" => { "adapter" => "not_postgres", "database" => "not_foo" } }
actual = resolve(:default_env, config)
expected = { "adapter"=>"postgresql", "database"=>"foo", "host"=>"localhost" }
assert_equal expected, actual
end
- def test_resolver_with_database_uri_and_known_string_key
+ def test_resolver_with_database_uri_and_and_current_env_string_key
ENV['DATABASE_URL'] = "postgres://localhost/foo"
config = { "default_env" => { "adapter" => "not_postgres", "database" => "not_foo" } }
actual = assert_deprecated { resolve("default_env", config) }
@@ -41,10 +53,18 @@ module ActiveRecord
assert_equal expected, actual
end
- def test_resolver_with_database_uri_and_current_env_symbol_key
+ def test_resolver_with_database_uri_and_known_key
ENV['DATABASE_URL'] = "postgres://localhost/foo"
- config = { "not_production" => { "adapter" => "not_postgres", "database" => "not_foo" } }
- actual = resolve(:default_env, config)
+ config = { "production" => { "adapter" => "not_postgres", "database" => "not_foo", "host" => "localhost" } }
+ actual = resolve(:production, config)
+ expected = { "adapter"=>"not_postgres", "database"=>"not_foo", "host"=>"localhost" }
+ assert_equal expected, actual
+ end
+
+ def test_resolver_with_custom_database_uri_and_custom_key
+ ENV['DATABASE_URL_PRODUCTION'] = "postgres://localhost/foo"
+ config = { "production" => { "adapter" => "not_postgres", "database" => "not_foo", "host" => "localhost" } }
+ actual = resolve(:production, config)
expected = { "adapter"=>"postgresql", "database"=>"foo", "host"=>"localhost" }
assert_equal expected, actual
end
@@ -88,9 +108,9 @@ module ActiveRecord
end
def test_string_connection
- config = { "production" => "postgres://localhost/foo" }
+ config = { "default_env" => "postgres://localhost/foo" }
actual = klass.new(config).resolve
- expected = { "production" =>
+ expected = { "default_env" =>
{ "adapter" => "postgresql",
"database" => "foo",
"host" => "localhost"
@@ -100,9 +120,9 @@ module ActiveRecord
end
def test_url_sub_key
- config = { "production" => { "url" => "postgres://localhost/foo" } }
+ config = { "default_env" => { "url" => "postgres://localhost/foo" } }
actual = klass.new(config).resolve
- expected = { "production" =>
+ expected = { "default_env" =>
{ "adapter" => "postgresql",
"database" => "foo",
"host" => "localhost"