skip to content

In a health-diary app, what should an older build do when it opens an on-device database already migrated by a newer build, as after a rolled-back release?

level: seniorimportance: nice to knowfreq 22%

answer

  1. no step leads downward
  2. a stored version above the known maximum
  3. never delete to fit the old shape
  4. a compatible-with marker beside the version
  5. roll forward, not back

basics

~20 s

It must not migrate down or delete anything. Seeing a stored version above the highest it knows, it leaves the file untouched and asks for the current build, unless the newer build marked the file safe for older code.

solid answer

~50 s

Store updates normally move users forward only, so rolling back a bad release usually means shipping the previous code as a new build, and on every phone that took the bad release that code meets a file already migrated past what it knows. No step leads downward, and a destructive downgrade fallback would delete device-only diary entries. So the open path checks: if the stored version is higher than its own, it does not write; it shows that the diary needs the current version and keeps the data intact. A newer build can make this gentler by storing a `compatible with` version beside the schema version, the oldest code that may safely open the file; additive changes keep it low, meaning changes raise it. The better habit is to roll forward with a fix, or to re-ship old features on top of the newer schema code.

go deeper

for a junior

Recall that migrations only go forward, so an older build that finds a newer stored version has no step to run.

for a middle

Explain how an older build meets a newer schema in practice and why the open path checks for a higher stored version before any query.

for a senior

Show the handling: leave the file untouched, tell the user, report it, and use a compatible-with marker so additive changes keep rollbacks possible.

for a principal

Set the release policy: which schema changes make a release impossible to roll back cleanly, and the rule that bad releases are rolled forward.

## How an older build meets a newer schema Migration chains only run upward: each step goes from N to N+1. A **downgrade** is the case where code that knows only version 8 opens a file already at version 9. On a phone this happens more often than it sounds: - **Rolling back a bad release.** Store updates normally move users forward only, so "rolling back" usually means shipping the previous release's code again as a new build. Every phone that installed the bad release already migrated its database, and the re-shipped code now opens a file one version ahead of it. - **Development and testing.** Engineers and testers install an older build over a newer one and keep its data, which development tooling allows. In both cases the data on the device is the newer shape, and for a health diary much of it exists nowhere else. ## What the older code must not do - **Delete and recreate.** Some persistence layers offer a destructive fallback for exactly this direction; one platform's migration guide documents a variant that recreates the tables only when migrating from a higher version to a lower one. For device-only entries that is permanent data loss. - **Open and write anyway.** The file may hold data whose meaning changed: a field split in two, rows moved to a new table. Old code that writes into it can corrupt rows the newer build will later misread. - **Guess a reverse step.** Nothing in the old build knows what version 9 changed. ## What it should do 1. **Read the version first.** If the stored version is higher than the highest this build knows, take the newer-schema path before any query runs. 2. **Leave the file untouched.** No writes, no migration, no deletion. 3. **Tell the user plainly**: the diary was opened by a newer version of the app, please install the current one. Offer an export if the file can be copied out unchanged. 4. **Report it**, with both versions, so the team sees how many phones a rollback stranded. ## Making older code safe on purpose A newer build can record two numbers: | Stored value | Meaning | Who sets it | |---|---|---| | `schema_version` | the shape the file is in | every migration step | | `compatible_with` | the oldest code version that may read and write it safely | the step that changes meaning | An **additive** change, such as a new table older code never reads, leaves `compatible_with` where it was, so the previous build can still open the file and simply ignore the addition. A change in **meaning**, such as splitting a field or moving data, raises it. The older build then compares its own version with `compatible_with`, not with `schema_version`, and opens normally whenever it is allowed to. This turns most rollbacks into non-events, but only if the newer code tolerates rows the older code wrote, for example rows with the new columns left empty. The marker has a cost. Every schema change now needs a deliberate decision about whether it changes meaning, and getting that decision wrong in the lenient direction lets old code write into a file it misunderstands. Teams that adopt it usually make the decision part of reviewing each migration step, next to the step itself, so it is made by the person who knows what the change does. ## The habit that avoids the problem - **Roll forward.** Ship the fix as the next version with the newer schema code intact. - If old features must return, **re-ship them on top of the newer migration code**, so the build knows version 9 even though its screens look like release 8. - Treat a schema change that raises `compatible_with` as a release you cannot cleanly roll back, and plan it that way. ## What the interviewer is checking Whether the candidate knows the chain has no way back, refuses to buy a clean launch with the user's history, and can name a design, the stored compatibility marker or the roll-forward habit, that keeps a rollback from becoming a data incident.

  • The newer build only added a table that older code never reads. Can the older build open the file?
    Yes, if the newer build left the compatible-with marker at the older version. The old code ignores the extra table and runs normally, and the newer build must then accept that rows written meanwhile did not touch that table.
  • Why not ship a reverse step in each release, just in case?
    A reverse step must run in the older build, which was written before the change existed, so it would have to ship ahead of time in every release and be tested both ways. Reversing a split or a backfill can also lose information. Rolling forward is cheaper and safer.

It is like opening a document saved by next year's word processor in last year's version: a careful program warns you and refuses to save over it, because writing it back in the old format would quietly throw away what it cannot understand.

  • An older build can run the newest migration step backwards to reach its own version.
  • Recreating the database on a downgrade is safe because the app then opens cleanly.
  • Extra columns from a newer build are always harmless, so older code may write freely.
  • Re-shipping the previous release's code restores the previous data shape on users' phones.