Skip to content

Commit 2561732

Browse files
committed
Some dbconsole tweaks. [#102 state:resolved]
1 parent b510d8b commit 2561732

File tree

1 file changed

+26
-20
lines changed

1 file changed

+26
-20
lines changed

railties/lib/commands/dbconsole.rb

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,55 @@
1+
require 'yaml'
12
require 'optparse'
3+
24
OptionParser.new do |opt|
35
opt.banner = "Usage: dbconsole [environment]"
46
opt.parse!(ARGV)
57
abort opt.to_s unless (0..1).include?(ARGV.size)
68
end
79

8-
env = ENV['RAILS_ENV'] = ARGV.first || ENV['RAILS_ENV'] || 'development'
10+
env = ARGV.first || ENV['RAILS_ENV'] || 'development'
11+
unless config = YAML.load_file(RAILS_ROOT + "/config/database.yml")[env]
12+
abort "No database is configured for the environment '#{env}'"
13+
end
14+
915

1016
def find_cmd(*commands)
11-
dirs_on_path = ENV['PATH'].split(File::PATH_SEPARATOR)
17+
dirs_on_path = ENV['PATH'].to_s.split(File::PATH_SEPARATOR)
1218
commands += commands.map{|cmd| "#{cmd}.exe"} if RUBY_PLATFORM =~ /win32/
1319
commands.detect do |cmd|
1420
dirs_on_path.detect do |path|
1521
File.executable? File.join(path, cmd)
1622
end
17-
end || abort("couldn't find matching executable: #{commands.join(', ')}")
23+
end || abort("Couldn't find database client: #{commands.join(', ')}. Check your $PATH and try again.")
1824
end
1925

26+
case config["adapter"]
27+
when "mysql"
28+
args = {
29+
'host' => '--host',
30+
'port' => '--port',
31+
'socket' => '--socket',
32+
'username' => '--user',
33+
'password' => '--password',
34+
'encoding' => '--default-character-set'
35+
}.map { |opt, arg| "#{arg}=#{config[opt]}" if config[opt] }.compact
2036

21-
require 'yaml'
22-
config = YAML::load(File.read(RAILS_ROOT + "/config/database.yml"))[env]
37+
args << config['database']
2338

24-
unless config
25-
abort "No database is configured for the environment '#{env}'"
26-
end
39+
exec(find_cmd('mysql5', 'mysql'), *args)
2740

28-
case config["adapter"]
29-
when "mysql"
30-
exec(find_cmd(*%w(mysql5 mysql)),
31-
*({ 'host' => '--host',
32-
'port' => '--port',
33-
'socket' => '--socket',
34-
'username' => '--user',
35-
'password' => '--password',
36-
'encoding' => '--default-character-set'
37-
}.map { |opt, arg| "#{arg}=#{config[opt]}" if config[opt] }.compact +
38-
[config['database']]))
3941
when "postgresql"
4042
ENV['PGHOST'] = config["host"] if config["host"]
4143
ENV['PGPORT'] = config["port"].to_s if config["port"]
4244
ENV['PGPASSWORD'] = config["password"].to_s if config["password"]
4345
exec(find_cmd('psql'), '-U', config["username"], config["database"])
46+
4447
when "sqlite"
4548
exec(find_cmd('sqlite'), config["database"])
49+
4650
when "sqlite3"
4751
exec(find_cmd('sqlite3'), config["database"])
48-
else abort "not supported for this database type"
52+
53+
else
54+
abort "Unknown command-line client for #{config['database']}. Submit a Rails patch to add support!"
4955
end

0 commit comments

Comments
 (0)