#! /usr/bin/env ruby
#
# $Id: wipedb 1171 2006-06-10 16:36:57Z jpb $
#
# Copyright 2006 J. P. Block
require 'ostruct'
require 'optparse'
require 'yaml'
class MyArgs
MODENAMES = %w[development test production]
# return structure showing what opts were picked
def self.parse(args)
options = OpenStruct.new
options.mode = 'development'
options.debug = 0
opts = OptionParser.new do |opts|
opts.banner = "Usage: script/wipedb [options]"
opts.separator ""
opts.separator "Specific options:"
mode_list = MODENAMES.join(',')
opts.on("-m MODENAME", "--mode MODENAME", MODENAMES, "Select mode",
" (#{mode_list})") do |the_mode|
options.mode = the_mode
end
opts.on("-d", "--debug", "Turn on debug") do
options.debug = 1
end
opts.on_tail("-h", "--help", "Show this message") do
puts opts
exit
end
opts.on_tail("--version", "Show version") do
puts "$Id: wipedb 1171 2006-06-10 16:36:57Z jpb $"
exit
end
end
opts.parse!(args)
options
end # parse()
end
options = MyArgs.parse(ARGV)
mode = options.mode
config = YAML.load_file('config/database.yml')
database = config[options.mode]['database']
host = config[options.mode]['host']
password = config[options.mode]['password']
username = config[options.mode]['username']
if options.debug == 1:
puts "mode: #{mode}"
puts "database: #{database}"
puts "host: #{host}"
puts "username: #{username}"
puts "password: #{password}"
end
zap_list = `mysqldump -u #{username} -p#{password} --complete-insert --add-drop-table --quick --single-transaction #{database} | grep DROP`
zapper = IO.popen("mysql -u #{username} -p#{password} #{database}", "w+")
zap_list.each do |z|
zapper.puts(z)
end
zapper.close_write
|