Event filters
Event filters can be used to
- filter which unsynced / unreplicated differences should be processed by rubyrep
- implement custom sync / replication actions
This is done by assigning the custom event filter to the (table specific) option :event_filter.
An event filter implements the before_sync and before_replicate methods1.
The before_sync / before_replicate methods
- are called for each each detected sync / replication difference
- must return true for rubyrep to proceed with syncing / replicating the difference
- can use the calling parameters to access both databases for custom actions
1 It is OK to only implement one of the methods.
Sync filter
before_sync is called with
table: name of the left table (of the sync difference)key: the column_name => value hash of the primary key of the sync differencesync_helper: aSyncHelperthrough which the database can be accessedtype: type of sync difference (:left,:rightor:conflict)row:- for
:leftor:rightdifferences: the column_name => value pairs of the unsynced row - for
:conflict: an array of conflicting left and right row
- for
Replication filter
before_replicate is called with
table: name of the left table (of the unreplicated change)key: the column_name => value hash of the primary key of the changed rowreplication_helper: aReplicationHelperenabling database accessdiff: theReplicationDifferencedescribing the unreplicated change.
Example
The following simple example configures rubyrep to sync / replicate only accounts with a primary key < 100:
...
class AccountFilter
def before_sync(table, key, sync_helper, type, row)
key['id'] < 100
end
def before_replicate(table, key, replication_helper, diff)
key['id'] < 100
end
end
config.add_table_option 'accounts', :event_filter => AccountFilter.new
...