DEV Community

Cover image for Automate Realm App Rebuilds
Dachary
Dachary

Posted on

Automate Realm App Rebuilds

I'm currently developing a macOS/SwiftUI app that persists data with Realm Database and Realm Sync. If you haven't used it before, Realm Sync is a magical service that keeps data in sync across devices and persists the data to a MongoDB Atlas cloud database through a BaaS (Backend-as-a-Service). I'm using it in my app because I want to use the same app on multiple devices, and have my data sync magically between the devices.

The great thing about Sync is that it doesn't require much code for developers to use. If you're already using Realm to store your data, using Realm Sync to share it across devices is almost as simple as opening the database with a Sync configuration instead of a local configuration. There are a few more details to handle - some additional error handlers to write - but overall, it "just works" and handles conflict resolution and the hard parts of syncing data for you.

The less awesome part about Sync is the fact that there isn't a good path to migrate "breaking" schema changes. The Realm Sync BaaS doesn't have a concept of a versioned schema, so making "breaking" schema changes requires jumping through hoops and you should generally try to avoid it.

One problem: while developing my very own Realm Sync app, I'm making breaking schema changes frequently. Probably every other day, if not more often, I realize I need a new property or something that is currently a required property has to be optional - or a different type - for Reasons.

In order to deal with this during development, I'm just deleting my Realm App backend, deleting the database from MongoDB Atlas (which blows away all of my existing data - which is fine because I can't easily use it after making a breaking schema change), and creating a new Realm App. Then I have to configure the new Realm app's authentication, permissions, etc.

This is a bit of a pain, so I wrote my very first bash script to automate this process. The script itself is 17 lines - 2 of which are comments to help devs figure out what to put in one of the other lines. The README is 361 lines. Can you tell I write documentation for a living?

If you're also developing Realm Sync applications, I hope you find this helpful!

The script is available on GitHub at realm-development-tools/rebuild-sync-app. PRs are welcome if you spot an issue or easy improvement. I'll add error handling someday, but have lost enough of my Sunday to this rabbit hole...

Top comments (0)