Class: MySQLDBTool::Restore

Inherits:
Object
  • Object
show all
Defined in:
lib/mysql_db_tool/restore.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Restore

Returns a new instance of Restore.



7
8
9
10
11
12
13
14
15
# File 'lib/mysql_db_tool/restore.rb', line 7

def initialize(options = {})
  @options = options
  tableConfig = MySQLDBTool::Config::ConfigLoader.load(options[:env])
  @data_tables = tableConfig[:data_tables]
  @ignore_tables = tableConfig[:ignore_tables]
  @db_info = tableConfig[:db_info]

  @db_info[:database] = @options[:database] if @options[:database]
end

Instance Method Details

#performObject



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
69
70
71
72
# File 'lib/mysql_db_tool/restore.rb', line 17

def perform

  verify_tools_exist

  env=@options[:env] || "local"

  if env.downcase.end_with? "prod"
    abort "Aborted. Never restore production DB!"
  end

  id=@options[:id] || "0"
  isRun=@options[:run]
  isDropAllTables=@options[:drop_all_tables]

  puts "ARGV=#{ARGV}, env=#{env}, id=#{id}, run=#{isRun} isDropAllTables=#{isDropAllTables}"

  backupDir=backupDirName(id)

  puts "backupDir=#{backupDir}"
  databases = Array(@db_info[:database])

  databaseMap = {}
  sameDb = true

  Dir.entries(backupDir).reject {|f| File.directory? f}.sort.each do |f|

    index, origin_database = split_integer_and_string(f)
    database = get_element_or_last(databases, index)
    sameDb = sameDb && (database == origin_database)
    databaseMap["`#{origin_database}`\\."] = "`#{database}`."
  end

  gsubstring = sameDb ? "" : databaseMap.map { |k,v| ".gsub(/#{k}/, \"#{v}\")" }.join("")

  Dir.entries(backupDir).reject {|f| File.directory? f}.sort.flat_map do |f|

    commands = []

    index, origin_database = split_integer_and_string(f)
    database = get_element_or_last(databases, index)

    defaultOptions=mysqlDefaultOptions(@db_info, database)
    backupDir=backupDirName(id, f)

    commands.push("cat #{MySQLDBTool.find_resource('sql/drop_all_tables.sql')} | mysql #{defaultOptions}") if isDropAllTables
    commands.push("cat #{MySQLDBTool.find_resource('sql/drop_all_views.sql')} | mysql #{defaultOptions}") if isDropAllTables

    Dir.entries(backupDir).reject {|f| File.directory? f} .select {|f| f.include?("-schema.sql")} .each {|f|
      restore_each(commands, backupDir+"/"+f, defaultOptions, gsubstring)
    }
    Dir.entries(backupDir).reject {|f| File.directory? f} .reject {|f| f.include?("-schema.sql")} .each {|f|
      restore_each(commands, backupDir+"/"+f, defaultOptions, gsubstring)
    }
    commands
  end
end