Class: Sandbox

Inherits:
Object
  • Object
show all
Defined in:
lib/db_helper/sandbox.rb

Instance Method Summary collapse

Constructor Details

#initialize(dir) ⇒ Sandbox

Returns a new instance of Sandbox.



4
5
6
# File 'lib/db_helper/sandbox.rb', line 4

def initialize(dir)
  @dir = dir
end

Instance Method Details

#import(file) ⇒ Object



8
9
10
11
12
13
14
15
16
# File 'lib/db_helper/sandbox.rb', line 8

def import(file)
  if file.end_with?('tar.gz')
    import_innobackup(file)
  elsif file.end_with?('sql.gz')
    import_sql(file)
  else
    raise "Unrecognised file format for #{file}."
  end
end

#import_innobackup(file) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/db_helper/sandbox.rb', line 26

def import_innobackup(file)
  if File.exist?(data_new = File.join(@dir, 'data.new'))
    command "rm -rf #{data_new}"
  end
  command File.join(@dir,'start')
  command "mkdir #{data_new}"
  command "mv #{File.join(@dir, file)} #{File.join(data_new, file)}"
  command "cd #{data_new} && tar -xzf #{file}"
  command "rm #{File.join(data_new, file)}"
  command File.join(@dir,'stop')
  command "rm -rf #{File.join(@dir,'data')}"
  command "mv #{data_new} #{File.join(@dir,'data')}"
  command File.join(@dir,'start')
end

#import_sql(file) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/db_helper/sandbox.rb', line 18

def import_sql(file)
  command "gunzip #{File.join(@dir, file)}"
  command File.join(@dir, 'start')
  db_name = file.gsub(/_[0-9]{4}.*/, '')
  command "#{File.join(@dir, 'use')} #{db_name} < #{File.join(@dir, file.sub('.gz',''))}"
  command "rm #{File.join(@dir, file.sub('.gz',''))}"
end