Skip to content

Commit 1acd9a6

Browse files
committed
Allow to pass a connection to the dbconsole command
Since 0a4f600, 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.
1 parent 16f2b20 commit 1acd9a6

File tree

3 files changed

+64
-2
lines changed

3 files changed

+64
-2
lines changed

railties/CHANGELOG.md

+7
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
* Allow to pass a custom connection name to the `rails dbconsole`
2+
command when using a 3-level database configuration.
3+
4+
$ bin/rails dbconsole -c replica
5+
6+
*Robin Dupret*, *Jeremy Daer*
7+
18
* Skip unused components when running `bin/rails app:update`.
29

310
If the initial app generation skipped Action Cable, Active Record etc.,

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

+14-2
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,15 @@ def start
8787

8888
def config
8989
@config ||= begin
90-
if configurations[environment].blank?
90+
# We need to check whether the user passed the connection the
91+
# first time around to show a consistent error message to people
92+
# relying on 2-level database configuration.
93+
if @options["connection"] && configurations[connection].blank?
94+
raise ActiveRecord::AdapterNotSpecified, "'#{connection}' connection is not configured. Available configuration: #{configurations.inspect}"
95+
elsif configurations[environment].blank? && configurations[connection].blank?
9196
raise ActiveRecord::AdapterNotSpecified, "'#{environment}' database is not configured. Available configuration: #{configurations.inspect}"
9297
else
93-
configurations[environment]
98+
configurations[environment].presence || configurations[connection]
9499
end
95100
end
96101
end
@@ -99,6 +104,10 @@ def environment
99104
Rails.respond_to?(:env) ? Rails.env : Rails::Command.environment
100105
end
101106

107+
def connection
108+
@options.fetch(:connection, "primary")
109+
end
110+
102111
private
103112
def configurations # :doc:
104113
require APP_PATH
@@ -145,6 +154,9 @@ class DbconsoleCommand < Base # :nodoc:
145154
class_option :environment, aliases: "-e", type: :string,
146155
desc: "Specifies the environment to run this console under (test/development/production)."
147156

157+
class_option :connection, aliases: "-c", type: :string,
158+
desc: "Specifies the connection to use."
159+
148160
def perform
149161
extract_environment_option_from_argument
150162

railties/test/commands/dbconsole_test.rb

+43
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,49 @@ def test_unknown_command_line_client
200200
assert_match(/Unknown command-line client for db/, output)
201201
end
202202

203+
def test_primary_is_automatically_picked_with_3_level_configuration
204+
sample_config = {
205+
"test" => {
206+
"primary" => {
207+
"adapter" => "postgresql"
208+
}
209+
}
210+
}
211+
212+
app_db_config(sample_config) do
213+
assert_equal "postgresql", Rails::DBConsole.new.config["adapter"]
214+
end
215+
end
216+
217+
def test_specifying_a_custom_connection_and_environment
218+
stub_available_environments(["development"]) do
219+
dbconsole = parse_arguments(["-c", "custom", "-e", "development"])
220+
221+
assert_equal "development", dbconsole[:environment]
222+
assert_equal "custom", dbconsole.connection
223+
end
224+
end
225+
226+
def test_specifying_a_missing_connection
227+
app_db_config({}) do
228+
e = assert_raises(ActiveRecord::AdapterNotSpecified) do
229+
Rails::Command.invoke(:dbconsole, ["-c", "i_do_not_exist"])
230+
end
231+
232+
assert_includes e.message, "'i_do_not_exist' connection is not configured."
233+
end
234+
end
235+
236+
def test_specifying_a_missing_environment
237+
app_db_config({}) do
238+
e = assert_raises(ActiveRecord::AdapterNotSpecified) do
239+
Rails::Command.invoke(:dbconsole)
240+
end
241+
242+
assert_includes e.message, "'test' database is not configured."
243+
end
244+
end
245+
203246
def test_print_help_short
204247
stdout = capture(:stdout) do
205248
Rails::Command.invoke(:dbconsole, ["-h"])

0 commit comments

Comments
 (0)