In a health-diary app whose entries exist only on the phone, what should happen when an on-device schema migration fails or finds no path?
answer
- the transaction leaves the last good version
- restorable data may be rebuilt
- device-only data is never wiped
- count attempts outside the failing store
- degrade, tell, export, ship a fix
basics
~20 sThe failed step's transaction rolls back, leaving the file at its last good version. The app never wipes device-only entries: it stops the crash loop, explains, offers an export and waits for a fix. Only restorable data may be rebuilt destructively.
solid answer
~50 sEach step commits in one transaction with its version bump, so a failure rolls the file back to the last version that fully applied, and the data is intact. Failures come from data an old bug wrote, a full disk, a step that was never shipped for an old version, or a kill. A destructive fallback, delete and recreate, is fine for a cache store the server can refill with a resync, and never for diary entries that exist nowhere else. So the app records each attempt outside the database, because a counter inside the failing transaction rolls back with it; after repeated failures it stops retrying at every launch, shows a plain explanation, offers to export the raw file, and reports the starting version and failing step. The next build ships a step that handles that data, and the untouched file migrates then.
go deeper
Recall that a migration can fail, and that deleting the database to recover loses every entry that exists only on the phone.
Explain why each step commits with its version bump in one transaction and what state that leaves after a failure or a kill.
Show the production handling: separate failure kinds, a counter outside the store, no crash loop, an export, a report with the starting version, and a destructive fallback limited to restorable stores.
Weigh the policy for the whole app: which stores may ever be rebuilt, what the stuck-upgrade experience promises users, and how failures by starting version drive the next release.
## Why migrations fail on real phones A migration chain passes its tests and still fails in the field, because the field holds data and conditions the fixtures did not: - **data an old bug wrote**: a null where the schema later assumed a value, a duplicate an earlier version allowed, a date stored in an old format; - **a full disk**: a step that rebuilds a table needs room for a second copy while it works; - **a missing path**: no step was ever shipped from a very old version, or one was removed; - **an interruption**: the user leaves, the battery dies, or the OS ends the process. The design question is what state the data is in afterwards, and what the app does next. ## The state after a failure If each step and its version bump commit **in one transaction**, a failure or a kill rolls back the whole step. The file is left at the last version that fully applied, with every row it had. That is the property everything else rests on: a failed migration must never leave a half-converted file that no build understands. A **missing path** is different: nothing ran, and the build has no code that can read the file. Persistence layers typically refuse to open such a store, which crashes the app at launch, unless a **destructive fallback** is configured. One platform's migration guide spells out what that fallback does: it recreates the tables and permanently deletes all their data. ## Destructive fallback: only for what can be restored | Data | Destructive fallback? | Recovery | |---|---|---| | Downloaded reference list, cached search results | acceptable | refetch with a resync | | Entries already confirmed on the server (backup on) | acceptable only if every row is confirmed | full resync, slow but complete | | Entries written since the last upload, or with backup off | **never** | none: the data is gone | In a health diary most users never opt into backup, so the core store must have no destructive path at all. Splitting disposable caches into their own store lets the app delete and rebuild those freely without putting the diary at risk. ## What the app does next 1. **Count attempts outside the database.** A failure counter written inside the step's transaction rolls back with the step, so the app would never know it had already failed. Keep it in a small separate file. 2. **Stop the crash loop.** After a few failed launches, stop migrating on every start. Each retry burns launch time and, if the failure is a crash, makes the app unusable. 3. **Show a plain explanation** instead of a crash: the diary is safe but cannot open in this version, and an update will fix it. 4. **Offer an export** of the untouched file or a plain-text dump, so the user can keep or share their history while they wait. 5. **Report the failure** with the starting version, the failing step and the error, so the team sees which population is stuck. 6. **Ship a step that handles the bad data**; because the file was never modified, it migrates cleanly on the next build. If disk space allows, copying the database file before a risky step gives a second safety net, at the cost of time and free space the phone may not have. The failure screen is also a promise, so it must be true: if it says the diary is safe, nothing on the failure path may delete, truncate or overwrite the file. That includes well-meant cleanup code that removes "corrupt" databases, which on a stuck upgrade would remove a perfectly good file the app simply cannot read yet. ## Traps to avoid - Turning on a blanket destructive fallback "so the app never crashes": it converts a visible crash into silent loss of years of entries. - Retrying the same failing step on every launch forever. - Catching the error and opening the store anyway, so screens read a schema they do not expect. - Writing steps outside transactions, so a failure leaves a mixed file that the next build's chain cannot start from. ## What the interviewer is listening for That the candidate separates the two failure kinds, rolled-back step and missing path, ties the fallback decision to whether the server can restore the data, and designs the user's experience of a stuck upgrade instead of only the code path.
- Why not copy the whole database before every migration as a backup?It doubles the space the store needs and adds a full copy to the user's wait. Transactions already give each step a clean rollback, so a copy pays only for risky steps, such as a large data rewrite, and only when the phone has room for it.
- The failure is a full disk, not bad data. What changes?The step is correct and will succeed once there is room, so the app should say so: explain that storage is full, keep the file untouched, and retry when space frees up. Shipping a new step would not help, and deleting data to make room is never the migration's call.
- A destructive fallback is a safe default because the app then never crashes.
- A failed step leaves the file half-migrated, so it must be deleted.
- Retrying the migration on every launch will eventually get it through.
- A failure counter stored in the migrating database survives the failed step.
- Data with backup switched on can always be wiped and resynced.