Skip to content

Instantly share code, notes, and snippets.

@robworley
Created May 20, 2011 20:24
Show Gist options
  • Save robworley/983727 to your computer and use it in GitHub Desktop.
Save robworley/983727 to your computer and use it in GitHub Desktop.
Stream rows from one PostgreSQL database to another, eliminating the majority of ActiveRecord and SQL overhead
def clone_data(src_conn, dest_conn, table_name, options = {})
puts "Cloning #{table_name} data"
copyable = if options[:conditions]
"(SELECT * FROM #{table_name} WHERE #{options[:conditions]})"
else
table_name
end
copy_command = "COPY #{copyable} TO STDOUT"
src_conn.raw_connection.exec(copy_command)
dest_conn.raw_connection.exec("COPY #{table_name} FROM STDIN")
row_count = 0
while row = src_conn.raw_connection.get_copy_data do
dest_conn.raw_connection.put_copy_data(row)
row_count += 1
end
dest_conn.raw_connection.put_copy_end
puts "=> #{row_count} rows"
end
src_conn = SomeActiveRecordModel.connection
dest_conn = SomeOtherActiveRecordModel.connection
clone_data(src_conn, dest_conn, :schema_migrations)
clone_data(src_conn, dest_conn, :users, :conditions => "status = 'Active'")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment