Class: Flydata::Mysql::MysqlDumpGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/flydata/command/sync.rb

Constant Summary collapse

MYSQL_DUMP_CMD_TEMPLATE =

host, port, username, password, database, tables

"mysqldump -h %s -P %s -u%s %s --skip-lock-tables --single-transaction --flush-logs --hex-blob --master-data=2 %s %s"

Instance Method Summary collapse

Constructor Details

#initialize(conf) ⇒ MysqlDumpGenerator

Returns a new instance of MysqlDumpGenerator.



627
628
629
630
631
632
633
634
635
636
# File 'lib/flydata/command/sync.rb', line 627

def initialize(conf)
  password = conf['password'].to_s.empty? ? "" : "-p#{conf['password']}"
  tables = if conf['tables']
             conf['tables'].split(',').join(' ')
           else
             ''
           end
  @dump_cmd = MYSQL_DUMP_CMD_TEMPLATE %
    [conf['host'], conf['port'], conf['username'], password, conf['database'], tables]
end

Instance Method Details

#dump(file_path) ⇒ Object



637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
# File 'lib/flydata/command/sync.rb', line 637

def dump(file_path)
  cmd = "#{@dump_cmd} > #{file_path}"
  o, e, s = Open3.capture3(cmd)
  e.to_s.each_line {|l|  puts l unless /^Warning:/ =~ l } unless e.to_s.empty?
  unless s.exitstatus == 0
    if File.exists?(file_path)
      File.open(file_path, 'r') {|f| f.each_line{|l| puts l}}
      FileUtils.rm(file_path)
    end
    raise "Failed to run mysqldump command."
  end
  unless File.exists?(file_path)
    raise "mysqldump file does not exist. Something wrong..."
  end
  if File.size(file_path) == 0
    raise "mysqldump file is empty. Something wrong..."
  end
  true
end