You can download the code for this example here:
OnyxDevTools/onyx-database-samples/model-updates/ ... ManualMigrationDemo.java.
There are 2 parts to this example. The first part is to create an existing database containing the original structure of your model. The second is to change the data model and write a migration script to make it conform to the bulk schema updates.
When you have model updates that cannot be handled automatically by the lightweight migration there are use cases where you will have to perform a manual migration. In this even you will want to use the stream API within the PersistenceManager.
The stream API has several uses. It can be used to run perform analytics, run complex queries that require calculation, or in this case perform a manual migration.
This example will showcase one of the worst case scenarios which is moving a field from the Account entity and applying it to the related entity's Invoice. Since the balanceDue attribute has been removed from the Account entity, we must use the QueryMapStream lambda as opposed to the QueryStream. The primary difference is the QueryStream iterates through the serialized entities rather than a generic map. The generic map can be useful for accessing attributes that no longer exist on the serialized entity but still remain in the store.
The first iteration of our data model has the balanceDue on the Account.
Connect to a new database and seed the data.
The balanceDue attribute on the Account has been removed.
Create a script to reconnect to the database.
The Query specifies what records to iterate through. The Stream API requires a Query. This Query is designed to pull all Accounts.
The Stream API requires a Lambda or an extension of the QueryMapStream or QueryStream as a callback for each record in the query results. This example shows how to use a Lambda as the callback.
Within the QueryMapStream we fetch the latest Invoice in order to update the balanceDue. We then use the PersistenceManager to persist the change.