Contents
CWCoding With DhrumilCW Practical Flutter and Kotlin tutorials for working mobile developers

Home › Articles

Room Database Migration Checklist Before You Ship

By Priya Shah · Published · Updated · 4 min read

Server room with network cables and equipment representing database migration planning.
Server room with network cables and equipment representing database migration planning. · Foto: ugoxuqu / Pixabay

Most Room crashes in production are not query bugs. They are migrations that were never tested, a schema version that was bumped without an exported JSON file, or a destructive fallback that quietly deleted user data on a real device.

This checklist walks through what to verify before a release: version numbers, exported schemas, MigrationTestHelper tests, index and column changes, and the fallback rules you should think twice about. It is aimed at apps with a shipped user base, where the database already lives on thousands of phones.

Start With Version Numbers and Exported Schemas

Every schema change begins with a version bump in your @Database annotation. The rule is simple: if the SQLite structure changes at all, the version goes up by one. Adding a column, changing a default, dropping an index, or altering a NOT NULL constraint all count. Editing a migration file without touching the version number is the most common cause of a startup crash, because Room compares the stored identity hash on disk with the compiled one.

Export your schemas to version control. Set room.schemaLocation in the annotation processor arguments, usually app/schemas, and commit those JSON files. Room's generated code compares the identity hash from the exported schema with the one stored in the database, so a missing or stale JSON file makes the app fail on upgrade. Treat schema JSON like source code: review it in pull requests and never delete old versions.

  • Bump the version once per structural change, not per app release
  • Commit app/schemas JSON files and keep every historical version
  • Check for accidental schema drift after dependency upgrades
  • Verify the identity hash matches between exported and installed schemas

Write a Migration Object for Every Gap

Users skip releases. Someone on version 3 of your database may jump straight to version 7, so you need a path from 3 to 4, 4 to 5, 5 to 6, and 6 to 7, or one combined migration if Room can resolve the chain. Define each step as a Migration object with explicit ALTER TABLE and CREATE INDEX statements, and prefer small, single-purpose migrations over one giant script that does everything at once.

Watch the SQLite limitations. You cannot add a NOT NULL column without a default, and you cannot change a column type in place. The standard workaround is a table rebuild: create a new table with the target schema, copy the rows across with INSERT INTO ... SELECT, drop the old table, and rename the new one. Indexes must be recreated after the rename because dropping the table removes them.

  • Cover every version gap a real user could jump across
  • Add NOT NULL columns only with a DEFAULT value
  • Rebuild tables for type or constraint changes, then recreate indexes
  • Keep migration SQL readable; comment the intent of each step

Test Migrations With Real Data, Not Empty Tables

MigrationTestHelper is the tool that makes migrations testable. Create the database at an old version, insert representative rows, run the migration to the new version, and then validate the schema. Room's validateMigration helper catches mismatches between the migrated structure and the current entity definitions, which is exactly the failure that crashes apps at startup.

Empty tables hide most bugs. Insert rows with nulls in optional columns, long text, unicode, and duplicate values that would violate a new unique index. For apps with large datasets, seed a few thousand rows so you also see how long the migration takes on a mid-range device. A migration that runs 4 to 8 seconds on the main thread will trigger an ANR, so run it on a background dispatcher and show progress for long upgrades.

  • Seed old-version databases with realistic, messy data
  • Run validateMigration after every step
  • Time the migration on a low-end device, not just an emulator
  • Test the full upgrade path, not only the last single step

Decide Your Fallback Rule Deliberately

Room offers fallbackToDestructiveMigration, and it is tempting because it makes upgrade errors disappear. It works by dropping and recreating all tables when no migration path exists, which means every user row is gone. For a cache-only database that is fine. For anything holding user-generated content, orders, or sync state, a destructive fallback is data loss with a delay.

If you must keep a fallback for old versions, scope it: fallbackToDestructiveMigrationFrom with specific version numbers limits destruction to users on ancient builds you no longer support. Log which path was taken so you can see in crash reports how many installs hit it. Better still, write the missing migration and remove the fallback, then confirm in tests that no destructive path is reachable.

  • Use destructive fallback only for pure cache databases
  • Prefer fallbackToDestructiveMigrationFrom with explicit old versions
  • Log migration outcomes to catch silent upgrade failures
  • Remove the fallback once a migration chain is complete
401k rows18010k rows90050k rows2100100k rows12000500k rows
Typical migration duration by row count (mid-range device) (milliseconds)

Run the Pre-Release Verification Pass

Before shipping, walk the upgrade path on a device with an old build installed. Install the current production APK, create data, then install the new build over it without uninstalling. This catches signing, backup and restore, and migration ordering issues that unit tests miss. Repeat for at least two older versions if your release cadence is slow, since users commonly skip several updates.

Check performance and disk behavior too. A large table rebuild temporarily doubles storage for that table, so confirm free space on devices with heavy usage. Measure cold start time after the upgrade, and make sure the migration is not running on the main thread. Finally, review your release notes and support plan so you know what to tell users if an upgrade problem appears in the first days after rollout.

  • Install the new build over an old one without uninstalling
  • Test upgrades from at least two older versions
  • Check free disk space for table rebuilds
  • Measure cold start after migration and watch crash reports
Room migration risk by change type
ChangeTypical riskMigration effortTest focus
Add nullable columnLow1 ALTER TABLENull handling in queries
Add NOT NULL columnMediumALTER with DEFAULTExisting row defaults
Add indexLow1 CREATE INDEXQuery plan and write speed
Change column typeHighTable rebuildRow count and data fidelity
Drop or rename columnHighRebuild or manual copyEntity mapping and tests
Multi-version jumpHighFull migration chainUpgrade from every old version

Keep the Schema Honest Over Time

Migrations age badly when nobody reviews them. Add a test that opens the database at every supported version and runs the full chain to the current version, so a broken link fails in CI rather than on a user's phone. Keep entity classes and migration SQL in sync by regenerating exported schemas on every build and committing the diff.

For teams, document the rule in the repo: no schema change merges without a version bump, an exported JSON file, and a passing migration test. If you are still tuning list and form screens around this database, the widget-count and adapter-sizing guides on this site pair well with database work, and the Flutter widget budget breakdown and RecyclerView adapter sizing guide cover the UI side once your data layer is stable.

  • Fail CI on schema drift or missing migration tests
  • Review schema JSON diffs in pull requests
  • Document the version-bump rule in the repository
  • Bump the schema version once per structural change and commit exported JSON schemas
  • Test every migration step with realistic seeded data and validateMigration
  • Never use a destructive fallback on tables holding user data
  • Verify upgrades by installing the new build over an old one on a real device
Priya Shah · Android and Kotlin author
Covers Kotlin, Jetpack libraries and Android architecture with a hands-on approach. Focuses on migration guides, database work and networking patterns for small teams.

Related articles

↑