Class: Mysql::Partitioner::Cli

Inherits:
Object
  • Object
show all
Defined in:
lib/mysql/partitioner/cli.rb

Instance Method Summary collapse

Instance Method Details

#runObject



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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/mysql/partitioner/cli.rb', line 12

def run()

  options = {
    :dry_run     => false,
    :debug       => false,
    :config      => nil,
  }
  begin
    ARGV.options do |opt|
      opt.on("-c", "--config CONFIG_NAME")         {    |v| options[:config] = v }
      opt.on("", "--cmd check or migrate")         {    |v| options[:cmd] = v  }
      opt.on('',   '--dry-run')                    {    |v| options[:dry_run]  = v }
      opt.on('-d',   '--debug')                    {    |v| options[:debug]  =  v }
      opt.parse!
    end
  rescue => e
    $stderr.puts e
    exit 1
  end
  config = nil
  begin
    config = YAML.load_file(options[:config])
  rescue => e
    puts "Failed to load yaml file #{options[:config]} #{e}"
    exit 1
  end

  logger = Logger.new(STDOUT)
  logger.level = options[:debug] ?  Logger::DEBUG : Logger::INFO
  config.deep_symbolize_keys!

  config.keys.each do|item|
    task = config[item]
    database = task[:database]
    cli =  Mysql2::Client.new(
      :host     => database[:host],
      :username => database[:username],
      :password => database[:password],
      :port     => database[:port],
      :database => database[:name],
    )
    session = Mysql::Partitioner::Session.new(cli, options[:dry_run], logger)
    strategy = Mysql::Partitioner::Strategy.build(session, task[:table], task[:strategy])
    case options[:cmd]
    when "check"
      logger.info("Running check on #{item}")
      strategy.check!
      logger.info("success")
    when "migrate"
      logger.info("Running migrate on #{item} dry_run=#{options[:dry_run]}")
      strategy.migrate
      logger.info("success")
    else
      raise "Unknown command"
    end
  end
end