Realm Kotlin SDK and Android app releases: making a schema version bump reversible after migration
0 reputation · 02 Feb 2024, 22:02 UTC
0 reputation · 02 Feb 2024, 22:02 UTC
I am integrating the Realm Kotlin SDK into an Android application where each release may bump the Realm schema version and run a Migration callback that adds or transforms fields. The documented behavior is that once the local .realm file is migrated to the higher version, the change is one-way: shipping a hotfix that reverts the schema version in code causes the older build to fail opening the already-migrated file.
The two components at the boundary are the Realm file on device and the app update/rollback pipeline (e.g., staged rollout plus the ability to reinstall a previous APK). The uncertainty is how to design the release process so that rolling back the app binary does not strand users on an unreadable database, given that Realm offers no built-in downgrade path and deleteRealmIfMigrationNeeded() would wipe local data.
Specifically:
Assuming a recent Realm Kotlin SDK 1.x release; verification against the exact version in use is still needed.
32775 reputation · 03 Feb 2024, 05:27 UTC
No — copying the .realm file before migration is not a reliable rollback mechanism, and there is no supported downgrade or compatibility mode in current Realm Kotlin SDK 1.x releases. If the file's internal schema version is higher than the configured schemaVersion, opening it is a hard error. The only built-in escape is deleteRealmIfMigrationNeeded(), which destroys local data. Rollback safety has to come from release design, not from file backups.
Two separate problems make pre-migration copies fragile:
compactOnLaunch and background compaction make the timing even less predictable. A consistent copy requires every instance closed and all transactions committed, which is hard to guarantee on an arbitrary launch path.Realm stores the schema version in the file header and requires the configured schemaVersion to be greater than or equal to it. A lower configured version is treated as an unhandled migration error in current 1.x releases; there is no documented compatibility mode. The exact exception type and message vary by SDK version, so verify against the version you ship.
schemaVersion for changes the old build can tolerate. Additive model changes without a version bump keep the file readable by both builds.schemaVersion increase as a point-of-no-return release: ship it only after staged rollout of the previous version is complete and rollback of that version is no longer offered.app-v2.realm) and migrates data lazily; the old APK still opens the old file. Rollback then strands nothing, at the cost of dual-file sync/copy logic.One detail that changes this recommendation: if your migration is destructive (renames, removals, type changes), additive-compat gating is off the table and the versioned-file pattern becomes the only safe option.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.