Class: RR::SyncHelper
- Inherits:
-
Object
- Object
- RR::SyncHelper
- Includes:
- LogHelper
- Defined in:
- lib/rubyrep/sync_helper.rb
Overview
Provides helper functionality for the table syncers. The methods exposed by this class are intended to provide a stable interface for third party syncers.
Instance Attribute Summary collapse
-
#table_sync ⇒ Object
The current
TableSync
instance.
Instance Method Summary collapse
-
#delete_record(database, table, values) ⇒ Object
Delegates to Committers::BufferedCommitter#delete_record.
-
#ensure_event_log ⇒ Object
Checks if the event log table already exists and creates it if necessary.
-
#extract_key(row) ⇒ Object
Given a column_name => value hash of a full row, returns a column_name => value hash of the primary key columns.
-
#finalize(success = true) ⇒ Object
Asks the committer (if it exists) to finalize any open transactions
success
should be true if there were no problems, false otherwise. -
#initialize(table_sync) ⇒ SyncHelper
constructor
Creates a new SyncHelper for the given
TableSync
instance. -
#insert_record(database, table, values) ⇒ Object
Delegates to Committers::BufferedCommitter#insert_record.
-
#left_table ⇒ Object
Name of the left table.
-
#log_sync_outcome(row, type, outcome, details = nil) ⇒ Object
Logs the outcome of a replication into the replication log table.
-
#right_table ⇒ Object
Name of the right table.
-
#session ⇒ Object
The active
Session
. -
#sync_options ⇒ Object
Sync options for the current table sync.
-
#tables ⇒ Object
A hash with :
left
: name of the table in the left database :right
: name of the table in the right database. -
#update_record(database, table, values, old_key = nil) ⇒ Object
Delegates to Committers::BufferedCommitter#update_record.
Methods included from LogHelper
Constructor Details
#initialize(table_sync) ⇒ SyncHelper
Creates a new SyncHelper for the given TableSync
instance.
117 118 119 |
# File 'lib/rubyrep/sync_helper.rb', line 117 def initialize(table_sync) self.table_sync = table_sync end |
Instance Attribute Details
#table_sync ⇒ Object
The current TableSync
instance
11 12 13 |
# File 'lib/rubyrep/sync_helper.rb', line 11 def table_sync @table_sync end |
Instance Method Details
#delete_record(database, table, values) ⇒ Object
Delegates to Committers::BufferedCommitter#delete_record
51 52 53 |
# File 'lib/rubyrep/sync_helper.rb', line 51 def delete_record(database, table, values) committer.delete_record(database, tables[database], values) end |
#ensure_event_log ⇒ Object
Checks if the event log table already exists and creates it if necessary
66 67 68 69 70 71 |
# 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 |
#extract_key(row) ⇒ Object
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
33 34 35 |
# File 'lib/rubyrep/sync_helper.rb', line 33 def extract_key(row) row.reject {|column, value| not primary_key_names.include? column } end |
#finalize(success = true) ⇒ Object
Asks the committer (if it exists) to finalize any open transactions success
should be true if there were no problems, false otherwise.
112 113 114 |
# File 'lib/rubyrep/sync_helper.rb', line 112 def finalize(success = true) @committer.finalize(success) if @committer end |
#insert_record(database, table, values) ⇒ Object
Delegates to Committers::BufferedCommitter#insert_record
41 42 43 |
# File 'lib/rubyrep/sync_helper.rb', line 41 def insert_record(database, table, values) committer.insert_record(database, tables[database], values) end |
#left_table ⇒ Object
Name of the left table
17 |
# File 'lib/rubyrep/sync_helper.rb', line 17 def left_table; table_sync.left_table; end |
#log_sync_outcome(row, type, outcome, details = nil) ⇒ Object
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
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# 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 "#{[: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 |
#right_table ⇒ Object
Name of the right table
20 |
# File 'lib/rubyrep/sync_helper.rb', line 20 def right_table; table_sync.right_table; end |
#session ⇒ Object
The active Session
14 |
# File 'lib/rubyrep/sync_helper.rb', line 14 def session; table_sync.session; end |
#sync_options ⇒ Object
Sync options for the current table sync
38 |
# File 'lib/rubyrep/sync_helper.rb', line 38 def ; @sync_options ||= table_sync.; end |
#tables ⇒ Object
A hash with :left
: name of the table in the left database :right
: name of the table in the right database
25 26 27 |
# File 'lib/rubyrep/sync_helper.rb', line 25 def tables @tables ||= {:left => left_table, :right => right_table} end |
#update_record(database, table, values, old_key = nil) ⇒ Object
Delegates to Committers::BufferedCommitter#update_record
46 47 48 |
# 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 |