Migrating data from one sync realm to another

I’m looking for a way to move objects from one realm into another. I’ve been trying to do this by modifying their partition values accordingly (for me, that’s ownerId, which I’m changing from currentUser.id to newUser.id). At this point I’ve tried every possible variation of this that I could think of: doing a .create with the object and deleting the old one, migrating them all to a local realm and then back to the other synced realm, just changing the ownerId and praying that would do something.

I’ve gotten so many different errors with everything I’ve tried, the most common being
Ending session with error: failed to validate upload changesets: SET instruction had incorrect partition value for key "ownerId" (ProtocolErrorCode=212)
as well as
MongoDB error: E11000 duplicate key error (even after generating new _ids)
and
integrating changesets failed: error creating new integration attempt: error doing preliminary merge for integration attempt: error finding merge window: error finding reciprocal history version range: connection(cluster0-shard-00-01-ltqcv.mongodb.net:27017[-11247596]) failed to write: context canceled (ProtocolErrorCode=101)

I’ve also gotten a ton of native iOS errors (I’m using React Native) intermittently when logging out users, logging in users on different devices, and just occasionally when logging in. (the most recent was in table.hpp line 249: REALM_ASSERT(!key.is_unresolved());.

Would love some direction on how to move forward on this.

1 Like

@Peter_Stakoun Can we see how you’ve set up your schema and partitionKey? Also how are you copying the data over? You’ll need to copy the values over - you cannot just copy object references over since they are from different realms

I ended up figuring out a way to get around this in a way that didn’t require me to change any partition keys, so I would be ok with closing out this issue without resolution.

My schema is set up as such:

static schema: ObjectSchema = {
    name: 'User',
    primaryKey: '_id',
    properties: {
      _id: 'string',
      ownerId: 'string',
      .....
    },
}

User is a custom object I use to wrap user data and ownerId is the partition key.
The way I was copying the object was:

destinationRealm.create(
  User.schema.name,
  {
    ...User.serialize(user),
    ownerId: app.currentUser!.id,
  },
  Realm.UpdateMode.All,
)

where user is an object within the source realm and serialize is a function I wrote to turn the object into a vanilla js object with all of the object’s properties. I assumed that destructuring this object and changing the ownerId would work, but I got the error Ending session with error: failed to validate upload changesets: SET instruction had incorrect partition value for key "ownerId" (ProtocolErrorCode=212) when using this method.

@Peter_Stakoun So the spread syntax ... does not work with Realm. We do have obj.keys() and obj.entries() that return the realm properties which will work in a for…in loop. Hope that helps.

2 Likes