1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Use DatabaseConfig objects in dbconsole

We have these nice objects for collecting database configurations, so we
should use them everywhere instead of the hashes.

Also call `db_config.database` and `db_config.adapter` where necessary.

John Crepezzi <seejohnrun@github.com>
This commit is contained in:
eileencodes 2019-10-11 16:01:55 -04:00
parent f54a8d0d43
commit a843301a58
2 changed files with 30 additions and 21 deletions

View file

@ -17,7 +17,7 @@ module Rails
def start
ENV["RAILS_ENV"] ||= @options[:environment] || environment
case config[:adapter]
case db_config.adapter
when /^(jdbc)?mysql/
args = {
host: "--host",
@ -38,7 +38,7 @@ module Rails
args << "-p"
end
args << config[:database]
args << db_config.database
find_cmd_and_exec(["mysql", "mysql5"], *args)
@ -47,14 +47,14 @@ module Rails
ENV["PGHOST"] = config[:host] if config[:host]
ENV["PGPORT"] = config[:port].to_s if config[:port]
ENV["PGPASSWORD"] = config[:password].to_s if config[:password] && @options[:include_password]
find_cmd_and_exec("psql", config[:database])
find_cmd_and_exec("psql", db_config.database)
when "sqlite3"
args = []
args << "-#{@options[:mode]}" if @options[:mode]
args << "-header" if @options[:header]
args << File.expand_path(config[:database], Rails.respond_to?(:root) ? Rails.root : nil)
args << File.expand_path(db_config.database, Rails.respond_to?(:root) ? Rails.root : nil)
find_cmd_and_exec("sqlite3", *args)
@ -64,7 +64,7 @@ module Rails
if config[:username]
logon = config[:username].dup
logon << "/#{config[:password]}" if config[:password] && @options[:include_password]
logon << "@#{config[:database]}" if config[:database]
logon << "@#{db_config.database}" if db_config.database
end
find_cmd_and_exec("sqlplus", logon)
@ -72,7 +72,7 @@ module Rails
when "sqlserver"
args = []
args += ["-D", "#{config[:database]}"] if config[:database]
args += ["-D", "#{db_config.database}"] if db_config.database
args += ["-U", "#{config[:username]}"] if config[:username]
args += ["-P", "#{config[:password]}"] if config[:password]
@ -85,23 +85,29 @@ module Rails
find_cmd_and_exec("sqsh", *args)
else
abort "Unknown command-line client for #{config[:database]}."
abort "Unknown command-line client for #{db_config.database}."
end
end
def config
@config ||= begin
db_config.configuration_hash
end
def db_config
return @db_config if @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.
if @options[:database] && configurations[database].blank?
raise ActiveRecord::AdapterNotSpecified, "'#{database}' database is not configured. Available configuration: #{configurations.inspect}"
elsif configurations[environment].blank? && configurations[database].blank?
raise ActiveRecord::AdapterNotSpecified, "'#{environment}' database is not configured. Available configuration: #{configurations.inspect}"
else
(configurations[database] || configurations[environment].presence).symbolize_keys
end
@db_config = configurations.configs_for(env_name: environment, spec_name: database)
unless @db_config
raise ActiveRecord::AdapterNotSpecified,
"'#{database}' database is not configured for '#{environment}'. Available configuration: #{configurations.inspect}"
end
@db_config
end
def environment

View file

@ -4,6 +4,7 @@ require "abstract_unit"
require "minitest/mock"
require "rails/command"
require "rails/commands/dbconsole/dbconsole_command"
require "active_record/database_configurations"
class Rails::DBConsoleTest < ActiveSupport::TestCase
def setup
@ -231,7 +232,7 @@ class Rails::DBConsoleTest < ActiveSupport::TestCase
Rails::Command.invoke(:dbconsole, ["--db", "i_do_not_exist"])
end
assert_includes e.message, "'i_do_not_exist' database is not configured."
assert_includes e.message, "'i_do_not_exist' database is not configured for 'test'."
end
end
@ -241,7 +242,7 @@ class Rails::DBConsoleTest < ActiveSupport::TestCase
Rails::Command.invoke(:dbconsole)
end
assert_includes e.message, "'test' database is not configured."
assert_includes e.message, "'primary' database is not configured for 'test'."
end
end
@ -294,8 +295,10 @@ class Rails::DBConsoleTest < ActiveSupport::TestCase
attr_reader :dbconsole
def start(config = {}, argv = [])
hash_config = ActiveRecord::DatabaseConfigurations::HashConfig.new("test", "primary", config)
@dbconsole = make_dbconsole.new(parse_arguments(argv))
@dbconsole.stub(:config, config) do
@dbconsole.stub(:db_config, hash_config) do
capture_abort { @dbconsole.start }
end
end