Sha256: 8561d6dd98f089431d11c16355d08c3085bde6e352af64a92dd667d519e80972

Contents?: true

Size: 976 Bytes

Versions: 1

Compression:

Stored size: 976 Bytes

Contents

require 'csv'

module PostgresUpsert
  class ModelToModelAdapter
    def initialize(destination_model, source_model, options = {})
      @destination_model = destination_model
      @source_model = source_model
      @options = options
    end

    def write
        source_table = @source_model.table_name
        source_conn = @source_model.connection.raw_connection

        to_stdout_sql = "COPY #{source_table} TO STDOUT"
      
        csv_string = CSV.generate do |csv|
          csv << @source_model.column_names # CSV header row
          source_conn.copy_data(to_stdout_sql) do
            while (line = source_conn.get_copy_data) do
              csv << line.split("\t")
            end
          end
        end
        io = StringIO.new(csv_string)
        Writer.new(@destination_model, io, @options).write
    end
  
  private

    def get_columns
      # columns_list = @options[:columns] 
      # columns_list ||= 
      @source.column_names
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
postgres_upsert-5.1.0 lib/postgres_upsert/model_to_model_adapter.rb