How to Handle Room Migrations When Android Users Skip App Versions
Keep Room migrations reachable for users who skip app releases, then test schema and data preservation from every supported starting version.
Table of Contents9 sections

A user can upgrade directly from the oldest app build you still support to the latest one. Room opens the database they already have, not the database that someone would get after installing every app release in order. Your newest build needs a working migration path from that user’s actual schema.
Quick solution:
Keep the migrations that connect every supported database version to the current one. Register them in the latest build, then test each starting version with saved schemas and real-looking rows.
Latest schema: 5
Database at 3: run 3 → 4 → 5
Database at 4: run 4 → 5
Fresh install: create 5 directly
This example uses AndroidX Room 2 with Android’s SupportSQLite API and a fictional offline notes database. Database version numbers are not app release numbers.
Start with the databases users actually have
An app release can change screens or fix bugs without changing Room’s schema version. Conversely, several app builds distributed through different tracks can contain different schema versions. A migration plan based on the last two release names is therefore less useful than an inventory of the database versions actually shipped. Start by listing which production, pilot, or managed installations are allowed to upgrade to the next build. Map their app releases to the database schema version for each persisted database separately.
In this example, schema 3 allowed a user to create notes offline. Schema 4 added a flag for pinned notes. Schema 5 adds an index for listings that order by pinned status and modification time. A device that stopped using the app at schema 3 might still have unsynced edits. The fact that schema 4 was available in the store for several months doesn’t mean that device ever opened it.
Build the test matrix from versions that can still meet the latest binary. Include older installations that return after a long gap and restored app data if your distribution and backup settings can restore an older database. Do not assume today’s active-user telemetry lists every file that still exists. If a version is intentionally unsupported, choose what users see and how they can recover before removing its path.
Register the chain in the newest binary
A Room migration represents a transition between two database versions. When the necessary edges are registered, the latest app can run them as a chain on the existing file. Running 3 → 4 does not mean launching the old app. The version 5 binary runs the migration code for that transition and then the next transition as part of opening the database. RoomDatabase.Builder documents how manual migrations are registered.
The first step adds a non-null INTEGER column using the SQLite default 0, meaning unpinned. The second creates the index. The final Room entity needs a matching pinned column declared with a default such as @ColumnInfo(defaultValue = "0"), plus an @Index whose name and columns match the SQL below. The example assumes v3 already has a notes table with an INTEGER id, TEXT body, and INTEGER updated_at. Keep the table and column names fixed in each migration; do not reach into a current entity to discover the name of a column that may be renamed later.
import androidx.room.Room
import androidx.room.migration.Migration
import androidx.sqlite.db.SupportSQLiteDatabase
val MIGRATION_3_4 = object : Migration(3, 4) {
override fun migrate(db: SupportSQLiteDatabase) {
db.execSQL("ALTER TABLE notes ADD COLUMN pinned INTEGER NOT NULL DEFAULT 0")
}
}
val MIGRATION_4_5 = object : Migration(4, 5) {
override fun migrate(db: SupportSQLiteDatabase) {
db.execSQL(
"CREATE INDEX index_notes_pinned_updated_at ON notes(pinned, updated_at)"
)
}
}
// In app setup; context and NotesDb are supplied by the app.
val database = Room.databaseBuilder(context, NotesDb::class.java, "notes.db")
.addMigrations(MIGRATION_3_4, MIGRATION_4_5)
.build()
Keep the same migration list in the production database factory and the tests that exercise that factory. If another flavor builds its own database instance, check that it uses the same supported paths. Adding a migration in one factory and forgetting a second factory can yield a green migration test and an app that still fails on first open. A migration class existing somewhere in the project is not the same as that class being reachable by the builder.
Prefer adjacent steps unless a shortcut earns its place
It is tempting to add a dedicated migration for every possible source and destination. Start with the adjacent chain instead. It keeps each schema change in one place and makes it easier to compare version snapshots and reason about what a row looks like after a particular transition. When a step is expensive, measure the actual upgrade with representative data before deciding that the chain is too slow.
Room can choose a direct migration over the intermediate steps. The Migration API reference describes this behavior. Do not register a shortcut merely because it creates the final index faster. If 3 → 5 bypasses 3 → 4, it must also add the pinned column and preserve the same semantics for notes created before pinning existed. Any old backfill, deduplication rule, data normalization, or relationship repair that still matters must also be reflected in the shortcut.
Compare final state rather than requiring the SQL statements to run in the same order. A valid shortcut might apply one table rebuild instead of two if it produces the same rows, values, constraints, and indexes. For a migrated v3 note, it should still set pinned to zero and preserve the note body, timestamps, and identity. Start with the equivalent behavior, then test the supposed payoff. Registering new shortcuts without testing the actual routing can silently change the path older devices have been relying on.
Keep fresh installs equivalent to upgraded ones
A database created by schema 5 starts with the schema generated for its entities. A database upgraded from schema 3 gets there through SQL and any data transformations in your migrations. Adding a column with a SQL default only in the migration is therefore not enough if the corresponding latest Room entity declares a different schema. Match the default, nullability, names, and index definitions rather than just verifying that a SELECT statement can read the new column.
SQLite’s ALTER TABLE documentation spells out the constraints on adding columns, including the requirement for a non-null default when adding a column constrained to NOT NULL. In this example, the default is both mechanically valid and an honest interpretation of the old rows because no notes were pinned before the feature existed. If the new column means something that cannot be inferred from historical rows, don’t invent a value just to pass validation. Consider a nullable transitional field or a separate data migration with a clearly defined mapping and tests for ambiguous rows.
Treat indexes as part of the schema as well. The example index is defined on pinned and updated_at in that order; name and column order should match between the migrated database and the final entity. Whether an index improves a particular query depends on its predicates and ordering. Verify with representative data and a query plan if performance is the reason for adding it. A passing upgrade test establishes that the index is present, not that it solves every slow query.
Test every supported starting point with meaningful rows
Export your schemas and keep the files in source control. These artifacts let you create a database as it looked when an older version was shipped. Do not regenerate an old schema from current classes and call it history. Keep the test runner’s assets configured to include the exported schemas for the actual database class and variant under test. Android’s Room migration guide explains exporting and using schema history.
A schema test is only half the check. The MigrationTestHelper reference describes running and validating migrations; add assertions for data the helper cannot judge. Give the old fixture at least one row with nonempty text and known timestamps. Seed with old-schema SQL, not the latest DAO, which may assume a new column already exists. In a separate fixture for version 4, include a note that is already pinned. Otherwise, a migration that incorrectly resets every note’s pin flag could pass the whole suite.
For a SupportSQLite-based instrumentation test, the following fragment assumes helper is a MigrationTestHelper rule configured with the instrumentation and NotesDb::class.java. The test uses a separate database file for this starting version and passes the full production migration chain to the helper.
val name = "upgrade-from-v3"
helper.createDatabase(name, 3).use { old ->
old.execSQL(
"INSERT INTO notes(id, body, updated_at) VALUES (7, 'Offline edit', 1200)"
)
}
helper.runMigrationsAndValidate(name, 5, true, MIGRATION_3_4, MIGRATION_4_5)
.use { latest ->
latest.query("SELECT body, updated_at, pinned FROM notes WHERE id = 7").use { row ->
check(row.moveToFirst())
check(row.getString(0) == "Offline edit")
check(row.getLong(1) == 1200L)
check(row.getInt(2) == 0)
}
}
Also open a seeded older file through the real application factory and perform a user-visible read and write after the upgrade. This catches misregistration and differences between fixture configuration and actual app startup. A migration helper test that receives migrations explicitly does not demonstrate that the production builder received those same objects. Use a fresh install test to open the same DAO operations without any legacy migration being involved.
Use data invariants to tell a pass from a false pass
A table can match Room’s expected schema and still contain the wrong records. Define what must remain true in terms you can assert. The example note should retain its ID, text, and timestamp after migrating from 3. A migrated v4 note with pinned set to one should keep its pin. If a real database has parent and child tables, verify that the correct relationships survive rather than only comparing row counts.
| Starting file | Upgrade expectation | Content assertion |
|---|---|---|
| Version 3 with offline note | Both migration steps run with the registered chain | Same ID, body and timestamp; pinned is 0 |
| Version 4 with a pinned note | Only the final step runs | Existing pinned value is still 1 |
| No database | Latest schema created without a migration | Insert, query, and pin a new note using the DAO |
| Oldest supported version with empty tables | Reaches latest schema | Normal app queries succeed with empty results |
| Unsupported version in a non-destructive configuration | No silent recreation | Open fails as expected; source data remains available for the recovery plan |
Run a larger fixture for changes that copy or rebuild a table. The elapsed time for a copy on a clean emulator says little about a device with heavy local usage. Include revisions with many rows, large payloads, limited free space, and a realistic opening flow. Time when content first becomes usable, rather than only the duration of a single SQL statement. If the upgrade is slow enough that users might restart the app, test interruption and reopening instead of assuming a clean run.
Handle missing paths without silently discarding user data
When Room cannot find a migration path, it can fail to open. The migration guide describes a destructive fallback that recreates database tables when a path is missing if that behavior is enabled. That is not a repair for a database with irreplaceable offline edits. It turns a missing transition into loss of data. In this notes example, keep destructive fallback off. If opening fails, use a defined error and recovery flow; report the affected app build and database version without logging note text.
For a separate database that is genuinely only a disposable cache, recreation might be a valid product decision. Verify the data can actually be refetched, including on a slow or absent connection, and check whether the database also contains queued mutations or other state that contradicts the word cache. Paging 3 with Room and Kotlin Flow illustrates why locally stored data can be central to the offline experience. Treat cache policy as a property of the whole database you’re about to recreate, not a label attached to one table.
Be careful with rollback. Stopping a staged app rollout protects devices that haven’t installed it; it does not move a device already migrated to schema 5 back to schema 4. An older binary can be unable to open a newer file unless a downgrade path is explicitly supported. Shipping a corrected newer build that can read the new schema is often easier to reason about than expecting the previous APK to undo a migration. Make the rollback assumptions explicit in the release notes and test whichever recovery route the team plans to use.
Put the path matrix in the release checklist
Each time the Room schema changes, update the matrix before release. Include the oldest versions still eligible for update, any direct shortcut that can take precedence, and the latest fresh install. For each important table, write down the values the business expects to survive. Add one instrumented test for the actual builder and a representative on-device test where storage and startup latency matter.
- Keep committed schema exports for all supported starting versions and build variants.
- Register the full migration chain in each app database factory.
- Seed old fixtures with their own schema and assert values, not just row counts.
- Compare upgraded and fresh databases using both schema validation and DAO operations.
- Review fallback and downgrade behavior before rollout.
- Monitor failures by app and schema version without collecting private payloads.
The release is ready when the latest app can open the oldest supported real database, preserve what the user put into it, and keep working. “It upgraded from yesterday’s build” only proves the most recent edge.
You Might Also Like
View all articles
Migrating Legacy Android Paging 2 to Paging 3
A practical migration guide for Android engineers moving legacy Paging 2 screens to Paging 3 with Room and Kotlin Flow.

Paging 3 with Room and Kotlin Flow
A practical guide to Android pagination with Room, Kotlin Flow, Paging 3, and RemoteMediator, focused on architecture decisions, failure modes, and offline behavior.

Android Paging 3 with Kotlin Flow: Room, RemoteMediator, and Offline-First Data
A practical architecture guide to Android Paging 3 with Kotlin Flow, Room, and RemoteMediator, focused on source-of-truth decisions, refresh semantics, failure states, and offline behavior.