Class: Flydata::SourcePostgresql::QueryBasedSync::ResourceRequester

Inherits:
QueryBasedSync::ResourceRequester show all
Defined in:
lib/flydata/source_postgresql/query_based_sync/resource_requester.rb

Constant Summary collapse

RESPONSE_CLASS =
Flydata::SourcePostgresql::QueryBasedSync::Response

Instance Attribute Summary

Attributes inherited from QueryBasedSync::ResourceRequester

#context

Instance Method Summary collapse

Methods inherited from QueryBasedSync::ResourceRequester

#each_response, #start

Constructor Details

#initialize(*args) ⇒ ResourceRequester

Returns a new instance of ResourceRequester.



13
14
15
16
# File 'lib/flydata/source_postgresql/query_based_sync/resource_requester.rb', line 13

def initialize(*args)
  super
  @tables_missing_meta = []
end

Instance Method Details

#create_resource_client(context) ⇒ Object

Override



19
20
21
# File 'lib/flydata/source_postgresql/query_based_sync/resource_requester.rb', line 19

def create_resource_client(context)
  FlydataCore::Postgresql::PGClient.new(context.dbconf)
end

#fetch_responses_once(table_name, latest_src_pos) ⇒ Object

Override



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
49
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
# File 'lib/flydata/source_postgresql/query_based_sync/resource_requester.rb', line 24

def fetch_responses_once(table_name, latest_src_pos)
  table_sym = table_name.to_sym
  t_meta = context.table_meta[table_sym]

  if t_meta.nil? || t_meta.empty?
    unless @tables_missing_meta.include?(table_name)
      @tables_missing_meta << table_name
      log_error "No metadata is available for table '#{table_name}'.  Make sure the table exists and is visible to the PostgreSQL user."
    end
    return
  end

  table_src_pos = context.table_src_pos_files[table_sym].pos
  master_src_pos = context.cur_src_pos_file.pos

  # Set from_sid, to_sid and pk_values

  to_sid = latest_src_pos.snapshot
  pk_values = nil

  # - table.binlog.pos does not exist
  if table_src_pos.nil?
    from_sid = master_src_pos.snapshot
  # - pk_values exists -> continue from the last transaction
  elsif table_src_pos.pk_values
    from_sid = table_src_pos.snapshot
    to_sid = table_src_pos.to_snapshot
    pk_values = table_src_pos.pk_values
  else
    from_sid = table_src_pos.snapshot
  end

  if from_sid == to_sid
    log_debug("Skip fetching records (No changes)")
    return
  end

  log_debug("Start query", from_sid: from_sid, to_sid: to_sid, pk_values: pk_values)

  # Create and execute query
  query = generate_query(t_meta, from_sid, to_sid, pk_values)

  begin
    result = @resource_client.query(query)
  rescue PG::TRSerializationFailure => e
    raise FlydataCore::RetryableError.new(e)
  end

  # Create response object based on result
  responses = build_responses(table_name, result, from_sid: from_sid, to_sid: to_sid, pk_values: pk_values)

  Array(responses)
end