Definition
A schema migration is a conversion of stored data from the layout that old software wrote to the layout that new software expects. The layout is called a schema: the description of a stored record’s fields, their sizes, order and byte encoding, and their meaning. The migration reads each record in the old schema, checks it, writes the same information in the new schema, and marks the result with the new schema’s version, so that readers can tell the schemas apart. It must survive a reset part way through.
The idea
Version 1 of a device’s software stores a count of events as a 2-byte number. Version 2 needs room for larger counts and stores a 4-byte number. If version 2 simply reads 4 bytes where version 1 wrote 2, the extra 2 bytes are whatever happens to follow: leftover data, or the value 0xff that erased flash memory §6.6 reads as on most microcontrollers §6.1. The count comes out wildly wrong, and nothing reports an error. Software changes; stored data stays as it was written, and must be converted explicitly.
Moving house box by box is a fair picture. Each box is repacked into a new labeled box, and no old box is thrown away until its new box has safely arrived, so a move that stops halfway loses nothing and the labels show where it stopped. The picture stops where space is tight: a device may lack room for a second copy, and then the old box must be emptied while the new one is filled, which needs a written record of progress.
A reset in the middle of a migration is the hard case. Recovery must know, for every record, which schema it uses and whether it is complete. Guessing from whichever bytes happen to look sensible is how a half-written record gets read as a valid new one.
How it works
Four rules for the stored formats make migration safe.
Every record says what it is. It begins with a schema version tag and a length, and ends with a check value such as a CRC §4.32 (cyclic redundancy check), a short code computed from the other bytes that stops matching them after most kinds of damage; a 32-bit CRC catches every change to a single bit and every change confined to 32 consecutive bits.
The stored format is defined byte by byte, including endianness §4.21, the order in which the bytes of a multi-byte number are stored. Defining that format is serialization §4.28: turning data into a defined sequence of bytes. Copying the raw memory of a program’s data structure is not a defined format, because a compiler change can alter the padding (unused bytes the compiler places between fields) and with it the layout.
Old records stay readable until they are converted, and the converting code stays until no old record can remain.
One write decides which copy counts.
A migration that writes a separate copy then runs in this order.
At start-up, read the record that the selector, a small stored record naming the current copy, points to, and check its tag, length and check value.
If the record already uses the new schema and passes its checks, use it; there is nothing to migrate.
If it uses the old schema, check every field, including value ranges.
Build the new record in RAM and write it to free storage, leaving the old record untouched.
Read the new record back and check it.
Commit: point the selector at the new copy with one write that recovery sees as all or nothing, for example a selector record with its own check value.
Erase the old record only after the commit, and only when no software that may still run needs it.
A reset before step 6 leaves the old record selected and intact. The partial new copy is not selected, so recovery erases it and runs the migration again from the same old record, which gives the same result because its input has not changed. A reset after step 6 leaves the complete new record selected. When there is no room for a second copy, the migration converts in place and needs write-ahead logging §8.12: before overwriting part of the old record, it stores in a journal what it is about to do and the bytes it will overwrite, so that recovery can either finish the step or undo it.
Migration must also agree with the choice of software image. Many devices keep the previous software image so that a recovery fallback §8.29 can start it if the new one fails. The previous image understands only the old schema. If the old record is erased before the new software has been confirmed as working, a fallback starts old software on data it cannot read.
Worked example
Schema 1 stores the count as 2 bytes, least significant byte first (little-endian), with no tag; it was designed before tags were used, and its records are recognized by their length of 2 bytes, which no schema 2 record has. Schema 2 stores a 1-byte tag 0x02, then the count as 4 bytes, little-endian, then a 2-byte check value, 7 bytes in all. The stored count is 258. Since , it is 0x0102 in hexadecimal.
Schema 1 bytes:
02 01.Schema 2 count bytes: 258 as a 4-byte number is
0x00000102, stored as02 01 00 00. The record is02, then02 01 00 00, then the 2 check bytes.
The careless upgrade. New software reads 4 bytes where schema 1 wrote 2, and the 2 bytes that follow are erased flash, ff ff. It reads 02 01 ff ff, which as a little-endian number is 0xffff0102, that is . A count of 258 has become more than 4 billion, and no error is reported.
A reset during a correct migration. The migration has written the tag 02 and the first 2 count bytes 02 01 of the new copy when the reset comes. The 7 bytes of the new copy read 02 02 01 ff ff ff ff. A reader that trusted the tag would take the count as 0xffff0102 again. Under the rules above no reader gets that far: the selector still names the schema 1 record, so the partial copy is never read as a record. Recovery erases it and runs the migration again from 02 01, which still means 258.
A conversion that cannot be repeated. Suppose schema 2 also changes a time field from milliseconds to microseconds, and an in-place migration multiplies the field by 1000 before it updates the tag. A field of 5 milliseconds becomes 5000 microseconds. A reset before the tag is updated leaves the tag saying schema 1, so the rerun multiplies again: microseconds, which is 5 seconds instead of 5 milliseconds. A journal entry, or a separate copy, would have prevented the double conversion.
Stated precisely
Let be the set of byte strings that are complete, valid records in schema , let be the information that a record in stands for, and let (mu) be the migration function from schema 1 to schema 2. A migration is correct when four conditions hold.
Meaning preserved: for every , and . In the example, both records mean the count 258.
Unambiguous recognition: no byte string is in both and , and no partly written record is in either. Tags, lengths and check values provide this.
Atomic publication: after a reset at any moment, the selector names exactly one record, and that record is in or in .
Compatibility: every software image that may start, including a fallback image, can read the schema of the selected record.
Condition 3 rests on two assumptions: the write that moves the selector is all or nothing as recovery sees it, and the old record is not erased before that write. Running the migration again after a reset is safe because its input, the old record, does not change before the commit. A migration that changes its own input before the commit is safe to run again only if applying it twice gives the same result as applying it once, which multiplying by 1000 does not.
Limits and common mistakes
Reading an old format is not the same as writing a new one safely. Software that can read both schemas still needs the ordered steps to convert without loss.
A fallback to older software after a migration can fail even though the old image is intact and its digital signature §5.14 checks, because it cannot read the new data. Keep the old record until the new software is confirmed, or make the old software refuse unknown schema versions safely.
Guessing the schema from a record’s size, or from whether its values look plausible, is unsafe: the half-written copy in the example has the right size.
A new schema that narrows a field, for example from 4 bytes to 2, cannot hold every old value. The migration must state what happens to a value that does not fit instead of cutting it off silently.
A schema change is not only a change of field sizes. Changing the byte order, or the meaning of a field, such as its unit, while every size stays the same, is also a schema change and needs a new version number.
Migrations chain. Software that may meet records written by versions 1, 2 and 3 needs a migration for each step, and each must be tested on records written by the old software itself, not on records the new software made up.
Questions answered
What happens if reset occurs halfway through migration?
With a correctly designed migration, nothing is lost: the reset leaves the old record selected and intact, and the migration runs again after the restart. That outcome depends on three rules. The new record is written to separate storage, or every in-place step is first recorded in a journal; the new record becomes current only through one final all-or-nothing write, such as moving a selector; and the old record is erased only after that write. Recovery then reads the selector, finds the complete old record, discards the partial new copy, and repeats the conversion from the unchanged old data. A reset after the final write leaves the complete new record in force. Without those rules, the reset leaves a record that belongs to neither schema. Software may read it as the new schema and get wrong values, such as a count of 258 read as 4,294,902,018, or may rerun a conversion on data that was already converted.
Related entries: Serialization (§4.28), Deserialization (§4.29), Recovery fallback (§8.29), Crash consistency (§8.4), Endianness (§4.21), Write-ahead logging (§8.12).