We have an application, that up until now was totally offline and was using Realm as a database. We are currently trying to implement a cloud-synced version of the app. Obviously, we need to somehow migrate the offline data for the existing users to the Realm cloud.
In the offline version of the app, all of our models had the Id
as a primary key
[PrimaryKey]
public string Id { get; set; } = Guid.NewGuid().ToString();
Because we didn’t have any explicit MapTo
attributes, internally, the primary key is stored as Id
as well.
Inside Realm sync, there’s a requirement that the primary key should be named as _id
internally. Also, we should define a new property for the partition key. So, in the synced version of the Realm, we should have at least the following:
[PrimaryKey]
[MapTo("_id")]
public string Id { get; set; } = Guid.NewGuid().ToString();
[Required]
[MapTo("_partitionKey")]
public string PartitionKey { get; set; }
The approach we’ve decided to take is comprised of 2 main phases
- First of all, we will need to update the schema of the offline Realm to the point, where the schema would be final and acceptable to be used by the Realm cloud sync. This involves both the necessary changes to migrate somehow the primary key to
_id
and introduce_partitionKey
. Also some changes in general schema improvements and cleanup internal to our company to make the database future proof. This is done to safeguard us as much as possible from schema changes after we’ve migrated fully to Realm sync. At the completion of this phase, we will have a schema, that’s simply ready to be hooked to cloud sync without any further changes. But we won’t physically connect to the Realm sync and users will still be using the offline version of the app. - After this, we will implement the Realm cloud sync. On the app launch, we will detect that the users are on the previous version of the app and will suggest them migrate their data to the synced version. We’ve already implemented code that deep copies data from offline Realm to synced Realm that has the same schema. We will simply copy over their existing data to their user-specific partition inside the Realm cloud. Job done.
So, now the questions and the challenges that we are facing:
- I am facing a problem with phase 1. I can’t find a way of changing the name of the primary key. Is that possible in the C# client? I really don’t want to have 2 separate schemas for the offline and the synced versions. As that would mean duplicating a tone of code to support both schema versions, which essentially are going to the same classes duplicated into 2 different namespaces with slightly different schema.
- During the initial rounds of hooking Realm sync into the existing schema the app would crash with exceptions like this one
It’s was extremely hard to understand what was causing those issues. Sometimes, I would have a missing partition key, sometimes, user auth was incorrectly configured. Are there any tools/logs that would help when investigating such issues? Cause the only investigation we can do now is a mere “needle in a haystack” search or guessing.The program ‘XXX.exe’ has exited with code -1073740791 (0xc0000409).
- Is the strategy that I have described above a good one? Perhaps, there’s a better approach?
Thanks in advance for the help.