|
| 1 | +require 'yaml' |
1 | 2 | require 'optparse' |
| 3 | + |
2 | 4 | OptionParser.new do |opt| |
3 | 5 | opt.banner = "Usage: dbconsole [environment]" |
4 | 6 | opt.parse!(ARGV) |
5 | 7 | abort opt.to_s unless (0..1).include?(ARGV.size) |
6 | 8 | end |
7 | 9 |
|
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 | + |
9 | 15 |
|
10 | 16 | 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) |
12 | 18 | commands += commands.map{|cmd| "#{cmd}.exe"} if RUBY_PLATFORM =~ /win32/ |
13 | 19 | commands.detect do |cmd| |
14 | 20 | dirs_on_path.detect do |path| |
15 | 21 | File.executable? File.join(path, cmd) |
16 | 22 | 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.") |
18 | 24 | end |
19 | 25 |
|
| 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 |
20 | 36 |
|
21 | | -require 'yaml' |
22 | | -config = YAML::load(File.read(RAILS_ROOT + "/config/database.yml"))[env] |
| 37 | + args << config['database'] |
23 | 38 |
|
24 | | -unless config |
25 | | - abort "No database is configured for the environment '#{env}'" |
26 | | -end |
| 39 | + exec(find_cmd('mysql5', 'mysql'), *args) |
27 | 40 |
|
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']])) |
39 | 41 | when "postgresql" |
40 | 42 | ENV['PGHOST'] = config["host"] if config["host"] |
41 | 43 | ENV['PGPORT'] = config["port"].to_s if config["port"] |
42 | 44 | ENV['PGPASSWORD'] = config["password"].to_s if config["password"] |
43 | 45 | exec(find_cmd('psql'), '-U', config["username"], config["database"]) |
| 46 | + |
44 | 47 | when "sqlite" |
45 | 48 | exec(find_cmd('sqlite'), config["database"]) |
| 49 | + |
46 | 50 | when "sqlite3" |
47 | 51 | 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!" |
49 | 55 | end |
0 commit comments