Guest User

Untitled

a guest
Apr 25th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.91 KB | None | 0 0
  1. begin
  2. mysql = DBI.connect("DBI:Mysql:source:localhost", "username", "password")
  3. pg = DBI.connect("DBI:Pg:destination:localhost", "username", "password")
  4. mysql.execute("SET NAMES 'UTF8'")
  5.  
  6. mysql.select_all("SHOW TABLES") do |table|
  7. next if ['schema_migrations', 'sessions'].include?(table.to_s)
  8. select = mysql.execute("SELECT * FROM #{table}")
  9. columns = select.column_names.map { |key| "\"#{key}\"" }.join(', ')
  10. insert = pg.prepare("INSERT INTO #{table} (#{columns}) VALUES(#{(['?'] * select.column_names.size).join(',')})")
  11. select.each do |row|
  12. insert.execute(*row)
  13. puts "Inserted ID #{row['id']} into #{table}"
  14. end
  15. insert.finish
  16. pg.execute("SELECT setval('#{table}_id_seq', max(id), true) FROM #{table}") if select.column_names.include?('id')
  17. end
  18.  
  19. rescue DBI::DatabaseError => e
  20. puts "Error #{e.err}: #{e.errstr}"
  21. ensure
  22. mysql.disconnect if mysql
  23. pg.disconnect if pg
  24. end
Add Comment
Please, Sign In to add comment