lib/lhm.rb in lhm-1.2.0 vs lib/lhm.rb in lhm-1.3.0
- old
+ new
@@ -32,20 +32,35 @@
# to set this option (see SqlHelper#supports_atomic_switch?)
# @yield [Migrator] Yielded Migrator object records the changes
# @return [Boolean] Returns true if the migration finishes
# @raise [Error] Raises Lhm::Error in case of a error and aborts the migration
def self.change_table(table_name, options = {}, &block)
- connection = Connection.new(adapter)
-
origin = Table.parse(table_name, connection)
invoker = Invoker.new(origin, connection)
block.call(invoker.migrator)
invoker.run(options)
true
end
+ def self.cleanup(run = false)
+ lhm_tables = connection.select_values("show tables").select do |name|
+ name =~ /^lhm(a|n)_/
+ end
+ return true if lhm_tables.empty?
+ if run
+ lhm_tables.each do |table|
+ connection.execute("drop table #{table}")
+ end
+ true
+ else
+ puts "Existing LHM backup tables: #{lhm_tables.join(", ")}."
+ puts "Run Lhm.cleanup(true) to drop them all."
+ false
+ end
+ end
+
def self.setup(adapter)
@@adapter = adapter
end
def self.adapter
@@ -53,6 +68,12 @@
begin
raise 'Please call Lhm.setup' unless defined?(ActiveRecord)
ActiveRecord::Base.connection
end
end
+
+ protected
+ def self.connection
+ Connection.new(adapter)
+ end
+
end