# File lib/rubyrep/sync_helper.rb, line 74 def primary_key_names @primary_key_names ||= session.left.primary_key_names(left_table) end
class RR::SyncHelper
Provides helper functionality for the table syncers. The methods exposed by this class are intended to provide a stable interface for third party syncers.
Attributes
The current TableSync
instance
Public Class Methods
Creates a new SyncHelper
for the given TableSync
instance.
# File lib/rubyrep/sync_helper.rb, line 117 def initialize(table_sync) self.table_sync = table_sync end
Public Instance Methods
Delegates to Committers::BufferedCommitter#delete_record
# File lib/rubyrep/sync_helper.rb, line 51 def delete_record(database, table, values) committer.delete_record(database, tables[database], values) end
Checks if the event log table already exists and creates it if necessary
# File lib/rubyrep/sync_helper.rb, line 66 def ensure_event_log unless @ensured_event_log ReplicationInitializer.new(session).ensure_event_log @ensured_event_log = true end end
Given a column_name => value hash of a full row, returns a column_name => value hash of the primary key columns.
-
row
: the full row
Returns
# File lib/rubyrep/sync_helper.rb, line 33 def extract_key(row) row.reject {|column, value| not primary_key_names.include? column } end
Asks the committer (if it exists) to finalize any open transactions success
should be true if there were no problems, false otherwise.
# File lib/rubyrep/sync_helper.rb, line 112 def finalize(success = true) @committer.finalize(success) if @committer end
Delegates to Committers::BufferedCommitter#insert_record
# File lib/rubyrep/sync_helper.rb, line 41 def insert_record(database, table, values) committer.insert_record(database, tables[database], values) end
Name of the left table
# File lib/rubyrep/sync_helper.rb, line 17 def left_table; table_sync.left_table; end
Logs the outcome of a replication into the replication log table.
-
row
: a column_name => value hash for at least the primary keys of the record -
type
: string describing the type of the sync -
outcome
: string describing what's done about the sync -
details
: string with further details regarding the sync
# File lib/rubyrep/sync_helper.rb, line 84 def log_sync_outcome(row, type, outcome, details = nil) ensure_event_log if primary_key_names.size == 1 key = row[primary_key_names[0]] else key_parts = primary_key_names.map do |column_name| %Q("#{column_name}"=>#{row[column_name].to_s.inspect}) end key = key_parts.join(', ') end sync_outcome, sync_details = fit_description_columns(outcome, details) session.left.insert_record "#{sync_options[:rep_prefix]}_logged_events", { :activity => 'sync', :change_table => left_table, :diff_type => type.to_s, :change_key => key, :left_change_type => nil, :right_change_type => nil, :description => sync_outcome, :long_description => sync_details, :event_time => Time.now, :diff_dump => nil } end
Name of the right table
# File lib/rubyrep/sync_helper.rb, line 20 def right_table; table_sync.right_table; end
The active Session
# File lib/rubyrep/sync_helper.rb, line 14 def session; table_sync.session; end
Sync options for the current table sync
# File lib/rubyrep/sync_helper.rb, line 38 def sync_options; @sync_options ||= table_sync.sync_options; end
A hash with :left
: name of the table in the left database :right
: name of the table in the right database
# File lib/rubyrep/sync_helper.rb, line 25 def tables @tables ||= {:left => left_table, :right => right_table} end
Delegates to Committers::BufferedCommitter#update_record
# File lib/rubyrep/sync_helper.rb, line 46 def update_record(database, table, values, old_key = nil) committer.update_record(database, tables[database], values, old_key) end
Private Instance Methods
Return the committer, creating it if not yet there.
# File lib/rubyrep/sync_helper.rb, line 56 def committer unless @committer committer_class = Committers::committers[sync_options[:committer]] @committer = committer_class.new(session) end @committer end
Returns an array of primary key names for the synced table