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
+def find_cmd(*commands)
0
+ dirs_on_path = ENV['PATH'].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
+config = YAML::load(File.read(RAILS_ROOT + "/config/database.yml"))[env]
0
+ abort "No database is configured for the environment '#{env}'"
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"
Comments
This leaves the MySQL password in the output of ps. The same is true in the case of the environment variable used for postgres.
If this is a concern, you develop in a strange place and have bigger problems. That said, patches are welcome :)