Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- begin
- mysql = DBI.connect("DBI:Mysql:source:localhost", "username", "password")
- pg = DBI.connect("DBI:Pg:destination:localhost", "username", "password")
- mysql.execute("SET NAMES 'UTF8'")
- mysql.select_all("SHOW TABLES") do |table|
- next if ['schema_migrations', 'sessions'].include?(table.to_s)
- select = mysql.execute("SELECT * FROM #{table}")
- columns = select.column_names.map { |key| "\"#{key}\"" }.join(', ')
- insert = pg.prepare("INSERT INTO #{table} (#{columns}) VALUES(#{(['?'] * select.column_names.size).join(',')})")
- select.each do |row|
- insert.execute(*row)
- puts "Inserted ID #{row['id']} into #{table}"
- end
- insert.finish
- pg.execute("SELECT setval('#{table}_id_seq', max(id), true) FROM #{table}") if select.column_names.include?('id')
- end
- rescue DBI::DatabaseError => e
- puts "Error #{e.err}: #{e.errstr}"
- ensure
- mysql.disconnect if mysql
- pg.disconnect if pg
- end
Add Comment
Please, Sign In to add comment