1

I'm using realmjs DB in my react-native app where I have a schema settings. I want to change a property type of that schema from int to string. So I understand that I need to perform migration and I decided to perform a linear migration. For the migration I followed the example from Realm documentation and ended up doing something like below:

const schemaList = [schemaV1,schemaV2];
let nextSchemaIndex = Realm.schemaVersion(Realm.defaultPath);
while (nextSchemaIndex < schemaList.length) {
    const migratedRealm = new Realm(schemaList[nextSchemaIndex++]);
    migratedRealm.close();
}

export default new Realm(schemaList[schemaList.length - 1]);

schemaV1 is the old version of the db and schemaV2 is the latest version of the db after changes of property type. The schemaV2 also has a migration function like below:

    if (oldRealm.schemaVersion < 1) {
        const oldObjects = oldRealm.objects(TBL_MOBILE_SETTING);
        const newObjects = newRealm.objects(TBL_MOBILE_SETTING);
        for (let i = 0; i < oldObjects.length; i++) {
            newObjects[i].module    = oldObjects[i].module;
            newObjects[i].setting   = oldObjects[i].setting;
        }
    }

But at the end, when I'm trying to run the app, it's crashing with an error message that

Database migration is needed

Does it mean that the migration function in schemaV2 never runs? If so, how to make sure that all the migration functions are running properly? Or am I missing something else?

EDIT

I found that column type change is not allowed in RealmJS and I added a new column and tried to perform migration but still same error.

Julfikar
  • 1,353
  • 2
  • 18
  • 35

1 Answers1

0

The problem was what I guessed. The migration function was not being called. I had to create an export const function and call the migration function in the index file cause it's the file which is called at the starting of the app.

Julfikar
  • 1,353
  • 2
  • 18
  • 35