From 971d5107cd4cd08c22a85d34546f4ba03ed5c925 Mon Sep 17 00:00:00 2001 From: Huiming Teo Date: Sun, 15 Dec 2013 14:07:34 +0800 Subject: fixed rails dbconsole to support ENV['DATABASE_URL']. --- railties/CHANGELOG.md | 4 ++ railties/lib/rails/commands/dbconsole.rb | 13 ++--- railties/test/commands/dbconsole_test.rb | 94 +++++++++++++++++++++++--------- 3 files changed, 76 insertions(+), 35 deletions(-) (limited to 'railties') 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 -- cgit v1.2.3