Skip to content
This repository
Browse code

Refactor script/dbconsole into an object

  • Loading branch information...
commit a2cb90c0c246e772e9735ff59f5517e3dee313ff 1 parent d8c5ea7
authored
169  railties/lib/rails/commands/dbconsole.rb
@@ -2,86 +2,95 @@
2 2
 require 'yaml'
3 3
 require 'optparse'
4 4
 
5  
-include_password = false
6  
-options = {}
7  
-
8  
-OptionParser.new do |opt|
9  
-  opt.banner = "Usage: dbconsole [options] [environment]"
10  
-  opt.on("-p", "--include-password", "Automatically provide the password from database.yml") do |v|
11  
-    include_password = true
12  
-  end
13  
-
14  
-  opt.on("--mode [MODE]", ['html', 'list', 'line', 'column'],
15  
-    "Automatically put the sqlite3 database in the specified mode (html, list, line, column).") do |mode|
16  
-      options['mode'] = mode
17  
-  end
18  
-
19  
-  opt.on("-h", "--header") do |h|
20  
-    options['header'] = h
21  
-  end
22  
-
23  
-  opt.parse!(ARGV)
24  
-  abort opt.to_s unless (0..1).include?(ARGV.size)
25  
-end
26  
-
27  
-env = ARGV.first || ENV['RAILS_ENV'] || 'development'
28  
-unless config = YAML::load(ERB.new(IO.read(Rails.root + "/config/database.yml")).result)[env]
29  
-  abort "No database is configured for the environment '#{env}'"
30  
-end
31  
-
32  
-
33  
-def find_cmd(*commands)
34  
-  dirs_on_path = ENV['PATH'].to_s.split(File::PATH_SEPARATOR)
35  
-  commands += commands.map{|cmd| "#{cmd}.exe"} if RUBY_PLATFORM =~ /win32/
  5
+module Rails
  6
+  class DBConsole
  7
+    def self.start
  8
+      new.start
  9
+    end
36 10
 
37  
-  full_path_command = nil
38  
-  found = commands.detect do |cmd|
39  
-    dir = dirs_on_path.detect do |path|
40  
-      full_path_command = File.join(path, cmd)
41  
-      File.executable? full_path_command
  11
+    def start
  12
+      include_password = false
  13
+      options = {}
  14
+      OptionParser.new do |opt|
  15
+        opt.banner = "Usage: dbconsole [options] [environment]"
  16
+        opt.on("-p", "--include-password", "Automatically provide the password from database.yml") do |v|
  17
+          include_password = true
  18
+        end
  19
+
  20
+        opt.on("--mode [MODE]", ['html', 'list', 'line', 'column'],
  21
+          "Automatically put the sqlite3 database in the specified mode (html, list, line, column).") do |mode|
  22
+            options['mode'] = mode
  23
+        end
  24
+
  25
+        opt.on("-h", "--header") do |h|
  26
+          options['header'] = h
  27
+        end
  28
+
  29
+        opt.parse!(ARGV)
  30
+        abort opt.to_s unless (0..1).include?(ARGV.size)
  31
+      end
  32
+
  33
+      env = ARGV.first || ENV['RAILS_ENV'] || 'development'
  34
+      unless config = YAML::load(ERB.new(IO.read("#{Rails.root}/config/database.yml")).result)[env]
  35
+        abort "No database is configured for the environment '#{env}'"
  36
+      end
  37
+
  38
+
  39
+      def find_cmd(*commands)
  40
+        dirs_on_path = ENV['PATH'].to_s.split(File::PATH_SEPARATOR)
  41
+        commands += commands.map{|cmd| "#{cmd}.exe"} if RUBY_PLATFORM =~ /win32/
  42
+
  43
+        full_path_command = nil
  44
+        found = commands.detect do |cmd|
  45
+          dir = dirs_on_path.detect do |path|
  46
+            full_path_command = File.join(path, cmd)
  47
+            File.executable? full_path_command
  48
+          end
  49
+        end
  50
+        found ? full_path_command : abort("Couldn't find database client: #{commands.join(', ')}. Check your $PATH and try again.")
  51
+      end
  52
+
  53
+      case config["adapter"]
  54
+      when "mysql"
  55
+        args = {
  56
+          'host'      => '--host',
  57
+          'port'      => '--port',
  58
+          'socket'    => '--socket',
  59
+          'username'  => '--user',
  60
+          'encoding'  => '--default-character-set'
  61
+        }.map { |opt, arg| "#{arg}=#{config[opt]}" if config[opt] }.compact
  62
+
  63
+        if config['password'] && include_password
  64
+          args << "--password=#{config['password']}"
  65
+        elsif config['password'] && !config['password'].to_s.empty?
  66
+          args << "-p"
  67
+        end
  68
+
  69
+        args << config['database']
  70
+
  71
+        exec(find_cmd('mysql', 'mysql5'), *args)
  72
+
  73
+      when "postgresql"
  74
+        ENV['PGUSER']     = config["username"] if config["username"]
  75
+        ENV['PGHOST']     = config["host"] if config["host"]
  76
+        ENV['PGPORT']     = config["port"].to_s if config["port"]
  77
+        ENV['PGPASSWORD'] = config["password"].to_s if config["password"] && include_password
  78
+        exec(find_cmd('psql'), config["database"])
  79
+
  80
+      when "sqlite"
  81
+        exec(find_cmd('sqlite'), config["database"])
  82
+
  83
+      when "sqlite3"
  84
+        args = []
  85
+
  86
+        args << "-#{options['mode']}" if options['mode']
  87
+        args << "-header" if options['header']
  88
+        args << config['database']
  89
+
  90
+        exec(find_cmd('sqlite3'), *args)
  91
+      else
  92
+        abort "Unknown command-line client for #{config['database']}. Submit a Rails patch to add support!"
  93
+      end
42 94
     end
43 95
   end
44  
-  found ? full_path_command : abort("Couldn't find database client: #{commands.join(', ')}. Check your $PATH and try again.")
45  
-end
46  
-
47  
-case config["adapter"]
48  
-when "mysql"
49  
-  args = {
50  
-    'host'      => '--host',
51  
-    'port'      => '--port',
52  
-    'socket'    => '--socket',
53  
-    'username'  => '--user',
54  
-    'encoding'  => '--default-character-set'
55  
-  }.map { |opt, arg| "#{arg}=#{config[opt]}" if config[opt] }.compact
56  
-
57  
-  if config['password'] && include_password
58  
-    args << "--password=#{config['password']}"
59  
-  elsif config['password'] && !config['password'].to_s.empty?
60  
-    args << "-p"
61  
-  end
62  
-
63  
-  args << config['database']
64  
-
65  
-  exec(find_cmd('mysql', 'mysql5'), *args)
66  
-
67  
-when "postgresql"
68  
-  ENV['PGUSER']     = config["username"] if config["username"]
69  
-  ENV['PGHOST']     = config["host"] if config["host"]
70  
-  ENV['PGPORT']     = config["port"].to_s if config["port"]
71  
-  ENV['PGPASSWORD'] = config["password"].to_s if config["password"] && include_password
72  
-  exec(find_cmd('psql'), config["database"])
73  
-
74  
-when "sqlite"
75  
-  exec(find_cmd('sqlite'), config["database"])
76  
-
77  
-when "sqlite3"
78  
-  args = []
79  
-
80  
-  args << "-#{options['mode']}" if options['mode']
81  
-  args << "-header" if options['header']
82  
-  args << config['database']
83  
-
84  
-  exec(find_cmd('sqlite3'), *args)
85  
-else
86  
-  abort "Unknown command-line client for #{config['database']}. Submit a Rails patch to add support!"
87  
-end
  96
+end
1  railties/lib/rails/generators/rails/app/templates/script/dbconsole
... ...
@@ -1,2 +1,3 @@
1 1
 require File.expand_path('../../config/application',  __FILE__)
2 2
 require 'rails/commands/dbconsole'
  3
+Rails::DBConsole.start

0 notes on commit a2cb90c

Please sign in to comment.
Something went wrong with that request. Please try again.