Class: PostgresUpsert::ReadAdapters::ActiveRecordAdapter

Inherits:
Object
  • Object
show all
Defined in:
lib/postgres_upsert/read_adapters/active_record_adapter.rb

Instance Method Summary collapse

Constructor Details

#initialize(source, options) ⇒ ActiveRecordAdapter

Returns a new instance of ActiveRecordAdapter.



4
5
6
7
# File 'lib/postgres_upsert/read_adapters/active_record_adapter.rb', line 4

def initialize(source, options)
  @options = sanitize_options(options)
  @source = source
end

Instance Method Details

#columnsObject



32
33
34
# File 'lib/postgres_upsert/read_adapters/active_record_adapter.rb', line 32

def columns
  @source.column_names
end

#continuous_write_enabledObject



15
16
17
# File 'lib/postgres_upsert/read_adapters/active_record_adapter.rb', line 15

def continuous_write_enabled
  false
end

#gets {|line| ... } ⇒ Object

Yields:

  • (line)


19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/postgres_upsert/read_adapters/active_record_adapter.rb', line 19

def gets(&block)
  batch_size = 1_000
  line = ""
  conn = @source.connection.raw_connection

  conn.copy_data("COPY #{@source.table_name} TO STDOUT") do
    while (line_read = conn.get_copy_data) do
      line << line_read
    end
  end
  yield line
end

#sanitize_options(options) ⇒ Object



9
10
11
12
13
# File 'lib/postgres_upsert/read_adapters/active_record_adapter.rb', line 9

def sanitize_options(options)
  options.slice(
    :columns, :map, :unique_key
  )
end