realm / realm-kotlin

Kotlin Multiplatform and Android SDK for the Realm Mobile Database: Build Better Apps Faster.
Apache License 2.0
878 stars 52 forks source link

Auto migration is not working when adding a new property #1771

Open sembozdemir opened 3 weeks ago

sembozdemir commented 3 weeks ago

How frequently does the bug occur?

Always

Description

I am expecting that auto migration should work according to the documentation here when I add a new property. However, it is failing for some reason. I guess there is a bug or am I missing something? Could you please help with it?

const val realmSchemaVersion = 3L // it was 2L

private val config = RealmConfiguration
  .Builder(setOf(MessageRealmObject::class, PendingMessageRealmObject::class))
  .schemaVersion(realmSchemaVersion)
  .build()

@Keep
private class MessageRealmObject : RealmObject {
    @PrimaryKey
    var _id: String? = null
    var chatId: String? = null
    var role: String? = null
    var text: String? = null
    var createdAt: Long? = null
    var photoId: String? = null // new property
    var photoImageUrl: String? = null // new property
}

@Keep
private class PendingMessageRealmObject : RealmObject {
    @PrimaryKey
    var _id: RealmUUID = RealmUUID.random()
    var chatId: String? = null
    var text: String? = null
    var createdAt: Long? = null
}

private fun <T> withRealm(func: (realm: Realm) -> T): T {
    val realm: Realm = Realm.open(config)
    val returnValue = func(realm)
    realm.close()
    return returnValue
}

fun example() {
   // it crashes when withRealm is called.
   withRealm {}
}

Expected behavior Auto migration should work

Actual behavior App crashes with the following exception.

Stacktrace & log output

java.lang.IllegalStateException: [RLM_ERR_SCHEMA_MISMATCH]: Migration is required due to the following errors:
  - Property 'MessageRealmObject.photoId' has been added.
  - Property 'MessageRealmObject.photoImageUrl' has been added.

Can you reproduce the bug?

Always

Reproduction Steps

No response

Version

1.16.0

What Atlas App Services are you using?

Local Database only

Are you using encryption?

No

Platform OS and version(s)

Android 34

Build environment

Android Studio version: Android Studio Hedgehog | 2023.1.1 Patch Gradle version: 8.2

sync-by-unito[bot] commented 3 weeks ago

➤ PM Bot commented:

Jira ticket: RKOTLIN-1098