Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #13328 from teohm/dbconsole_support_database_url

Fixed rails dbconsole to support DATABASE_URL
  • Loading branch information...
commit bd66532ae439341f6978e672687cd4bfb0f0e163 2 parents dfbea4d + 971d510
Yves Senn senny authored
4 railties/CHANGELOG.md
View
@@ -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.
13 railties/lib/rails/commands/dbconsole.rb
View
@@ -81,14 +81,11 @@ def start
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
94 railties/test/commands/dbconsole_test.rb
View
@@ -2,29 +2,72 @@
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 @@ def test_print_help_long
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 @@ def capture_abort
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
Please sign in to comment.
Something went wrong with that request. Please try again.