Class: DBRocket::Command::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/db_rockets/cli/commands/base.rb

Direct Known Subclasses

App, Help, Server

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Base

Returns a new instance of Base.



6
7
8
# File 'lib/db_rockets/cli/commands/base.rb', line 6

def initialize(args)
  @args = args
end

Instance Attribute Details

#argsObject

Returns the value of attribute args.



5
6
7
# File 'lib/db_rockets/cli/commands/base.rb', line 5

def args
  @args
end

Instance Method Details

#askObject



46
47
48
# File 'lib/db_rockets/cli/commands/base.rb', line 46

def ask
  gets.strip
end

#ask_for_config_fileObject



66
67
68
69
70
71
72
73
# File 'lib/db_rockets/cli/commands/base.rb', line 66

def ask_for_config_file
  if File.exists?(config_file)
    print "The file config/db_rocket.yml exists, do you want overwrite this? (y/n): "
    ask
  else
    "y"
  end
end

#conf_to_uri_hash(conf) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/db_rockets/cli/commands/base.rb', line 98

def conf_to_uri_hash(conf)
  uri = {}
  uri['scheme'] = conf['adapter']
  uri['username'] = conf['user'] || conf['username']
  uri['password'] = conf['password']
  uri['host'] = conf['host'] || conf['hostname']
  uri['port'] = conf['port']
  uri['path'] = conf['database']

  conf['encoding'] = 'utf8' if conf['encoding'] == 'unicode' or conf['encoding'].nil?
  uri['query'] = "encoding=#{conf['encoding']}"

  uri
end

#config_fileObject



62
63
64
# File 'lib/db_rockets/cli/commands/base.rb', line 62

def config_file
  'config/db_rocket.yml'
end

#confirm(message = "Are you sure you wish to continue? (y/n)?") ⇒ Object



35
36
37
38
# File 'lib/db_rockets/cli/commands/base.rb', line 35

def confirm(message="Are you sure you wish to continue? (y/n)?")
  display("#{message} ", false)
  ask.downcase == 'y'
end

#display(msg, newline = true) ⇒ Object



26
27
28
29
30
31
32
33
# File 'lib/db_rockets/cli/commands/base.rb', line 26

def display(msg, newline=true)
  if newline
    puts(msg)
  else
    print(msg)
    STDOUT.flush
  end
end

#error(msg) ⇒ Object



40
41
42
43
# File 'lib/db_rockets/cli/commands/base.rb', line 40

def error(msg)
  STDERR.puts(msg)
  exit 1
end

#extract_option(options, default = true) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/db_rockets/cli/commands/base.rb', line 10

def extract_option(options, default=true)
  values = options.is_a?(Array) ? options : [options]
  return unless opt_index = args.select { |a| values.include? a }.first
  opt_position = args.index(opt_index) + 1
  if args.size > opt_position && opt_value = args[opt_position]
    if opt_value.include?('--')
      opt_value = nil
    else
      args.delete_at(opt_position)
    end
  end
  opt_value ||= default
  args.delete(opt_index)
  block_given? ? yield(opt_value) : opt_value
end

#home_directoryObject



54
55
56
# File 'lib/db_rockets/cli/commands/base.rb', line 54

def home_directory
  running_on_windows? ? ENV['USERPROFILE'] : ENV['HOME']
end

#load_tapsObject



191
192
193
194
195
196
197
198
199
200
201
# File 'lib/db_rockets/cli/commands/base.rb', line 191

def load_taps
  require 'taps/operation'
  require 'taps/cli'
  error "The db rocket gem requires taps 0.3" unless Taps.version =~ /^0.3/
  display "Loaded Taps v#{Taps.version}"
rescue LoadError
  message  = "Taps 0.3 Load Error: #{$!.message}\n"
  message << "You may need to install or update the taps gem to use db commands.\n"
  message << "On most systems this will be:\n\nsudo gem install taps"
  error message
end

#make_config_fileObject



204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/db_rockets/cli/commands/base.rb', line 204

def make_config_file
  overwrite_or_create_file = ask_for_config_file
  if overwrite_or_create_file == "y"
    config_file_hash = <<EOFILE
common: &common
  server: 127.0.0.1
  port: 5555
  http_user: user
  http_password: password
development:
  <<: *common
production:
  <<: *common
EOFILE
    File.open(config_file, 'w') do |f|
      f.puts config_file_hash
    end
  end
  overwrite_or_create_file
end

#make_url_from_config(environment) ⇒ Object



186
187
188
189
# File 'lib/db_rockets/cli/commands/base.rb', line 186

def make_url_from_config environment
  conf = YAML.load(File.read(Dir.pwd + "/#{config_file}"))[environment]
  "http://#{conf['http_user']}:#{conf['http_password']}@#{conf['server']}:#{conf['port']}"
end

#parse_database_yml(environment = nil) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/db_rockets/cli/commands/base.rb', line 75

def parse_database_yml(environment = nil)
  return "" unless File.exists?(Dir.pwd + '/config/database.yml')

  environment = ENV['RAILS_ENV'] || ENV['MERB_ENV'] || ENV['RACK_ENV'] if environment.nil?
  environment = 'development' if environment.nil? or environment.empty?

  conf = YAML.load(File.read(Dir.pwd + '/config/database.yml'))[environment]
  case conf['adapter']
    when 'sqlite3'
      return "sqlite://#{conf['database']}"
    when 'postgresql'
      uri_hash = conf_to_uri_hash(conf)
      uri_hash['scheme'] = 'postgres'
      return uri_hash_to_url(uri_hash)
    else
      return uri_hash_to_url(conf_to_uri_hash(conf))
  end
rescue Exception => ex
  puts "Error parsing database.yml: #{ex.message}"
  puts ex.backtrace
  ""
end

#parse_taps_optsObject

Raises:



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/db_rockets/cli/commands/base.rb', line 138

def parse_taps_opts
  opts = {}
  opts[:default_chunksize] = extract_option("--chunksize") || 1000
  opts[:default_chunksize] = opts[:default_chunksize].to_i rescue 1000
  opts[:environment] = extract_option("--environment") || ENV['RAILS_ENV'] || ENV['MERB_ENV'] || ENV['RACK_ENV']  || 'development'

  if filter = extract_option("--filter")
    opts[:table_filter] = filter
  elsif tables = extract_option("--tables")
    r_tables = tables.split(",").collect { |t| "^#{t.strip}$" }
    opts[:table_filter] = "(#{r_tables.join("|")})"
  end

  if extract_option("--disable-compression")
    opts[:disable_compression] = true
  end

  if resume_file = extract_option("--resume-filename")
    opts[:resume_filename] = resume_file
  end

  opts[:indexes_first] = !extract_option("--indexes-last")

  opts[:database_url] = args.shift.strip rescue ''
  if opts[:database_url] == ''
    opts[:database_url] = parse_database_yml(opts[:environment])
    display "Auto-detected local database: #{opts[:database_url]}" if opts[:database_url] != ''
  end
  raise(CommandFailed, "Invalid database url") if opts[:database_url] == ''

  if extract_option("--debug")
    Taps.log.level = Logger::DEBUG
  end

  #ENV['TZ'] = 'America/Los_Angeles'
  opts
end

#running_on_windows?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/db_rockets/cli/commands/base.rb', line 58

def running_on_windows?
  RUBY_PLATFORM =~ /mswin32/
end

#shell(cmd) ⇒ Object



50
51
52
# File 'lib/db_rockets/cli/commands/base.rb', line 50

def shell(cmd)
  `#{cmd}`
end

#taps_client(op, opts) ⇒ Object



176
177
178
179
180
181
182
183
184
# File 'lib/db_rockets/cli/commands/base.rb', line 176

def taps_client(op, opts)
  Taps::Config.verify_database_url(opts[:database_url])
  if opts[:resume_filename]
    Taps::Cli.new([]).clientresumexfer(op, opts)
  else
    opts[:remote_url] = make_url_from_config(opts[:environment])
    Taps::Cli.new([]).clientxfer(op, opts)
  end
end

#uri_hash_to_url(uri) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/db_rockets/cli/commands/base.rb', line 124

def uri_hash_to_url(uri)
  uri_parts = {
    :scheme   => uri['scheme'],
    :userinfo => userinfo_from_uri(uri),
    :password => uri['password'],
    :host     => uri['host'] || '127.0.0.1',
    :port     => uri['port'],
    :path     => "/%s" % uri['path'],
    :query    => uri['query'],
  }

  URI::Generic.build(uri_parts).to_s
end

#userinfo_from_uri(uri) ⇒ Object



113
114
115
116
117
118
119
120
121
122
# File 'lib/db_rockets/cli/commands/base.rb', line 113

def userinfo_from_uri(uri)
  username = uri['username'].to_s
  password = uri['password'].to_s
  return nil if username == ''

  userinfo  = ""
  userinfo << username
  userinfo << ":" << password if password.length > 0
  userinfo
end