After a Room schema bump to version 7, some upgrading users crash on launch with IllegalStateException; how do you tell a missing migration from a wrong one, and fix each?
answer
- read the exception message
- no path versus failed validation
- fresh installs never migrate
- identity hash without a version bump
- rolled-back transaction
basics
~20 sRead the message. 'A migration from 5 to 7 was required but not found' means no registered path: add the missing Migration. 'Migration didn't properly handle' means a path ran but the tables differ from the entities: fix that migration's SQL.
solid answer
~50 sRoom's open-time checks throw distinct `IllegalStateException`s, and the message tells you which case you hit. **"A migration from 5 to 7 was required but not found"**: no chain of registered migrations leads from that user's version, so you add the missing `Migration` (or `@AutoMigration`) for it. **"Migration didn't properly handle: ..."**: a path ran, but validation found a table that differs from the entities, such as a column added as nullable when the field is a non-null `Int`; you fix the migration's SQL, never the entity. **"Room cannot verify the data integrity"**: the schema changed but the version did not, so the stored identity hash no longer matches; bump the version and add a migration. In each case nothing is committed, so the user's file is left as it was and a fixed release can migrate it. A destructive fallback hides only the first case, and only by deleting data.
go deeper
Learn the three messages by heart: no migration found, migration didn't properly handle, and cannot verify data integrity.
Explain what Room compares at each stage: stored version against declared version, migrated tables against entities, stored identity hash against the compiled one.
Diagnose from the message, fix the SQL rather than the entity, rely on the rollback to keep data, and reproduce with a migration test before shipping.
Treat a migration crash as a release incident with a forward fix, and ask why the release process let an untested upgrade path ship.
## Where these crashes come from Room checks the database when it first opens it, which usually happens on the first query rather than when `build()` returns, so the stack trace often points at the first DAO call after launch. The check compares SQLite's stored `user_version` with the `@Database` version, runs migrations if they differ, validates the result, and verifies the schema **identity hash** stored in `room_master_table`. Each failure throws `IllegalStateException` with a distinct message. A detail explains why QA misses these: **a fresh install never migrates.** A file at version 0 has its tables created directly from the current entities, so every clean-install test passes. Only devices holding an older file take the migration path. ## The three messages | Message starts with | What it means | Who crashes | |---|---|---| | "A migration from 5 to 7 was required but not found" | No chain of registered migrations leads from 5 to 7 | Users whose file is at 5 | | "Migration didn't properly handle:" | A chain ran, but a table differs from what the entities expect | Users who ran the faulty step | | "Room cannot verify the data integrity" | Same version number, different schema identity hash | Every user with an existing file at that version | ## Missing migration The full message names the versions and the remedies: provide a path through `addMigration(...)` or allow destructive migration through one of the `fallbackToDestructiveMigration*` functions. Typical causes: - a `Migration` object was written but never passed to `addMigrations()`; - a release bumped the version with no schema change, and no empty step was registered; - a shortcut such as `Migration(4, 6)` leads to a version with no onward step, and Room's path search does not backtrack. **Fix:** add the step, for example `Migration(5, 6)`, ship it, and add a test from version 5. ## Wrong migration After the chain, Room compares each table with the expected table generated from the entities: column names, type affinity, `NOT NULL`, primary-key membership, a default value the entity declares, foreign keys and indices. The message lists the expected and the found definition of the first table that differs. Common causes: - `ALTER TABLE workouts ADD COLUMN calories INTEGER` when the field is a non-null `Int`, so Room expects `INTEGER NOT NULL` (which also needs a `DEFAULT` in SQL); - an index or foreign key declared on the entity but never created; - a renamed column whose new name was misspelt in the SQL. **Fix the migration, not the entity.** Diff the expected and found text, correct the SQL (or the auto-migration's spec), and add a migration test. ## Schema changed without a version bump If the entities changed but `version` did not, the stored identity hash differs from the one compiled into the app, and Room asks you to increase the version number. In development this appears after editing an entity; in production it means a release shipped a schema change without a bump. **Fix:** raise the version and supply a migration from the previous version that makes the change. ## Why the data is still there Room runs the whole chain, the validation and the version update inside one transaction. When any of them throws, the transaction rolls back: 1. the file keeps its old `user_version` and old tables; 2. the app crashes on every launch until a corrected build ships; 3. the corrected build finds the same old version and migrates normally. That is why the right response is a fixed release, not an emergency wipe. ## What a destructive fallback does and does not do - `fallbackToDestructiveMigration(...)` only acts when **no path exists**. It drops and recreates the tables instead of throwing, which deletes the user's data. - It does **not** catch a validation failure: if a path exists and produces the wrong tables, Room still throws "Migration didn't properly handle". - `fallbackToDestructiveMigrationFrom(...)` limits the wipe to the start versions you list; `fallbackToDestructiveMigrationOnDowngrade(...)` limits it to downgrades. Whether wiping and re-syncing is ever acceptable for a product is a design decision covered in the mobile system design material on on-device migrations. ## Reproducing it before users do - Create the database at the old version from its exported schema and run the migrations to 7 with Room's `MigrationTestHelper`; it throws the same validation error. - Install the previous release on an emulator, add data, then install the new build over it.
- Crash reports show 'Migration didn't properly handle' and a teammate proposes adding a destructive fallback to stop the crashes. Will it?No. The destructive fallback applies only when Room finds no migration path. Here a path exists and runs, and the failure comes from validation afterwards, so Room still throws. The fix is to correct the migration's SQL so the tables match the entities, and ship it; the affected users' files rolled back and are still at the old version.
- Why did this crash never show up in QA's test passes on clean devices?A clean install starts at version 0, so Room creates every table straight from the current entities and runs no migration at all. The faulty step only runs on devices holding a file from an earlier release; testing needs either `MigrationTestHelper` from each shipped version or an install-over of the previous build.
- A user installs an older build after a rolled-back release and crashes with 'A migration from 7 to 6 was required but not found'. What are Room's options?Room treats a downgrade like any other version change: it needs a registered `Migration(7, 6)`, or a fallback that permits wiping on downgrade, such as `fallbackToDestructiveMigrationOnDowngrade(...)`. Whether a product should ever allow that is a policy question for the on-device migration design, not a Room default.
- A destructive fallback also catches migrations that produce the wrong tables
- Changing the entity to match the migrated table is the right fix
- The failed migration leaves users with a half-migrated database
- If fresh installs work, the migration must be correct
- Bumping the version without a migration fixes the integrity crash for existing users