Module: MysqlTruck::Helper

Includes:
FileUtils
Included in:
Dumper, Loader
Defined in:
lib/mysql_truck/helper.rb

Instance Method Summary collapse

Instance Method Details

#base_pathObject



54
55
56
57
58
59
60
# File 'lib/mysql_truck/helper.rb', line 54

def base_path
  if config[:dump_dir]
    config[:dump_dir].is_a?(Pathname) ? config[:dump_dir].join("mysqltruck") : Pathname.new(config[:dump_dir]).join("mysqltruck")
  else
    Pathname.new("/tmp/mysqltruck")
  end
end

#benchmarkObject



66
67
68
69
70
71
# File 'lib/mysql_truck/helper.rb', line 66

def benchmark
  time = Benchmark.realtime do
    yield
  end
  puts "Ran in #{formatted_time(time)}."
end

#bucket_dirObject



62
63
64
# File 'lib/mysql_truck/helper.rb', line 62

def bucket_dir
  "mysql/#{config[:bucket_dir] || config[:database]}/"
end

#configObject



6
7
8
# File 'lib/mysql_truck/helper.rb', line 6

def config
  @config
end

#csv_optionsObject



77
78
79
# File 'lib/mysql_truck/helper.rb', line 77

def csv_options
  " --fields-enclosed-by=\\\" --fields-terminated-by=, "
end

#db_connection_optionsObject



17
18
19
20
21
22
23
24
# File 'lib/mysql_truck/helper.rb', line 17

def db_connection_options
  opts = %Q[ -u #{config[:username]} ]
  opts += %Q[ -p"#{config[:password]}" ] unless config[:password].nil?
  opts += %Q[ -h #{config[:host]} --default-character-set=utf8 ]
  opts += %Q[ -P #{config[:port]} ] if config[:port]
  opts += %Q[ #{config[:database]} ]
  opts
end

#formatted_time(time) ⇒ Object



73
74
75
# File 'lib/mysql_truck/helper.rb', line 73

def formatted_time(time)
  "#{"%0.5f" % time}s"
end

#initialize_directoriesObject



34
35
36
37
38
# File 'lib/mysql_truck/helper.rb', line 34

def initialize_directories
  mkdir_p base_path
  mkdir_p tmp_path
  chmod 0777, tmp_path
end

#initialize_s3Object



10
11
12
13
14
15
# File 'lib/mysql_truck/helper.rb', line 10

def initialize_s3
  @s3 = RightAws::S3.new(
    config[:s3_access_key],
    config[:s3_secret_access_key])
  @bucket = @s3.bucket(config[:bucket])
end

#local_host?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/mysql_truck/helper.rb', line 26

def local_host?
  config[:host] == '127.0.0.1' || config[:host] == 'localhost'
end

#remote_host?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/mysql_truck/helper.rb', line 30

def remote_host?
  !local_host?
end

#remove_directoriesObject



40
41
42
# File 'lib/mysql_truck/helper.rb', line 40

def remove_directories
  rm_r tmp_path, :force => true
end

#s3_pathObject



49
50
51
52
# File 'lib/mysql_truck/helper.rb', line 49

def s3_path
  raise "@time not initialized" unless @time
  "#{bucket_dir}#{@time.strftime("%Y-%m-%d")}"
end

#tmp_pathObject



44
45
46
47
# File 'lib/mysql_truck/helper.rb', line 44

def tmp_path
  raise "@time not initialized" unless @time
  base_path.join(@time.strftime("%Y-%m-%d"))
end