Skip to content

Commit ef5ebd9

Browse files
authored
Merge pull request rails#35703 from y-yagi/add_database_option_to_dbconsole_command
Rename `connection` option to `database` in `dbconsole` command
2 parents 0214d40 + 29b16d3 commit ef5ebd9

File tree

3 files changed

+42
-13
lines changed

3 files changed

+42
-13
lines changed

railties/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
* The `connection` option of `rails dbconsole` command is deprecated in
2+
favor of `database` option.
3+
4+
*Yuji Yaginuma*
5+
16
* Replace `chromedriver-helper` gem with `webdrivers` in default Gemfile.
27
`chromedriver-helper` is deprecated as of March 31, 2019 and won't
38
receive any further updates.

railties/lib/rails/commands/dbconsole/dbconsole_command.rb

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# frozen_string_literal: true
22

3+
require "active_support/deprecation"
4+
require "active_support/core_ext/string/filters"
35
require "rails/command/environment_argument"
46

57
module Rails
@@ -89,15 +91,15 @@ def start
8991

9092
def config
9193
@config ||= begin
92-
# We need to check whether the user passed the connection the
94+
# We need to check whether the user passed the database the
9395
# first time around to show a consistent error message to people
9496
# relying on 2-level database configuration.
95-
if @options["connection"] && configurations[connection].blank?
96-
raise ActiveRecord::AdapterNotSpecified, "'#{connection}' connection is not configured. Available configuration: #{configurations.inspect}"
97-
elsif configurations[environment].blank? && configurations[connection].blank?
97+
if @options["database"] && configurations[database].blank?
98+
raise ActiveRecord::AdapterNotSpecified, "'#{database}' database is not configured. Available configuration: #{configurations.inspect}"
99+
elsif configurations[environment].blank? && configurations[database].blank?
98100
raise ActiveRecord::AdapterNotSpecified, "'#{environment}' database is not configured. Available configuration: #{configurations.inspect}"
99101
else
100-
configurations[connection] || configurations[environment].presence
102+
configurations[database] || configurations[environment].presence
101103
end
102104
end
103105
end
@@ -106,8 +108,8 @@ def environment
106108
Rails.respond_to?(:env) ? Rails.env : Rails::Command.environment
107109
end
108110

109-
def connection
110-
@options.fetch(:connection, "primary")
111+
def database
112+
@options.fetch(:database, "primary")
111113
end
112114

113115
private
@@ -156,12 +158,22 @@ class DbconsoleCommand < Base # :nodoc:
156158
class_option :connection, aliases: "-c", type: :string,
157159
desc: "Specifies the connection to use."
158160

161+
class_option :database, aliases: "--db", type: :string,
162+
desc: "Specifies the database to use."
163+
159164
def perform
160165
extract_environment_option_from_argument
161166

162167
# RAILS_ENV needs to be set before config/application is required.
163168
ENV["RAILS_ENV"] = options[:environment]
164169

170+
if options["connection"]
171+
ActiveSupport::Deprecation.warn(<<-MSG.squish)
172+
`connection` option is deprecated and will be removed in Rails 6.1. Please use `database` option instead.
173+
MSG
174+
options["database"] = options["connection"]
175+
end
176+
165177
require_application_and_environment!
166178
Rails::DBConsole.start(options)
167179
end

railties/test/commands/dbconsole_test.rb

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -216,22 +216,22 @@ def test_primary_is_automatically_picked_with_3_level_configuration
216216
end
217217
end
218218

219-
def test_specifying_a_custom_connection_and_environment
219+
def test_specifying_a_custom_database_and_environment
220220
stub_available_environments(["development"]) do
221-
dbconsole = parse_arguments(["-c", "custom", "-e", "development"])
221+
dbconsole = parse_arguments(["--db", "custom", "-e", "development"])
222222

223223
assert_equal "development", dbconsole[:environment]
224-
assert_equal "custom", dbconsole.connection
224+
assert_equal "custom", dbconsole.database
225225
end
226226
end
227227

228-
def test_specifying_a_missing_connection
228+
def test_specifying_a_missing_database
229229
app_db_config({}) do
230230
e = assert_raises(ActiveRecord::AdapterNotSpecified) do
231-
Rails::Command.invoke(:dbconsole, ["-c", "i_do_not_exist"])
231+
Rails::Command.invoke(:dbconsole, ["--db", "i_do_not_exist"])
232232
end
233233

234-
assert_includes e.message, "'i_do_not_exist' connection is not configured."
234+
assert_includes e.message, "'i_do_not_exist' database is not configured."
235235
end
236236
end
237237

@@ -245,6 +245,18 @@ def test_specifying_a_missing_environment
245245
end
246246
end
247247

248+
def test_connection_options_is_deprecate
249+
command = Rails::Command::DbconsoleCommand.new([], ["-c", "custom"])
250+
Rails::DBConsole.stub(:start, nil) do
251+
assert_deprecated("`connection` option is deprecated") do
252+
command.perform
253+
end
254+
end
255+
256+
assert_equal "custom", command.options["connection"]
257+
assert_equal "custom", command.options["database"]
258+
end
259+
248260
def test_print_help_short
249261
stdout = capture(:stdout) do
250262
Rails::Command.invoke(:dbconsole, ["-h"])

0 commit comments

Comments
 (0)