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
methods[1].
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
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
: aSyncHelper
through which the database can be accessedtype
: type of sync difference (:left
,:right
or:conflict
)row
:- for
:left
or:right
differences: 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
: aReplicationHelper
enabling database accessdiff
: theReplicationDifference
describing 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
...
[1] It is OK to only implement one of the methods.