Created
March 11, 2010 01:33
-
-
Save caironoleto/328701 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def interesting_tables | |
ActiveRecord::Base.connection.tables | |
end | |
class BackupDB < ActiveRecord::Base | |
end | |
destination_db = 'new' | |
BackupDB.establish_connection destination_db.intern | |
interesting_tables.each do |tbl| | |
unless tbl =~ /schema_mi/ | |
klass = tbl.classify.constantize | |
BackupDB.connection.execute "DELETE FROM #{tbl} where id > 0" | |
puts "Writing #{tbl}..." | |
klass.find_in_batches do |data| | |
data.each_with_index do |datum, i| | |
puts "-- #{tbl} record #{i}" | |
BackupDB.connection.execute "INSERT INTO #{tbl} (`#{datum.attributes.keys.join("`,`")}`) VALUES (#{datum.attributes.values.collect { |value| BackupDB.connection.quote(value) }.join(",")})" | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment