lib/lhm.rb in lhm-2.1.0 vs lib/lhm.rb in lhm-2.2.0

- old
+ new

@@ -49,13 +49,26 @@ block.call(invoker.migrator) invoker.run(options) true end - def cleanup(run = false) - lhm_tables = connection.select_values("show tables").select { |name| name =~ /^lhm(a|n)_/ } - lhm_triggers = connection.select_values("show triggers").collect do |trigger| + # Cleanup tables and triggers + # + # @param [Boolean] run execute now or just display information + # @param [Hash] options Optional options to alter cleanup behaviour + # @option options [Time] :until + # Filter to only remove tables up to specified time (defaults to: nil) + 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| @@ -64,16 +77,16 @@ 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." + 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." + 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 def setup(adapter) @@ -105,7 +118,6 @@ protected def connection Connection.new(adapter) end - end