I was trying to migrate a realm DB with custom configuration but it keeps throwing same error that exist before implementing the migration class.
Here are my code snippets
Realm configuration:
private fun getRealm(): Realm {
val config = RealmConfiguration.Builder()
.name(DB_NAME)
.encryptionKey(key)
.schemaVersion(2)
.migration(DbMigration())
.build()
return Realm.getInstance(config)
}
Here is my migration class:
public class DbMigration implements RealmMigration {
@Override
public void migrate(@NotNull final DynamicRealm realm, long oldVersion, long newVersion) {
RealmSchema schema = realm.getSchema();
if(oldVersion == 1){
RealmObjectSchema receiptLineSchema = schema.get(AggregationReceiptLine.class.getSimpleName());
receiptLineSchema.addField("payablePrice", double.class);
receiptLineSchema.addField("transactionFee", double.class);
oldVersion++;
}
}
}
And this is the error log despite adding the new fields in the migrate method of the migration class.
Caused by: io.realm.exceptions.RealmMigrationNeededException: Migration is required due to the following errors:
- Property 'AggregationReceiptLine.payablePrice' has been added.
- Property 'AggregationReceiptLine.transactionFee' has been added.
Keep in mind that using AggregationReceiptLine.class.getSimpleName() will break if you ever enable ProGuard. It does work in your code though as you would otherwise have gotten a NullPointerException.
Have you checked that the migration block is actually run? Version numbers start from 0, so I suspect that the version in your Realm file is not actually 1 when running your migration.
Thanks for the prompt response @cmelchior I will hardcode the className because proguard was enabled.
The migration block was not executed.
I eventually found out that the issue was caused by the key. Rather than using the key which was saved in an unencrypted DB, I passed the key directly from the point it was generated and that make the migration successful.
Probably something went wrong at the point of retrieving the key from the DB.
Hi @cmelchior how does Realm handle migration when user does not install all versions of the app, hence, missing some migration schemaVersions
_Example:_
The app has schemaVersion 1 as the first time user installs app. The next time user updates app, the schemaVersion has become 3. How does Realm handle the migration in such a way that user who jumps from schemaVersion 1 to 3 would not miss the _migration info_ in schemaVersion 2
The app has schemaVersion 1 as the first time user installs app
Realm is created with schemaVersion(0) by default. Though if you set schemaVersion(1) and the file is newly created, then it will be created with schema version 1 as is.
schemaVersion has become 3.
Ok
How does Realm handle the migration in such a way that user who jumps from schemaVersion 1 to 3 would not miss the migration info in schemaVersion 2
This is actually well-documented in the migrationExample.
Awesome. Thanks for the detailed explanation @Zhuinden
Most helpful comment
Keep in mind that using
AggregationReceiptLine.class.getSimpleName()will break if you ever enable ProGuard. It does work in your code though as you would otherwise have gotten a NullPointerException.Have you checked that the migration block is actually run? Version numbers start from 0, so I suspect that the version in your Realm file is not actually 1 when running your migration.