Skip to content

Commit

Permalink
Merge pull request #43921 from eileencodes/fix-dbconsole-when-primary…
Browse files Browse the repository at this point in the history
…-doesnt-exist

Fix dbconsole for 3-tier config.
  • Loading branch information
eileencodes committed Dec 17, 2021
1 parent b5fb6f7 commit c45313b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
17 changes: 10 additions & 7 deletions railties/lib/rails/commands/dbconsole/dbconsole_command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,15 +98,18 @@ def config
def db_config
return @db_config if defined?(@db_config)

# We need to check whether the user passed the database the
# first time around to show a consistent error message to people
# relying on 2-level database configuration.

@db_config = configurations.configs_for(env_name: environment, name: database, include_replicas: true)
# If the user provided a database, use that. Otherwise find
# the first config in the database.yml
if database
@db_config = configurations.configs_for(env_name: environment, name: database, include_replicas: true)
else
@db_config = configurations.find_db_config(environment)
end

unless @db_config
missing_db = database ? "'#{database}' database is not" : "No databases are"
raise ActiveRecord::AdapterNotSpecified,
"'#{database}' database is not configured for '#{environment}'. Available configuration: #{configurations.inspect}"
"#{missing_db} configured for '#{environment}'. Available configuration: #{configurations.inspect}"
end

@db_config
Expand All @@ -117,7 +120,7 @@ def environment
end

def database
@options.fetch(:database, "primary")
@options[:database]
end

private
Expand Down
2 changes: 1 addition & 1 deletion railties/test/commands/dbconsole_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ def test_specifying_a_missing_environment
Rails::Command.invoke(:dbconsole)
end

assert_includes e.message, "'primary' database is not configured for 'test'."
assert_includes e.message, "No databases are configured for 'test'."
end
end

Expand Down

0 comments on commit c45313b

Please sign in to comment.