Class: RR::TableSync

Inherits:
TableScan show all
Defined in:
lib/rubyrep/table_sync.rb

Overview

Synchronizes the data of two tables.

Instance Attribute Summary collapse

Attributes inherited from TableScan

#left_table, #primary_key_names, #progress_printer, #right_table, #session

Instance Method Summary collapse

Methods inherited from TableScan

#scan_options, #update_progress

Methods included from TableScanHelper

#rank_rows, scan_class

Constructor Details

#initialize(session, left_table, right_table = nil) ⇒ TableSync

Creates a new TableSync instance

* session: a Session object representing the current database session
* left_table: name of the table in the left database
* right_table: name of the table in the right database. If not given, same like left_table


18
19
20
# File 'lib/rubyrep/table_sync.rb', line 18

def initialize(session, left_table, right_table = nil)
  super
end

Instance Attribute Details

#helperObject

Instance of SyncHelper



7
8
9
# File 'lib/rubyrep/table_sync.rb', line 7

def helper
  @helper
end

Instance Method Details

#event_filtered?(type, row) ⇒ Boolean

Calls the event filter for the give table difference.

  • type: type of difference

  • row: the differing row

Refer to DirectTableScan#run for full description of type and row. Returns true if syncing of the difference should not proceed.

Returns:

  • (Boolean)


42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/rubyrep/table_sync.rb', line 42

def event_filtered?(type, row)
  event_filter = sync_options[:event_filter]
  if event_filter && event_filter.respond_to?(:before_sync)
    not event_filter.before_sync(
      helper.left_table,
      helper.extract_key([row].flatten.first),
      helper,
      type,
      row
    )
  else
    false
  end
end

#execute_sync_hook(hook_id) ⇒ Object

Executes the specified sync hook

  • hook_id: either :before_table_sync or :after_table_sync



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/rubyrep/table_sync.rb', line 24

def execute_sync_hook(hook_id)
  hook = sync_options[hook_id]
  if hook
    if hook.respond_to?(:call)
      hook.call(helper)
    else
      [:left, :right].each do |database|
        session.send(database).execute hook
      end
    end
  end
end

#runObject

Executes the table sync. If a block is given, yields each difference with the following 2 parameters

  • type

  • row

Purpose: enable display of progress information. See DirectTableScan#run for full description of yielded parameters.



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
# File 'lib/rubyrep/table_sync.rb', line 63

def run
  success = false

  scan_class = TableScanHelper.scan_class(session)
  scan = scan_class.new(session, left_table, right_table)
  scan.progress_printer = progress_printer

  self.helper = SyncHelper.new(self)
  syncer = Syncers.configured_syncer(sync_options).new(helper)

  execute_sync_hook :before_table_sync

  scan.run do |type, row|
    yield type, row if block_given? # To enable progress reporting
    unless event_filtered?(type, row)
      syncer.sync_difference type, row
    end
  end
  
  execute_sync_hook :after_table_sync

  success = true # considered to be successful if we get till here
ensure
  helper.finalize success if helper
end

#sync_optionsObject

Returns a hash of sync options for this table sync.



10
11
12
# File 'lib/rubyrep/table_sync.rb', line 10

def sync_options
  @sync_options ||= session.configuration.options_for_table(left_table)
end