Uploaded image for project: 'Realm Kotlin'
  1. Realm Kotlin
  2. RKOTLIN-1098

Auto migration is not working when adding a new property

      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

      Unable to find source-code formatter for language: shell. Available languages are: actionscript, ada, applescript, bash, c, c#, c++, cpp, css, erlang, go, groovy, haskell, html, java, javascript, js, json, lua, none, nyan, objc, perl, php, python, r, rainbow, ruby, scala, sh, sql, swift, visualbasic, xml, yaml
      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

            Assignee:
            Unassigned Unassigned
            Reporter:
            unitosyncbot Unito Sync Bot
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated: