Class: Multiwoven::Integrations::Destination::Postgresql::Client

Inherits:
DestinationConnector
  • Object
show all
Defined in:
lib/multiwoven/integrations/destination/postgresql/client.rb

Constant Summary collapse

MAX_CHUNK_SIZE =
10_000

Instance Method Summary collapse

Instance Method Details

#check_connection(connection_config) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/multiwoven/integrations/destination/postgresql/client.rb', line 11

def check_connection(connection_config)
  connection_config = connection_config.with_indifferent_access
  create_connection(connection_config)
  ConnectionStatus.new(
    status: ConnectionStatusType["succeeded"]
  ).to_multiwoven_message
rescue PG::Error => e
  ConnectionStatus.new(
    status: ConnectionStatusType["failed"], message: e.message
  ).to_multiwoven_message
end

#discover(connection_config) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/multiwoven/integrations/destination/postgresql/client.rb', line 23

def discover(connection_config)
  connection_config = connection_config.with_indifferent_access
  query = "SELECT table_name, column_name,
           CASE WHEN data_type = 'USER-DEFINED' THEN udt_name ELSE data_type END
           AS data_type,
           is_nullable
           FROM information_schema.columns
           WHERE table_schema = '#{connection_config[:schema]}' AND table_catalog = '#{connection_config[:database]}'
           ORDER BY table_name, ordinal_position;"

  db = create_connection(connection_config)
  records = db.exec(query) do |result|
    result.map do |row|
      row
    end
  end
  catalog = Catalog.new(streams: create_streams(records))
  catalog.to_multiwoven_message
rescue StandardError => e
  handle_exception(e, {
                     context: "POSTGRESQL:DISCOVER:EXCEPTION",
                     type: "error"
                   })
ensure
  db&.close
end

#write(sync_config, records, action = "destination_insert") ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/multiwoven/integrations/destination/postgresql/client.rb', line 50

def write(sync_config, records, action = "destination_insert")
  connection_config = sync_config.destination.connection_specification.with_indifferent_access
  raw_table = sync_config.stream.name
  table_name = qualify_table(connection_config[:schema], raw_table)
  db = create_connection(connection_config)
  primary_key = fetch_primary_key(db, connection_config[:schema], raw_table)

  write_success = 0
  write_failure = 0
  log_message_array = []

  records.each_slice(MAX_CHUNK_SIZE) do |chunk|
    bulk_write(db, table_name, chunk, primary_key, action)
    write_success += chunk.size
    log_message_array << log_request_response("info", "bulk_#{action}", "#{chunk.size} rows")
  rescue StandardError => e
    logger.warn("POSTGRESQL:BULK_WRITE:FALLBACK chunk_size=#{chunk.size} error=#{e.message}")
    chunk.each do |record|
      response = bulk_write(db, table_name, [record], primary_key, action)
      write_success += 1
      log_message_array << log_request_response("info", "fallback_#{action}", response)
    rescue StandardError => individual_error
      handle_exception(individual_error, {
                         context: "POSTGRESQL:RECORD:WRITE:EXCEPTION",
                         type: "error",
                         sync_id: sync_config.sync_id,
                         sync_run_id: sync_config.sync_run_id
                       })
      write_failure += 1
      log_message_array << log_request_response("error", "fallback_#{action}", individual_error.message)
    end
  end

  tracking_message(write_success, write_failure, log_message_array)
rescue StandardError => e
  handle_exception(e, {
                     context: "POSTGRESQL:RECORD:WRITE:EXCEPTION",
                     type: "error",
                     sync_id: sync_config.sync_id,
                     sync_run_id: sync_config.sync_run_id
                   })
ensure
  db&.close
end