[Realm SYNC] Do I need a partner collection for a non-destructive change?

Hello.

Imagine that we have a Person object in my realm database:

public class Person : RealmObject
{
    [PrimaryKey]
    [MapTo("_id")]
    public ObjectId Id { get; set; }

    public string FirstName { get; set; }
    public string LastName { get; set; }
    public int Age { get; set; }
}

Then I decide to use combined FullName property instead of the separate FirstName and LastName property in the Person schema.

public class Person : RealmObject
{
    [PrimaryKey]
    [MapTo("_id")]
    public ObjectId Id { get; set; }

    public string FullName { get; set; }
    public int Age { get; set; }
}

According to the doc this change isn’t destructive and I don’t have to create a partner collection, forward and backward migration functions.

But the problem is that I would like to migrate the realm to conform to the updated Person schema. If I were developing an application without Realm Sync, I would use the following migration function:

 var config = new RealmConfiguration
{
    SchemaVersion = 4,
    MigrationCallback = (migration, oldSchemaVersion) =>
    {
        var oldPeople = migration.OldRealm.All<Person>();
        var newPeople = migration.NewRealm.All<Person>();

        // Migrate Person objects
        for (var i = 0; i < newPeople.Count(); i++)
        {
            var oldPerson = oldPeople.ElementAt(i);
            var newPerson = newPeople.ElementAt(i);

            // Changes from version 1 to 2 (adding LastName) will occur automatically when Realm detects the change

            // Migrate Person from version 2 to 3: replace FirstName and LastName with FullName
            // LastName doesn't exist in version 1
            if (oldSchemaVersion < 2)
            {
                newPerson.FullName = oldPerson.FirstName;
            }
            else if (oldSchemaVersion < 3)
            {
                newPerson.FullName = $"{oldPerson.FirstName} {oldPerson.LastName}";
            }
        }
    }
};
var realm = Realm.GetInstance(config);

and that would be enough to extract the FullName from older databases combining the FirstName and LastName properties.

So I have the following questions:

  1. Do I need a partner collection for that changes?
  2. Does the non-sync realm migration function applicable for the realm sync? And if No what I should do in that case?

Let me answer 1 & 2: There is no migration for Realm Sync, it only applies to local Realms.

While this change isn’t destructive, it’s become a one-shot computed property, e.g. First Name + Last Name = Full name and I believe you also want Full Name to be managed going forward

Let me ask this - do you really want to also have a managed Full name property? Or this just an example?

In other words, having first and last as separate properties is a bit more flexible and if you need them combined you can use a computed property to present the full name and that would be backed by those two managed properties which would alleviate having to do any kind of migration.

HI @Jay . Thank you for your response. This was an example to understand the “migration” process. Actually in our app we are going to switch to realm sync. I am concerned about let’s say migration imitations. What will happen when I make non destructive change and write a migration function like in non-sync case. Will be that ok or should I do something else to get desirable result (For instance I am restoring the older database and it gets through migration and eventually I get a fresh database with non-nullable FullName-s)

@Vardan_Sargsyan92 On the serverside you need to add to your schema FullName, so your backend Schema would be FirstName, LastName, and FullName. You can then use a script to create that FullName Value, one time. On the client side you would change the schema to just have FullName, any existing users who upgrade would download the new FullName value from the serverside. I hope that helps, it is not a destructive change, just an addititve change but you still need to include all fields, old and new, on the server side. The client side is where you change the schema to not include the old fields.

1 Like

That makes sense. Thank you @Ian_Ward for answer.

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.