Product: | Syniti Data Replication, DBMoto |
Version: | All |
ID: | 1638 |
Summary: | Handling column value conflicts in a "count" column via a script to total the values in source and target columns |
During synchronization, there may be cases where you do not want a column on either the source or target table to "win" in case of a conflict, but you want the final value in both source and target columns to to reflect the increments applied to both the source and target fields. For example, if both the source and target tables contain a Count column, the desired result during synchronization would be to figure out the increase in the count in both the source and target Count columns, add those to the original value, then put the correct value in both columns. This can be accomplished by writing a script to use in the conflict resolution process. However, because the script uses the IRecord method GetValueBefore, this script will work only with databases that report column values before changes apply. (Oracle does not support this, and Microsoft SQL Server supports this only when performing trigger-based replication in DBMoto.)
To set up a script that will sum column values during conflict resolution:
- In the Management Center, select the replication.
- From the right mouse button menu, choose Replication Properties.
- In the Replication Properties dialog Preferences tab, set the Conflict Resolver property to UseScript.
- In the Replication Properties General tab, check the Use Script option.
- Click Script to open the Replication Script Editor.
- In the Replication Script Editor, choose Replication from the drop-down list on the left side.
- Choose onConflict from the drop-down list on the right side.
- In the editor, type your function after the line:
Public Overrides Function Replication_onConflict(recSource As IRecord, recTarget As IRecord) As IRecord
It should be similar to the example function below which accesses the value of the COUNT column before the update, and the value of the COUNT column after the update for both the source and target tables. It then calculates a final count by finding the delta on the source, the delta on the target, then adding those together with the original value on both source and target. The final value is set in the source column and that value is then replicated to the target column without conflict.
Starting value Source col: 1
New value Source col: 2
Starting value Target col: 1
New value Target col: 3
<Apply script below >
Ending Source col value: (2 - 1) + (3 - 1) + 1 = 4
<Value automatically applied to Target col.>
Public Overrides Function Replication_onConflict(recSource As IRecord, recTarget As IRecord) As IRecord
Dim objID As Object
Dim objSrcBCount As Object
Dim objSrcACount As Object
Dim objTarBCount As Object
Dim objTarACount As Object
Dim objTotalCount As Object
Dim TotalCount As Integer
If (recSource.OperationType = enmOperationType.Update) And (recTarget.OperationType = enmOperationType.Update) Then
objSrcBCount = recSource.GetValueBefore("COUNT")
objSrcACount = recSource.GetValueAfter("COUNT")
objTarBCount = recTarget.GetValueBefore("COUNT")
objTarACount = recTarget.GetValueAfter("COUNT")
TotalCount = (objSrcACount - objSrcBCount) + (objTarACount - objTarBCount) + objSrcBCount
recSource.SetValueAfter("COUNT", TotalCount)
Return recSource
Else
addlog ("Operation type is not an update", 0)
End If
End Function
- Test compile the script to make sure there are no errors.
- Click OK to close the Replication Script Editor.
- Click OK to close the Replication Properties dialog.
- Test the script by running the replication and making updates on the columns involved within the same time interval.