aboutsummaryrefslogtreecommitdiffstats
path: root/railties/lib/rails/commands/dbconsole.rb
diff options
context:
space:
mode:
authorMikel Lindsaar <raasdnil@gmail.com>2009-12-17 11:24:02 +1100
committerMikel Lindsaar <raasdnil@gmail.com>2009-12-17 11:24:02 +1100
commit186cd7bc530f705b889c27f3680ab48c7c10a6f3 (patch)
tree76955e442615d77ee05ef2a8260c5373e1cac680 /railties/lib/rails/commands/dbconsole.rb
parent5f2395041d1578433fa825ed5c6f26a201f2203d (diff)
parentb9d4ceb43c9497fb1c47d8b1e1e6a24a9e157384 (diff)
downloadrails-186cd7bc530f705b889c27f3680ab48c7c10a6f3.tar.gz
rails-186cd7bc530f705b889c27f3680ab48c7c10a6f3.tar.bz2
rails-186cd7bc530f705b889c27f3680ab48c7c10a6f3.zip
Merge branch 'rails'
Conflicts: actionmailer/lib/action_mailer.rb actionmailer/lib/action_mailer/delivery_method/smtp.rb
Diffstat (limited to 'railties/lib/rails/commands/dbconsole.rb')
-rw-r--r--railties/lib/rails/commands/dbconsole.rb171
1 files changed, 92 insertions, 79 deletions
diff --git a/railties/lib/rails/commands/dbconsole.rb b/railties/lib/rails/commands/dbconsole.rb
index 4e699acf6b..77c3404343 100644
--- a/railties/lib/rails/commands/dbconsole.rb
+++ b/railties/lib/rails/commands/dbconsole.rb
@@ -2,86 +2,99 @@ require 'erb'
require 'yaml'
require 'optparse'
-include_password = false
-options = {}
-
-OptionParser.new do |opt|
- opt.banner = "Usage: dbconsole [options] [environment]"
- opt.on("-p", "--include-password", "Automatically provide the password from database.yml") do |v|
- include_password = true
- end
-
- opt.on("--mode [MODE]", ['html', 'list', 'line', 'column'],
- "Automatically put the sqlite3 database in the specified mode (html, list, line, column).") do |mode|
- options['mode'] = mode
- end
-
- opt.on("-h", "--header") do |h|
- options['header'] = h
- end
-
- opt.parse!(ARGV)
- abort opt.to_s unless (0..1).include?(ARGV.size)
-end
-
-env = ARGV.first || ENV['RAILS_ENV'] || 'development'
-unless config = YAML::load(ERB.new(IO.read(Rails.root + "/config/database.yml")).result)[env]
- abort "No database is configured for the environment '#{env}'"
-end
-
+module Rails
+ class DBConsole
+ def self.start(app)
+ new(app).start
+ end
-def find_cmd(*commands)
- dirs_on_path = ENV['PATH'].to_s.split(File::PATH_SEPARATOR)
- commands += commands.map{|cmd| "#{cmd}.exe"} if RUBY_PLATFORM =~ /win32/
+ def initialize(app)
+ @app = app
+ end
- full_path_command = nil
- found = commands.detect do |cmd|
- dir = dirs_on_path.detect do |path|
- full_path_command = File.join(path, cmd)
- File.executable? full_path_command
+ def start
+ include_password = false
+ options = {}
+ OptionParser.new do |opt|
+ opt.banner = "Usage: dbconsole [options] [environment]"
+ opt.on("-p", "--include-password", "Automatically provide the password from database.yml") do |v|
+ include_password = true
+ end
+
+ opt.on("--mode [MODE]", ['html', 'list', 'line', 'column'],
+ "Automatically put the sqlite3 database in the specified mode (html, list, line, column).") do |mode|
+ options['mode'] = mode
+ end
+
+ opt.on("-h", "--header") do |h|
+ options['header'] = h
+ end
+
+ opt.parse!(ARGV)
+ abort opt.to_s unless (0..1).include?(ARGV.size)
+ end
+
+ env = ARGV.first || ENV['RAILS_ENV'] || 'development'
+ unless config = YAML::load(ERB.new(IO.read("#{@app.root}/config/database.yml")).result)[env]
+ abort "No database is configured for the environment '#{env}'"
+ end
+
+
+ def find_cmd(*commands)
+ dirs_on_path = ENV['PATH'].to_s.split(File::PATH_SEPARATOR)
+ commands += commands.map{|cmd| "#{cmd}.exe"} if RUBY_PLATFORM =~ /win32/
+
+ full_path_command = nil
+ found = commands.detect do |cmd|
+ dir = dirs_on_path.detect do |path|
+ full_path_command = File.join(path, cmd)
+ File.executable? full_path_command
+ end
+ end
+ found ? full_path_command : abort("Couldn't find database client: #{commands.join(', ')}. Check your $PATH and try again.")
+ end
+
+ case config["adapter"]
+ when "mysql"
+ args = {
+ 'host' => '--host',
+ 'port' => '--port',
+ 'socket' => '--socket',
+ 'username' => '--user',
+ 'encoding' => '--default-character-set'
+ }.map { |opt, arg| "#{arg}=#{config[opt]}" if config[opt] }.compact
+
+ if config['password'] && include_password
+ args << "--password=#{config['password']}"
+ elsif config['password'] && !config['password'].to_s.empty?
+ args << "-p"
+ end
+
+ args << config['database']
+
+ exec(find_cmd('mysql', 'mysql5'), *args)
+
+ when "postgresql"
+ ENV['PGUSER'] = config["username"] if config["username"]
+ ENV['PGHOST'] = config["host"] if config["host"]
+ ENV['PGPORT'] = config["port"].to_s if config["port"]
+ ENV['PGPASSWORD'] = config["password"].to_s if config["password"] && include_password
+ exec(find_cmd('psql'), config["database"])
+
+ when "sqlite"
+ exec(find_cmd('sqlite'), config["database"])
+
+ when "sqlite3"
+ args = []
+
+ args << "-#{options['mode']}" if options['mode']
+ args << "-header" if options['header']
+ args << config['database']
+
+ exec(find_cmd('sqlite3'), *args)
+ else
+ abort "Unknown command-line client for #{config['database']}. Submit a Rails patch to add support!"
+ end
end
end
- found ? full_path_command : abort("Couldn't find database client: #{commands.join(', ')}. Check your $PATH and try again.")
-end
-
-case config["adapter"]
-when "mysql"
- args = {
- 'host' => '--host',
- 'port' => '--port',
- 'socket' => '--socket',
- 'username' => '--user',
- 'encoding' => '--default-character-set'
- }.map { |opt, arg| "#{arg}=#{config[opt]}" if config[opt] }.compact
-
- if config['password'] && include_password
- args << "--password=#{config['password']}"
- elsif config['password'] && !config['password'].to_s.empty?
- args << "-p"
- end
-
- args << config['database']
-
- exec(find_cmd('mysql', 'mysql5'), *args)
-
-when "postgresql"
- ENV['PGUSER'] = config["username"] if config["username"]
- ENV['PGHOST'] = config["host"] if config["host"]
- ENV['PGPORT'] = config["port"].to_s if config["port"]
- ENV['PGPASSWORD'] = config["password"].to_s if config["password"] && include_password
- exec(find_cmd('psql'), config["database"])
-
-when "sqlite"
- exec(find_cmd('sqlite'), config["database"])
-
-when "sqlite3"
- args = []
-
- args << "-#{options['mode']}" if options['mode']
- args << "-header" if options['header']
- args << config['database']
-
- exec(find_cmd('sqlite3'), *args)
-else
- abort "Unknown command-line client for #{config['database']}. Submit a Rails patch to add support!"
-end
+end \ No newline at end of file