0
OptionParser.new do |opt|
0
opt.banner = "Usage: dbconsole [environment]"
0
abort opt.to_s unless (0..1).include?(ARGV.size)
0
-env = ENV['RAILS_ENV'] = ARGV.first || ENV['RAILS_ENV'] || 'development'
0
+env = ARGV.first || ENV['RAILS_ENV'] || 'development'
0
+unless config = YAML.load_file(RAILS_ROOT + "/config/database.yml")[env]
0
+ abort "No database is configured for the environment '#{env}'"
0
def find_cmd(*commands)
0
- dirs_on_path = ENV['PATH'].
split(File::PATH_SEPARATOR)
0
+ dirs_on_path = ENV['PATH'].
to_s.split(File::PATH_SEPARATOR)
0
commands += commands.map{|cmd| "#{cmd}.exe"} if RUBY_PLATFORM =~ /win32/
0
commands.detect do |cmd|
0
dirs_on_path.detect do |path|
0
File.executable? File.join(path, cmd)
0
- end || abort("
couldn't find matching executable: #{commands.join(', ')}")
0
+ end || abort("
Couldn't find database client: #{commands.join(', ')}. Check your $PATH and try again.")
0
+ 'socket' => '--socket',
0
+ 'username' => '--user',
0
+ 'password' => '--password',
0
+ 'encoding' => '--default-character-set'
0
+ }.map { |opt, arg| "#{arg}=#{config[opt]}" if config[opt] }.compact
0
-config = YAML::load(File.read(RAILS_ROOT + "/config/database.yml"))[env]
0
+ args << config['database']
0
- abort "No database is configured for the environment '#{env}'"
0
+ exec(find_cmd('mysql5', 'mysql'), *args)
0
- exec(find_cmd(*%w(mysql5 mysql)),
0
- *({ 'host' => '--host',
0
- 'socket' => '--socket',
0
- 'username' => '--user',
0
- 'password' => '--password',
0
- 'encoding' => '--default-character-set'
0
- }.map { |opt, arg| "#{arg}=#{config[opt]}" if config[opt] }.compact +
0
- [config['database']]))
0
ENV['PGHOST'] = config["host"] if config["host"]
0
ENV['PGPORT'] = config["port"].to_s if config["port"]
0
ENV['PGPASSWORD'] = config["password"].to_s if config["password"]
0
exec(find_cmd('psql'), '-U', config["username"], config["database"])
0
exec(find_cmd('sqlite'), config["database"])
0
exec(find_cmd('sqlite3'), config["database"])
0
-else abort "not supported for this database type"
0
+ abort "Unknown command-line client for #{config['database']}. Submit a Rails patch to add support!"
Comments
No one has commented yet.