public this repo is viewable by everyone
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/rails/rails.git
Add 'script/dbconsole' -- the database analog of 'script/console'
purcell (author)
12 days ago
jeremy (committer)
8 days ago
commit  4a07103687084496b773e18a03b1f2f5e686f7ad
tree    98b50b9347310a9fa9d9e561d0b2d5d245addc72
parent  0a21193dc660396fb993b06d1d3c168a9cd900a5
...
 
 
 
...
1
2
3
0
@@ -0,0 +1,3 @@
0
+#!/usr/bin/env ruby
0
+require File.dirname(__FILE__) + '/../config/boot'
0
+require 'commands/dbconsole'
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
0
@@ -0,0 +1,49 @@
0
+require 'optparse'
0
+OptionParser.new do |opt|
0
+ opt.banner = "Usage: dbconsole [environment]"
0
+ opt.parse!(ARGV)
0
+ abort opt.to_s unless (0..1).include?(ARGV.size)
0
+end
0
+
0
+env = ENV['RAILS_ENV'] = ARGV.first || ENV['RAILS_ENV'] || 'development'
0
+
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
0
+ end || abort("couldn't find matching executable: #{commands.join(', ')}")
0
+end
0
+
0
+
0
+require 'yaml'
0
+config = YAML::load(File.read(RAILS_ROOT + "/config/database.yml"))[env]
0
+
0
+unless config
0
+ abort "No database is configured for the environment '#{env}'"
0
+end
0
+
0
+case config["adapter"]
0
+when "mysql"
0
+ exec(find_cmd(*%w(mysql5 mysql)),
0
+ *({ 'host' => '--host',
0
+ 'port' => '--port',
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
+when "postgresql"
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
+when "sqlite"
0
+ exec(find_cmd('sqlite'), config["database"])
0
+when "sqlite3"
0
+ exec(find_cmd('sqlite3'), config["database"])
0
+else abort "not supported for this database type"
0
+end

Comments

  • jordi 2 days ago

    This leaves the MySQL password in the output of ps. The same is true in the case of the environment variable used for postgres.

  • jeremy 2 days ago

    If this is a concern, you develop in a strange place and have bigger problems. That said, patches are welcome :)