As the Android lead on a Room app, what schema-change policy would you set for exported schemas, auto versus hand-written migrations, and destructive fallback in release builds?
answer
- schemas as reviewed history
- a tested path from every shipped version
- auto for additive, manual for data
- which fallback, from which versions
basics
~10 sCommit exported schemas and review their diffs, require a tested path from every version still installed, auto-migrate additive changes, hand-write data moves, and keep a blanket fallbackToDestructiveMigration out of release builds.
solid answer
~50 sI treat the schema directory as reviewed history: every `@Database` version bump arrives with its exported JSON in the same pull request, and nobody edits or deletes old files. Each release must have a path from every version still installed, proven by `MigrationTestHelper` tests per start version in CI plus one full-builder test. Additive changes go through `@AutoMigration`, renames and deletes through a reviewed `AutoMigrationSpec`, and anything that moves or computes data through a hand-written `Migration`. In release builds there is no blanket `fallbackToDestructiveMigration`, because it silently turns a missing step into data loss and also wipes on downgrade. If the product accepts a wipe for ancient versions, I use `fallbackToDestructiveMigrationFrom` with an explicit list. The tradeoff is engineering time against the risk of data loss, and the cost grows with every version you keep supporting.
go deeper
Know that exported schemas are committed files and that a destructive fallback deletes users' local data.
Explain which changes auto-migrations handle, when a spec or a manual migration is needed, and what each fallback variant covers.
Back the policy with per-version migration tests and a production-builder test, and spot fallback settings that would hide a missing step.
Weigh review cost, test cost and data-loss risk, set the release-build fallback rule, and decide how long old versions stay supported.
## The decision a lead owns Room gives a team several ways to handle schema changes, and each one fails differently. The lead's job is to pick defaults the whole team follows, so that a schema change is routine rather than a risk. The policy has four parts: how schema history is kept, which kind of migration is used when, how coverage is proven, and which destructive options release builds may use. The product decision of when wiping and re-syncing local data is acceptable belongs to the mobile system design material on on-device migrations; this policy covers what the team does with Room's own tools. ## Schema history as a reviewed artefact - **Keep `exportSchema` on** (the default) with a `schemaDirectory` set through the Room Gradle plugin, and commit the folder. - **Review the JSON diff.** A version bump shows up as a new `<version>.json`; reviewers read the changed `createSql`, columns and indices next to the migration that produces them. - **Never edit or delete an old file.** Auto-migrations and `MigrationTestHelper` both read them; losing one makes that version untestable. - **Bump the version for every schema change.** Room's identity-hash check throws on open for users whose file has the old schema under the same number, so a missed bump is a launch crash. ## Auto versus hand-written migrations | Change | Default | Why | |---|---|---| | New table, column or index | `@AutoMigration` | Generated, little to review | | Rename or delete | `@AutoMigration` with an `AutoMigrationSpec` | The spec is the reviewed statement of intent | | Data moved, split, computed | Hand-written `Migration` | Explicit SQL, easier to reason about and test | There is no single right answer here. Auto-migrations reduce boilerplate but depend on intact schema history and hide the SQL from review. Hand-written migrations are explicit but easy to get subtly wrong, such as a `NOT NULL` column added as nullable. Some teams write everything by hand for auditability; others auto-migrate by default. When both exist for the same versions, Room runs the manual one. ## Proving coverage 1. A `MigrationTestHelper` test from **every start version still installed** to the current one, with real rows and data assertions. 2. One test that creates the oldest supported version and opens the database through the **production builder**, which catches a migration never passed to `addMigrations()`. 3. These tests run in CI as a release gate, on devices or emulators, because they are instrumented tests. 4. A review checklist: every change to an `@Entity` arrives with a version bump, a new schema file and a migration or `@AutoMigration` entry; a pull request missing any of the three is incomplete. ## Destructive fallback in release builds Room's options, and what each one costs: - `fallbackToDestructiveMigration(dropAllTables)` turns *any* missing path into dropped tables, **including on downgrade**. Room 3.0 defaults `dropAllTables` to `true`; Room 2.8.5 deprecates the no-argument form in favour of this parameter. - `fallbackToDestructiveMigrationFrom(dropAllTables, vararg startVersions)` wipes only from listed versions. `build()` throws `IllegalArgumentException` if any listed version is also a start or end version of a registered migration. - `fallbackToDestructiveMigrationOnDowngrade(dropAllTables)` wipes only when a newer file meets an older build. - `RoomDatabase.Callback.onDestructiveMigration` runs after a wipe, a hook for scheduling a re-sync. A reasonable default policy: - **No blanket fallback in release.** It replaces a crash you would see in crash reports with silent data loss you would not. - **A narrow `...From` list** only after a product decision to stop supporting specific ancient versions, documented next to the migrations. - **Debug builds** are a judgment call: a blanket fallback speeds up prototyping but hides the missing migrations the policy exists to catch. ## Costs and when to revisit - Every supported version adds tests, review time and CI minutes; the list of start versions should follow real install data, not history. - The move to Room 3.0 rewrites every migration to the `SQLiteConnection` form, a reason to keep migrations small and well tested. - Revisit the policy when a crash reaches users, when supported versions pile up, or when the data becomes recoverable from a server, which changes what a wipe costs.
- A developer adds fallbackToDestructiveMigrationFrom(4) and keeps Migration(4, 5) registered. What happens?The builder rejects it: `build()` throws `IllegalArgumentException` saying a migration's start or end version equals a version passed to `fallbackToDestructiveMigrationFrom()`. You must choose for version 4: either migrate it, or wipe it and remove the steps that use it.
- Why does a blanket fallbackToDestructiveMigration also matter for rolled-back releases?It allows destructive migration on downgrade too. If version 8 ships, users upgrade, and the team rolls back to a version-7 build with no `Migration(8, 7)`, those users' tables are dropped on open instead of the app crashing. Whether that is acceptable is a product call, so the policy should name the downgrade case explicitly, often with the narrower `fallbackToDestructiveMigrationOnDowngrade` or none at all.
- A blanket fallbackToDestructiveMigration is a safe default for release builds
- Old schema JSON files can be pruned once a migration ships
- Auto-migrations are always safer than hand-written ones
- Testing the newest migration alone is enough coverage
- Room's destructive fallback catches migrations that produce wrong tables
Assumes Room 3.0, where the fallback functions default dropAllTables to true; in Room 2.8.5 the no-argument fallbackToDestructiveMigration() is deprecated in favour of the dropAllTables overload.