From 1acd9a6464668d4d54ab30d016829f60b70dbbeb Mon Sep 17 00:00:00 2001 From: Robin Dupret Date: Mon, 5 Jun 2017 23:19:00 +0200 Subject: Allow to pass a connection to the `dbconsole` command Since 0a4f6009, it's possible to specify a 3-level database configuration to gather connections by environment. The `dbconsole` command will try to look for a database configuration which points to the current environment but with such flavour, the environment key is flushed out so let's add the ability to specify the connection and pick `primary` by default to be consistent with Active Record. --- railties/test/commands/dbconsole_test.rb | 43 ++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) (limited to 'railties/test') diff --git a/railties/test/commands/dbconsole_test.rb b/railties/test/commands/dbconsole_test.rb index 0f8c5dbb79..94bfe3a652 100644 --- a/railties/test/commands/dbconsole_test.rb +++ b/railties/test/commands/dbconsole_test.rb @@ -200,6 +200,49 @@ class Rails::DBConsoleTest < ActiveSupport::TestCase 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"]) -- cgit v1.2.3 From 48b249927375465a7102acc71c2dfb8d49af8309 Mon Sep 17 00:00:00 2001 From: Robin Dupret Date: Fri, 9 Jun 2017 00:15:41 +0200 Subject: Deprecate environment as an argument for dbconsole and console People should rather rely on the `-e` or `--environment` options to specify in which environment they want to work. This will allow us to specify the connection to pick as a regular argument in the future. --- railties/test/commands/console_test.rb | 24 ++++++++++++++++-------- railties/test/commands/dbconsole_test.rb | 12 ++++++++---- 2 files changed, 24 insertions(+), 12 deletions(-) (limited to 'railties/test') diff --git a/railties/test/commands/console_test.rb b/railties/test/commands/console_test.rb index 4fc082e4ca..08c560146c 100644 --- a/railties/test/commands/console_test.rb +++ b/railties/test/commands/console_test.rb @@ -47,7 +47,7 @@ class Rails::ConsoleTest < ActiveSupport::TestCase end def test_console_with_environment - start ["-e production"] + start ["-e", "production"] assert_match(/\sproduction\s/, output) end @@ -88,18 +88,24 @@ class Rails::ConsoleTest < ActiveSupport::TestCase 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 +117,9 @@ class Rails::ConsoleTest < ActiveSupport::TestCase 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 diff --git a/railties/test/commands/dbconsole_test.rb b/railties/test/commands/dbconsole_test.rb index 94bfe3a652..e6a67c2c49 100644 --- a/railties/test/commands/dbconsole_test.rb +++ b/railties/test/commands/dbconsole_test.rb @@ -98,14 +98,18 @@ class Rails::DBConsoleTest < ActiveSupport::TestCase end def test_rails_env_is_development_when_argument_is_dev - stub_available_environments([ "development", "test" ]) do - assert_match("development", parse_arguments([ "dev" ])[:environment]) + assert_deprecated do + stub_available_environments([ "development", "test" ]) do + assert_match("development", parse_arguments([ "dev" ])[:environment]) + end 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 -- cgit v1.2.3 From 3777701f1380f3814bd5313b225586dec64d4104 Mon Sep 17 00:00:00 2001 From: Robin Dupret Date: Sun, 16 Jul 2017 14:54:04 +0200 Subject: Properly expand the environment's name Running the `console` and `dbconsole` commands with a regular argument as the environment's name automatically expand it to match an existing environment (e.g. dev for development). This feature wasn't available using the `--environment` (a.k.a `-e`) option. --- railties/test/commands/console_test.rb | 5 +++++ railties/test/commands/dbconsole_test.rb | 6 ++++++ 2 files changed, 11 insertions(+) (limited to 'railties/test') diff --git a/railties/test/commands/console_test.rb b/railties/test/commands/console_test.rb index 08c560146c..a7169e16fb 100644 --- a/railties/test/commands/console_test.rb +++ b/railties/test/commands/console_test.rb @@ -82,6 +82,11 @@ class Rails::ConsoleTest < ActiveSupport::TestCase 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) diff --git a/railties/test/commands/dbconsole_test.rb b/railties/test/commands/dbconsole_test.rb index e6a67c2c49..4f55eb9aa6 100644 --- a/railties/test/commands/dbconsole_test.rb +++ b/railties/test/commands/dbconsole_test.rb @@ -105,6 +105,12 @@ class Rails::DBConsoleTest < ActiveSupport::TestCase end end + def test_rails_env_is_development_when_environment_option_is_dev + stub_available_environments([ "development", "test" ]) do + 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 assert_deprecated do stub_available_environments([ "dev" ]) do -- cgit v1.2.3