Sha256: bd9dd919b63ca460ea57f79936f76716ae657b5ce17c1d164abe6d2707342b3e

Contents?: true

Size: 2 KB

Versions: 10

Compression:

Stored size: 2 KB

Contents

module RR
  
  # Synchronizes the data of two tables.
  class TableSync < TableScan

    # Instance of SyncHelper
    attr_accessor :helper

    # Returns a hash of sync options for this table sync.
    def sync_options
      @sync_options ||= session.configuration.options_for_table(left_table)
    end
    
    # 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
    def initialize(session, left_table, right_table = nil)
      super
    end

    # Executes the specified sync hook
    # * +hook_id+: either :+before_table_sync+ or :+after_table_sync+
    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

    # 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.
    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
        syncer.sync_difference type, row
      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
    
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
rubyrep-1.0.9 lib/rubyrep/table_sync.rb
rubyrep-1.0.8 lib/rubyrep/table_sync.rb
rubyrep-1.0.3 lib/rubyrep/table_sync.rb
rubyrep-1.0.4 lib/rubyrep/table_sync.rb
rubyrep-1.0.5 lib/rubyrep/table_sync.rb
rubyrep-1.0.6 lib/rubyrep/table_sync.rb
rubyrep-1.0.7 lib/rubyrep/table_sync.rb
rubyrep-1.0.0 lib/rubyrep/table_sync.rb
rubyrep-1.0.1 lib/rubyrep/table_sync.rb
rubyrep-1.0.2 lib/rubyrep/table_sync.rb