Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle Realm React Native migrations and schemaVersion on iOS?

Before I fall in love with realm on react-native and iOS, I am trying to learn more about how I can handle migrations. This statement has me concerned:

Realm React Native 0.10.0

  • https://realm.io/docs/react-native/latest/#migrations

Migrations are currently limited to updating the schema and schemaVersion when opening a Realm as outlined above. Data migrations are not yet supported but may be added in the future.

I understand this to mean I need to increment schemaVersion each time I make a change to **any schema**.

How can I specify multiple schemas, each with their own schema versions ?

This does NOT work:

export default new Realm(
  {schema: [AppSetting], schemaVersion: 0},
  {schema: [Gps], schemaVersion: 3},
  {schema: [Waypoint], schemaVersion: 4},
  {schema: [FlightPath], schemaVersion: 1},
);

This assumes that my more complicated schemas might need to be revised frequently until I get things right.

Will migrations be simple as long as I only add new properties?

I assume I cannot rename or remove existing properties?

Advice on realm migrations is much appreciated,

like image 571
Ed of the Mountain Avatar asked Mar 15 '16 21:03

Ed of the Mountain


1 Answers

You need to specify a single schemaVersion for your entire schema:

export default new Realm({schema: [AppSetting, Gps, ...], schemaVersion: 0});

When you update any of the objectSchema in your schema you need to bump your schemaVersion. What this means is that some individual objectSchema will remain the same across multiple schemaVersions but I think this is less complicated than having different versions for each object type.

When you initialize a Realm with a new schemaVersion/schema, all new properties are added and missing properties and removed. So if you rename a property this will end up adding a new property with the new name, and removing the old property along with its data. For now if you want to copy data from one property to another you need to do it in two steps, so both the old property and new property exist at the same time allowing you to do the copy. You would also need to keep track of whether you did the copy so you only perform this the first time the the Realm is opened with the new schema. If you are only adding properties you can avoid most if not all of this complexity.

We didn't have time to finish migrations for the initial release, but the good news is most of the functionality is implemented internally and just needs to be exposed through the js apis. How things will work is you will be able to pass a migration function when opening a Realm which will give you access to both the pre and post migrated Realms allowing you to copy data as needed. We hope to have this completed in one of the next few releases.

like image 73
Ari Avatar answered Oct 03 '22 03:10

Ari