Class: PostgresUpsert::Writer

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

Direct Known Subclasses

TableWriter

Instance Method Summary collapse

Constructor Details

#initialize(klass, destination, source, options = {}) ⇒ Writer

Returns a new instance of Writer.



4
5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/postgres_upsert/writer.rb', line 4

def initialize(klass, destination, source, options = {})
  @klass = klass
  @destination = destination
  @source = source
  @options = options.reverse_merge({
    delimiter: ',',
    header: true,
    unique_key: [primary_key],
    update_only: false
  })
  @source = source
  @options[:unique_key] = Array.wrap(@options[:unique_key])

end

Instance Method Details

#writeObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/postgres_upsert/writer.rb', line 19

def write
  validate_options

  
  create_temp_table

  if @source.continuous_write_enabled
    write_continuous
  else
    write_batched
  end

  upsert_from_temp_table
  drop_temp_table

  summarize_results
end