Skip to content

Instantly share code, notes, and snippets.

@dannluciano
Forked from caironoleto/sqlite_to_mysql.rb
Created March 12, 2010 02:10
Show Gist options
  • Save dannluciano/329969 to your computer and use it in GitHub Desktop.
Save dannluciano/329969 to your computer and use it in GitHub Desktop.
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