Permalink
Browse files

Merge pull request #29358 from robin850/dbconsole-connection

Allow to pass a connection to the `dbconsole` command
  • Loading branch information...
kaspth committed Jul 16, 2017
2 parents 519467b + 3777701 commit c74820d516bdb731a3df3c94192a542abb6c393b
View
@@ -1,3 +1,29 @@
* Properly expand shortcuts for environment's name running the `console`
and `dbconsole` commands.
*Robin Dupret*
* Passing the environment's name as a regular argument to the
`rails dbconsole` and `rails console` commands is deprecated.
The `-e` option should be used instead.
Previously:
$ bin/rails dbconsole production
Now:
$ bin/rails dbconsole -e production
*Robin Dupret*, *Kasper Timm Hansen*
* Allow to pass a custom connection name to the `rails dbconsole`
command when using a 3-level database configuration.
$ bin/rails dbconsole -c replica
*Robin Dupret*, *Jeremy Daer*
* Skip unused components when running `bin/rails app:update`.
If the initial app generation skipped Action Cable, Active Record etc.,
@@ -7,13 +7,24 @@ module EnvironmentArgument #:nodoc:
included do
argument :environment, optional: true, banner: "environment"
class_option :environment, aliases: "-e", type: :string,
desc: "Specifies the environment to run this console under (test/development/production)."
end
private
def extract_environment_option_from_argument
if environment
self.options = options.merge(environment: acceptable_environment(environment))
elsif !options[:environment]
ActiveSupport::Deprecation.warn "Passing the environment's name as a " \
"regular argument is deprecated and " \
"will be removed in the next Rails " \
"version. Please, use the -e option " \
"instead."
elsif options[:environment]
self.options = options.merge(environment: acceptable_environment(options[:environment]))
else
self.options = options.merge(environment: Rails::Command.environment)
end
end
@@ -70,9 +70,6 @@ class ConsoleCommand < Base # :nodoc:
class_option :sandbox, aliases: "-s", type: :boolean, default: false,
desc: "Rollback database modifications on exit."
class_option :environment, aliases: "-e", type: :string,
desc: "Specifies the environment to run this console under (test/development/production)."
def initialize(args = [], local_options = {}, config = {})
console_options = []
@@ -87,10 +87,15 @@ def start
def config
@config ||= begin
if configurations[environment].blank?
# We need to check whether the user passed the connection the
# first time around to show a consistent error message to people
# relying on 2-level database configuration.
if @options["connection"] && configurations[connection].blank?
raise ActiveRecord::AdapterNotSpecified, "'#{connection}' connection is not configured. Available configuration: #{configurations.inspect}"
elsif configurations[environment].blank? && configurations[connection].blank?
raise ActiveRecord::AdapterNotSpecified, "'#{environment}' database is not configured. Available configuration: #{configurations.inspect}"
else
configurations[environment]
configurations[environment].presence || configurations[connection]
end
end
end
@@ -99,6 +104,10 @@ def environment
Rails.respond_to?(:env) ? Rails.env : Rails::Command.environment
end
def connection
@options.fetch(:connection, "primary")
end
private
def configurations # :doc:
require APP_PATH
@@ -142,8 +151,8 @@ class DbconsoleCommand < Base # :nodoc:
class_option :header, type: :boolean
class_option :environment, aliases: "-e", type: :string,
desc: "Specifies the environment to run this console under (test/development/production)."
class_option :connection, aliases: "-c", type: :string,
desc: "Specifies the connection to use."
def perform
extract_environment_option_from_argument
@@ -47,7 +47,7 @@ def test_start_with_sandbox
end
def test_console_with_environment
start ["-e production"]
start ["-e", "production"]
assert_match(/\sproduction\s/, output)
end
@@ -82,24 +82,35 @@ def test_e_option
assert_match(/\sspecial-production\s/, output)
end
def test_e_option_is_properly_expanded
start ["-e", "prod"]
assert_match(/\sproduction\s/, output)
end
def test_environment_option
start ["--environment=special-production"]
assert_match(/\sspecial-production\s/, output)
end
def test_rails_env_is_production_when_first_argument_is_p
start ["p"]
assert_match(/\sproduction\s/, output)
assert_deprecated do
start ["p"]
assert_match(/\sproduction\s/, output)
end
end
def test_rails_env_is_test_when_first_argument_is_t
start ["t"]
assert_match(/\stest\s/, output)
assert_deprecated do
start ["t"]
assert_match(/\stest\s/, output)
end
end
def test_rails_env_is_development_when_argument_is_d
start ["d"]
assert_match(/\sdevelopment\s/, output)
assert_deprecated do
start ["d"]
assert_match(/\sdevelopment\s/, output)
end
end
def test_rails_env_is_dev_when_argument_is_dev_and_dev_env_is_present
@@ -111,7 +122,9 @@ def test_rails_env_is_dev_when_argument_is_dev_and_dev_env_is_present
end
end
assert_match("dev", parse_arguments(["dev"])[:environment])
assert_deprecated do
assert_match("dev", parse_arguments(["dev"])[:environment])
end
ensure
Rails::Command::ConsoleCommand.class_eval do
undef_method :available_environments
@@ -98,14 +98,24 @@ def test_env
end
def test_rails_env_is_development_when_argument_is_dev
assert_deprecated do
stub_available_environments([ "development", "test" ]) do
assert_match("development", parse_arguments([ "dev" ])[:environment])
end
end
end
def test_rails_env_is_development_when_environment_option_is_dev
stub_available_environments([ "development", "test" ]) do
assert_match("development", parse_arguments([ "dev" ])[:environment])
assert_match("development", parse_arguments([ "-e", "dev" ])[:environment])
end
end
def test_rails_env_is_dev_when_argument_is_dev_and_dev_env_is_present
stub_available_environments([ "dev" ]) do
assert_match("dev", parse_arguments([ "dev" ])[:environment])
assert_deprecated do
stub_available_environments([ "dev" ]) do
assert_match("dev", parse_arguments([ "dev" ])[:environment])
end
end
end
@@ -200,6 +210,49 @@ def test_unknown_command_line_client
assert_match(/Unknown command-line client for db/, output)
end
def test_primary_is_automatically_picked_with_3_level_configuration
sample_config = {
"test" => {
"primary" => {
"adapter" => "postgresql"
}
}
}
app_db_config(sample_config) do
assert_equal "postgresql", Rails::DBConsole.new.config["adapter"]
end
end
def test_specifying_a_custom_connection_and_environment
stub_available_environments(["development"]) do
dbconsole = parse_arguments(["-c", "custom", "-e", "development"])
assert_equal "development", dbconsole[:environment]
assert_equal "custom", dbconsole.connection
end
end
def test_specifying_a_missing_connection
app_db_config({}) do
e = assert_raises(ActiveRecord::AdapterNotSpecified) do
Rails::Command.invoke(:dbconsole, ["-c", "i_do_not_exist"])
end
assert_includes e.message, "'i_do_not_exist' connection is not configured."
end
end
def test_specifying_a_missing_environment
app_db_config({}) do
e = assert_raises(ActiveRecord::AdapterNotSpecified) do
Rails::Command.invoke(:dbconsole)
end
assert_includes e.message, "'test' database is not configured."
end
end
def test_print_help_short
stdout = capture(:stdout) do
Rails::Command.invoke(:dbconsole, ["-h"])

0 comments on commit c74820d

Please sign in to comment.