diff options
author | Yves Senn <yves.senn@gmail.com> | 2013-12-16 02:37:01 -0800 |
---|---|---|
committer | Yves Senn <yves.senn@gmail.com> | 2013-12-16 02:37:01 -0800 |
commit | bd66532ae439341f6978e672687cd4bfb0f0e163 (patch) | |
tree | 3b9c754782efb3c5f8c35016544973147b484655 /railties | |
parent | dfbea4db546cc240b7173109017e43e34b2a1a09 (diff) | |
parent | 971d5107cd4cd08c22a85d34546f4ba03ed5c925 (diff) | |
download | rails-bd66532ae439341f6978e672687cd4bfb0f0e163.tar.gz rails-bd66532ae439341f6978e672687cd4bfb0f0e163.tar.bz2 rails-bd66532ae439341f6978e672687cd4bfb0f0e163.zip |
Merge pull request #13328 from teohm/dbconsole_support_database_url
Fixed rails dbconsole to support DATABASE_URL
Diffstat (limited to 'railties')
-rw-r--r-- | railties/CHANGELOG.md | 4 | ||||
-rw-r--r-- | railties/lib/rails/commands/dbconsole.rb | 13 | ||||
-rw-r--r-- | railties/test/commands/dbconsole_test.rb | 94 |
3 files changed, 76 insertions, 35 deletions
diff --git a/railties/CHANGELOG.md b/railties/CHANGELOG.md index 3813ef7909..cf8d2c2c88 100644 --- a/railties/CHANGELOG.md +++ b/railties/CHANGELOG.md @@ -1,3 +1,7 @@ +* Add `ENV['DATABASE_URL']` support in `rails dbconsole`. Fixes #13320. + + *Huiming Teo* + * Add `Application#message_verifier` method to return a message verifier. This verifier can be used to generate and verify signed messages in the application. diff --git a/railties/lib/rails/commands/dbconsole.rb b/railties/lib/rails/commands/dbconsole.rb index 3e4cc787c4..847447fdad 100644 --- a/railties/lib/rails/commands/dbconsole.rb +++ b/railties/lib/rails/commands/dbconsole.rb @@ -81,14 +81,11 @@ module Rails def config @config ||= begin - cfg = begin - YAML.load(ERB.new(IO.read("config/database.yml")).result) - rescue SyntaxError, StandardError - require APP_PATH - Rails.application.config.database_configuration - end - - cfg[environment] || abort("No database is configured for the environment '#{environment}'") + require APP_PATH + ActiveRecord::ConnectionAdapters::ConnectionSpecification::Resolver.new( + ENV['DATABASE_URL'], + (Rails.application.config.database_configuration || {}) + ).spec.config.stringify_keys end end diff --git a/railties/test/commands/dbconsole_test.rb b/railties/test/commands/dbconsole_test.rb index edb92b3aa2..a6cd6ec61d 100644 --- a/railties/test/commands/dbconsole_test.rb +++ b/railties/test/commands/dbconsole_test.rb @@ -2,29 +2,72 @@ require 'abstract_unit' require 'rails/commands/dbconsole' class Rails::DBConsoleTest < ActiveSupport::TestCase - def teardown - %w[PGUSER PGHOST PGPORT PGPASSWORD].each{|key| ENV.delete(key)} - end - def test_config - Rails::DBConsole.const_set(:APP_PATH, "erb") - - app_config({}) - capture_abort { Rails::DBConsole.new.config } - assert aborted - assert_match(/No database is configured for the environment '\w+'/, output) - app_config(test: "with_init") - assert_equal Rails::DBConsole.new.config, "with_init" - - app_db_file("test:\n without_init") - assert_equal Rails::DBConsole.new.config, "without_init" - - app_db_file("test:\n <%= Rails.something_app_specific %>") - assert_equal Rails::DBConsole.new.config, "with_init" + def setup + Rails::DBConsole.const_set('APP_PATH', 'rails/all') + end - app_db_file("test:\n\ninvalid") - assert_equal Rails::DBConsole.new.config, "with_init" + def teardown + Rails::DBConsole.send(:remove_const, 'APP_PATH') + %w[PGUSER PGHOST PGPORT PGPASSWORD DATABASE_URL].each{|key| ENV.delete(key)} + end + + def test_config_with_db_config_only + config_sample = { + "test"=> { + "adapter"=> "sqlite3", + "host"=> "localhost", + "port"=> "9000", + "database"=> "foo_test", + "user"=> "foo", + "password"=> "bar", + "pool"=> "5", + "timeout"=> "3000" + } + } + app_db_config(config_sample) + assert_equal Rails::DBConsole.new.config, config_sample["test"] + end + + def test_config_with_no_db_config + app_db_config(nil) + assert_raise(ActiveRecord::AdapterNotSpecified) { + Rails::DBConsole.new.config + } + end + + def test_config_with_database_url_only + ENV['DATABASE_URL'] = 'sqlite3://foo:bar@localhost:9000/foo_test?pool=5&timeout=3000' + app_db_config(nil) + assert_equal Rails::DBConsole.new.config.sort, { + "adapter"=> "sqlite3", + "host"=> "localhost", + "port"=> 9000, + "database"=> "foo_test", + "username"=> "foo", + "password"=> "bar", + "pool"=> "5", + "timeout"=> "3000" + }.sort + end + + def test_config_choose_database_url_if_exists + ENV['DATABASE_URL'] = 'sqlite3://foo:bar@dburl:9000/foo_test?pool=5&timeout=3000' + sample_config = { + "test" => { + "adapter"=> "sqlite3", + "host"=> "localhost", + "port"=> 9000, + "database"=> "foo_test", + "username"=> "foo", + "password"=> "bar", + "pool"=> "5", + "timeout"=> "3000" + } + } + app_db_config(sample_config) + assert_equal Rails::DBConsole.new.config["host"], "dburl" end def test_env @@ -177,6 +220,10 @@ class Rails::DBConsoleTest < ActiveSupport::TestCase private + def app_db_config(results) + Rails.application.config.stubs(:database_configuration).returns(results) + end + def dbconsole @dbconsole ||= Rails::DBConsole.new(nil) end @@ -197,11 +244,4 @@ class Rails::DBConsoleTest < ActiveSupport::TestCase end end - def app_db_file(result) - IO.stubs(:read).with("config/database.yml").returns(result) - end - - def app_config(result) - Rails.application.config.stubs(:database_configuration).returns(result.stringify_keys) - end end |