Realm-java: Issue with Realm migration on custom configuration

Created on 28 Nov 2018  路  5Comments  路  Source: realm/realm-java

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.
O-Community T-Help

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.

All 5 comments

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

Awesome. Thanks for the detailed explanation @Zhuinden

Was this page helpful?
0 / 5 - 0 ratings

Related issues

wyvern610 picture wyvern610  路  3Comments

AlbertVilaCalvo picture AlbertVilaCalvo  路  3Comments

jjorian picture jjorian  路  3Comments

mithrann picture mithrann  路  3Comments

pawlo2102 picture pawlo2102  路  3Comments