aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/cases/connection_adapters
diff options
context:
space:
mode:
authorMatthew Draper <matthew@trebex.net>2014-04-03 03:33:40 +1030
committerMatthew Draper <matthew@trebex.net>2014-04-03 06:16:03 +1030
commit88e60a48ec7f3447225e9f5e47a251d6e1af78e2 (patch)
tree13f307aada094557215c9a00fbf237b809167f0e /activerecord/test/cases/connection_adapters
parentc82483a10abd30310f3360f6ebb6a4dddafdb2ba (diff)
downloadrails-88e60a48ec7f3447225e9f5e47a251d6e1af78e2.tar.gz
rails-88e60a48ec7f3447225e9f5e47a251d6e1af78e2.tar.bz2
rails-88e60a48ec7f3447225e9f5e47a251d6e1af78e2.zip
Avoid a spurious deprecation warning for database URLs
This is all about the case where we have a `DATABASE_URL`, and we have a `database.yml` present, but the latter doesn't contain the key we're looking for. If the key is a symbol, we'll always connect to `DATABASE_URL`, per the new behaviour in 283a2edec2f8ccdf90fb58025608f02a63948fa0. If the key is a string, on the other hand, it should always be a URL: the ability to specify a name not present in `database.yml` is new in this version of Rails, and that ability does not stretch to the deprecated use of a string in place of a symbol. Uncovered by @guilleiguaran while investigating #14495 -- this actually may be related to the original report, but we don't have enough info to confirm.
Diffstat (limited to 'activerecord/test/cases/connection_adapters')
-rw-r--r--activerecord/test/cases/connection_adapters/connection_handler_test.rb48
1 files changed, 48 insertions, 0 deletions
diff --git a/activerecord/test/cases/connection_adapters/connection_handler_test.rb b/activerecord/test/cases/connection_adapters/connection_handler_test.rb
index 2992ceb211..e097449029 100644
--- a/activerecord/test/cases/connection_adapters/connection_handler_test.rb
+++ b/activerecord/test/cases/connection_adapters/connection_handler_test.rb
@@ -17,6 +17,54 @@ module ActiveRecord
ENV["DATABASE_URL"] = @previous_database_url
end
+ def resolve(spec, config)
+ ConnectionSpecification::Resolver.new(klass.new(config).resolve).resolve(spec)
+ end
+
+ def spec(spec, config)
+ ConnectionSpecification::Resolver.new(klass.new(config).resolve).spec(spec)
+ end
+
+ def test_resolver_with_database_uri_and_known_key
+ ENV['DATABASE_URL'] = "postgres://localhost/foo"
+ config = { "production" => { "adapter" => "not_postgres", "database" => "not_foo" } }
+ actual = resolve(:production, config)
+ expected = { "adapter"=>"postgresql", "database"=>"foo", "host"=>"localhost" }
+ assert_equal expected, actual
+ end
+
+ def test_resolver_with_database_uri_and_known_string_key
+ ENV['DATABASE_URL'] = "postgres://localhost/foo"
+ config = { "production" => { "adapter" => "not_postgres", "database" => "not_foo" } }
+ actual = assert_deprecated { resolve("production", config) }
+ expected = { "adapter"=>"postgresql", "database"=>"foo", "host"=>"localhost" }
+ assert_equal expected, actual
+ end
+
+ def test_resolver_with_database_uri_and_unknown_symbol_key
+ ENV['DATABASE_URL'] = "postgres://localhost/foo"
+ config = { "not_production" => { "adapter" => "not_postgres", "database" => "not_foo" } }
+ actual = resolve(:production, config)
+ expected = { "adapter"=>"postgresql", "database"=>"foo", "host"=>"localhost" }
+ assert_equal expected, actual
+ end
+
+ def test_resolver_with_database_uri_and_unknown_string_key
+ ENV['DATABASE_URL'] = "postgres://localhost/foo"
+ config = { "not_production" => { "adapter" => "not_postgres", "database" => "not_foo" } }
+ assert_raises AdapterNotSpecified do
+ spec("production", config)
+ end
+ end
+
+ def test_resolver_with_database_uri_and_supplied_url
+ ENV['DATABASE_URL'] = "not-postgres://not-localhost/not_foo"
+ config = { "production" => { "adapter" => "also_not_postgres", "database" => "also_not_foo" } }
+ actual = resolve("postgres://localhost/foo", config)
+ expected = { "adapter"=>"postgresql", "database"=>"foo", "host"=>"localhost" }
+ assert_equal expected, actual
+ end
+
def test_jdbc_url
config = { "production" => { "url" => "jdbc:postgres://localhost/foo" } }
actual = klass.new(config).resolve