Skip to content

Commit 720e2da

Browse files
authored
Merge pull request rails#43921 from eileencodes/fix-dbconsole-when-primary-doesnt-exist
Fix dbconsole for 3-tier config.
2 parents ddc63b7 + b96d01b commit 720e2da

File tree

2 files changed

+11
-8
lines changed

2 files changed

+11
-8
lines changed

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

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -97,15 +97,18 @@ def start
9797
def db_config
9898
return @db_config if defined?(@db_config)
9999

100-
# We need to check whether the user passed the database the
101-
# first time around to show a consistent error message to people
102-
# relying on 2-level database configuration.
103-
104-
@db_config = configurations.configs_for(env_name: environment, name: database, include_hidden: true)
100+
# If the user provided a database, use that. Otherwise find
101+
# the first config in the database.yml
102+
if database
103+
@db_config = configurations.configs_for(env_name: environment, name: database, include_hidden: true)
104+
else
105+
@db_config = configurations.find_db_config(environment)
106+
end
105107

106108
unless @db_config
109+
missing_db = database ? "'#{database}' database is not" : "No databases are"
107110
raise ActiveRecord::AdapterNotSpecified,
108-
"'#{database}' database is not configured for '#{environment}'. Available configuration: #{configurations.inspect}"
111+
"#{missing_db} configured for '#{environment}'. Available configuration: #{configurations.inspect}"
109112
end
110113

111114
@db_config
@@ -116,7 +119,7 @@ def environment
116119
end
117120

118121
def database
119-
@options.fetch(:database, "primary")
122+
@options[:database]
120123
end
121124

122125
private

railties/test/commands/dbconsole_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ def test_specifying_a_missing_environment
271271
Rails::Command.invoke(:dbconsole)
272272
end
273273

274-
assert_includes e.message, "'primary' database is not configured for 'test'."
274+
assert_includes e.message, "No databases are configured for 'test'."
275275
end
276276
end
277277

0 commit comments

Comments
 (0)