MongoDB Developer
Realm
plus
Sign in to follow topics
MongoDB Developer Centerchevron-right
Developer Topicschevron-right
Productschevron-right
Realmchevron-right

How to Update Realm SDK Database Schema for Android

Mohit Sharma2 min read • Published Oct 21, 2021 • Updated Sep 02, 2022
AndroidRealmSDKKotlinJava
Facebook Icontwitter iconlinkedin icon
Rate this tutorial
star-empty
star-empty
star-empty
star-empty
star-empty
This is a follow-up article in the Getting Started Series. In this article, we learn how to modify/migrate Realm local database schema.

Introduction

As you add and change application features, you need to modify database schema, and the need for migrations arises, which is very important for a seamless user experience.
By the end of this article, you will learn:
  1. How to update database schema post-production release on play store.
  2. How to migrate user data from one schema to another.
Before we get down to business, let's quickly recap how we set Realm in our application.
Doing migration in Realm is very straightforward and simple. The high-level steps for the successful migration of any database are:
  1. Update the database version.
  2. Make changes to the database schema.
  3. Migrate user data from old schema to new.

Update the Database Version

This is the simplest step, which can be done by incrementing the version of REALM_SCHEMA_VERSION, which notifies Relam about database changes. This, in turn, runs triggers migration, if provided.
To add migration, we use the migration function available in RealmConfiguration.Builder, which takes an argument of RealmMigration, which we will review in the next step.

Make Changes to the Database Schema

In Realm, all the migration-related operation has to be performed within the scope of RealmMigration.
To add/update/rename any field:

Migrate User Data from Old Schema to New

All the data transformation during migration can be done with transform function with the help of set and get methods.
In the above snippet, we are setting the default value of fullName by extracting the value from old data, like firstName and lastName.
We can also use transform to update the data type.
In case you want to delete the complete Realm, you can use deleteRealmIfMigrationNeeded with RealmConfiguration.Builder, but that should be considered as the last resort.
Thank you for reading! If you have any queries or comments, you can share them on the Realm forum or tweet me @codeWithMohit.
In the next article, we will discuss how to migrate the Realm database with Atlas Device Sync.
If you have an iOS app, do check out the iOS tutorial on Realm iOS Migration.

Facebook Icontwitter iconlinkedin icon
Rate this tutorial
star-empty
star-empty
star-empty
star-empty
star-empty
Related
Article

Technical Preview of a Realm Flipper Plugin


Oct 20, 2022 | 3 min read
Article

A Preview of Flexible Sync


Jun 14, 2023 | 5 min read
Article

Realm Meetup - Realm Kotlin Multiplatform for Modern Mobile Apps


Mar 21, 2023 | 23 min read
News & Announcements

Open Synced Realms in SwiftUI using @Auto/AsyncOpen


Oct 19, 2022 | 4 min read
Table of Contents