Search code examples
iosswiftdatabasexcoderealm

RealmSwift linear migration


Hello I have a question about realm migration. I understand that you need to use linear migration because the users may not update every time you reliese a version so the database scheme that they use may be different from the one you are using in the current version. I have this code:

let configuration = Realm.Configuration( schemaVersion: 3, migrationBlock:migrationBlock)
func migrationBlock(migration:Migration,oldVersion:Int){
if olVersion<2{
migrateFrom1To2(migration)
}
if oldVeraion<3{
migrateFrom2To3(migration)
}
}

What I don't get is that: If a user is using the first version and I have realised the third version when the user updates to third from first howmany times migrationBlock is called and if it only gets called once and I after each migration from 1to2 or 2to3 is the old version which is one of the two parameters of migrationBlock in realm configuration incremented after each migration for example when the helper method migrateFrom1To2 is called it migrates to the schemaVersion 2 but the latest is 3 so does it increment the oldVersion from 1 to 2 or does it increment the schemeVersion? and check again? My question is too long but please I need to understand it and I appreciate your help .

Thank you.


Solution

  • When you do a linear migration from schema version 1 to schema version 3, the value of oldVersion is 1.

    What will happen is that the first if statement (if oldVersion < 2) will be true, meaning migrateFrom1To2(migration) will be called, and perform the changes you want on the MigrationObject.

    Afterwards, you reach the second if statement (if oldVersion < 3), which will also be true (because oldVersion is still 1), meaning you'll perform migrateFrom2To3(migration) on the same MigrationObject as before, including the changes done in migrateFrom1To2(migration).