Uploaded image for project: 'Realm JavaScript SDK'
  1. Realm JavaScript SDK
  2. RJS-1967

onMigration is not working as expected

    • Type: Icon: Bug Bug
    • Resolution: Works as Designed
    • Priority: Icon: Minor - P4 Minor - P4
    • None
    • Affects Version/s: None
    • Component/s: None

      How frequently does the bug occur?

      All the time

      Description

      I wanted to implement migration in the realm initialization when using @realm/react but was unable because of the exception in host function. In migration I wanted to delete some objects in the newRealm. The code below represents how I did it

      onMigration: (oldRealm, newRealm) => {
        if (oldRealm.schemaVersion < 2) {
          const objects = oldRealm.objects('Currency')
      
          // Delete some object in new realm
          newRealm.delete(objects.filtered('code == "RUB" OR code == "BYN"'))
        }
      }
      

      Which outputs the error [Error: Exception in HostFunction: Must be in a write transaction]

      As soon as I'm wrapping operation in write transaction like

      onMigration: (oldRealm, newRealm) => {
        if (oldRealm.schemaVersion < 2) {
          const objects = oldRealm.objects('Currency')
      
          // Delete some object in new realm
          newRealm.write(() => {
            newRealm.delete(objects.filtered('code == "RUB" OR code == "BYN"'))
          })
        }
      }
      

      It outputs such error then [Error: Exception in HostFunction: The Realm is already in a write transaction]

      Stacktrace & log output

      No response

      Can you reproduce the bug?

      Yes, always

      Reproduction Steps

      Listed in the error description

      Version

      11.1.0

      What SDK flavour are you using?

      Local Database only

      Are you using encryption?

      No, not using encryption

      Platform OS and version(s)

      iOS 16.1

      Build environment

      No response

      Cocoapods version

      No response

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

              Created:
              Updated:
              Resolved: