No release in over 3 years
Low commit activity in last 3 years
Supports binary COPY into PostgreSQL with activerecord
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

>= 2.12.0
>= 2.12.0

Runtime

 Project Readme

activerecord-copy

Library to assist using binary COPY into PostgreSQL with activerecord.

Binary copy functionality is based on pg_data_encoder, but modified to support additional types, and to prefer column type specifications over inferred data types.

COPY is the most efficient way to bulk-load data into Postgres, since it doesn't have to wait for network round trips between individual rows (amongst other benefits), and using binary encoding instead of text encoding avoids any processing overhead on the database side when parsing the rows.

Installation

Add this line to your application's Gemfile:

gem 'activerecord-copy'

Usage

Once you've included the library in your Gemfile, it will automatically add the copy_from_client method to ActiveRecord::Base, and therefore to all your model classes. You can use it like this:

class MyModel < ApplicationRecord
end

my_data = [
  { field_1: 'abc', field_2: 'def' },
  { field_1: 'foo', field_2: 'bar' },
]

MyModel.copy_from_client [:field_1, :field_2] do |copy|
  my_data.each do |d|
    copy << [d[:field_1], d[:field_2]]
  end
end

MyModel.find_by(field_1: 'abc')

Authors

Credits to Pete Brumm who wrote pg_data_encoder and which this library repurposes.

LICENSE

Copyright (c) 2018, Lukas Fittl lukas@fittl.com
activerecord-copy is licensed under the MIT license, see LICENSE file for details.

pg_data_encoder is Copyright (c) 2012, Pete Brumm
pg_data_encoder is included under the terms of the MIT license, see LICENSE file for details.