You are here: Advanced Replication Strategies > Prepare Old Databases for Replication

Prepare Old Databases for Replication

Sometimes replication is a new requirement for a existing application. Therefore existing databases have to be replicated. However the required UUIDs and timestamps haven't been enabled so far. How do you migrate the older databases to enable replication?

The first step is to enable the UUIDs and timestamps.

configuration.File.GenerateUUIDs = ConfigScope.Globally;
configuration.File.GenerateCommitTimestamps = true;
Db4oReplicationExamples.cs: Configure db4o to generate UUIDs and commit timestamps
configuration.File.GenerateUUIDs = ConfigScope.Globally
configuration.File.GenerateCommitTimestamps = True
Db4oReplicationExamples.vb: Configure db4o to generate UUIDs and commit timestamps

The next issue is that the all objects stored don't have a UUID nor a version numbers. The easiest way to have such a number is to load and update each object in the database. Because when you update a object the new configuration takes effect.

IList<object> allObjects = desktopDatabase.Query<object>();
foreach (object objectToUpdate in allObjects)
{
    desktopDatabase.Store(objectToUpdate);
}
desktopDatabase.Commit();
AdvancedReplicationExamples.cs: Updating all objects ensures that it has a UUID and timestamp
Dim allObjects As IList(Of Object) = desktopDatabase.Query(Of Object)()
For Each objectToUpdate As Object In allObjects
    desktopDatabase.Store(objectToUpdate)
Next
desktopDatabase.Commit()
AdvancedReplicationExamples.vb: Updating all objects ensures that it has a UUID and timestamp

Updating all object is maybe not an option. It's also possible to do this during the first replication. First query the objects which are target of the replication. Then do the replication. When a object doesn't have a UUID yet, update the object to ensure it has an UUID. Make also sure that the update-depth is high enough so that child objects also have an UUID.

IReplicationSession replicationSession = Replication.Begin(desktopRelicationPartner, mobileRelicationPartner);
IList<Car> initialReplication = desktopDatabase.Query<Car>();

foreach (Car changedObjectOnDesktop in initialReplication)
{
    IObjectInfo infoAboutObject = desktopDatabase.Ext().GetObjectInfo(changedObjectOnDesktop);
    if (null == infoAboutObject.GetUUID())
    {
        desktopDatabase.Ext().Store(changedObjectOnDesktop, 2);
    }
    replicationSession.Replicate(changedObjectOnDesktop);
}
replicationSession.Commit();
AdvancedReplicationExamples.cs: Migrate on the fly
Dim replicationSession As IReplicationSession = _
    Replication.Begin(desktopDatabase, mobileDatabase)
Dim initialReplication As IList(Of Car) = desktopDatabase.Query(Of Car)()

For Each changedObjectOnDesktop As Car In initialReplication
    Dim infoAboutObject As IObjectInfo = desktopDatabase.Ext() _
        .GetObjectInfo(changedObjectOnDesktop)
    If infoAboutObject.GetUUID() Is Nothing Then
        desktopDatabase.Ext().Store(changedObjectOnDesktop, 2)
    End If
    replicationSession.Replicate(changedObjectOnDesktop)
Next
replicationSession.Commit()
AdvancedReplicationExamples.vb: Migrate on the fly