Method: Lhm#cleanup

Defined in:
lib/lhm.rb

#cleanup(run = false, options = {}) ⇒ Object

Cleanup tables and triggers

Parameters:

  • run (Boolean) (defaults to: false)

    execute now or just display information

  • options (Hash) (defaults to: {})

    Optional options to alter cleanup behaviour

Options Hash (options):

  • :until (Time)

    Filter to only remove tables up to specified time (defaults to: nil)



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/lhm.rb', line 60

def cleanup(run = false, options = {})
  lhm_tables = connection.select_values('show tables').select { |name| name =~ /^lhm(a|n)_/ }
  if options[:until]
    lhm_tables.select! { |table|
      table_date_time = Time.strptime(table, 'lhma_%Y_%m_%d_%H_%M_%S')
      table_date_time <= options[:until]
    }
  end

  lhm_triggers = connection.select_values('show triggers').collect do |trigger|
    trigger.respond_to?(:trigger) ? trigger.trigger : trigger
  end.select { |name| name =~ /^lhmt/ }

  if run
    lhm_triggers.each do |trigger|
      connection.execute("drop trigger if exists #{trigger}")
    end
    lhm_tables.each do |table|
      connection.execute("drop table if exists #{table}")
    end
    true
  elsif lhm_tables.empty? && lhm_triggers.empty?
    puts 'Everything is clean. Nothing to do.'
    true
  else
    puts "Existing LHM backup tables: #{lhm_tables.join(', ')}."
    puts "Existing LHM triggers: #{lhm_triggers.join(', ')}."
    puts 'Run Lhm.cleanup(true) to drop them all.'
    false
  end
end