Block migrations
The structure of a block may change over time.
Consider the following example: The eyebrow field in a headline block should be changed from a basic string to a rich text block to support formatting.
class HeadlineBlockData extends BlockData {
@BlockField()
eyebrow: string;
@ChildBlock(RichTextBlock)
eyebrow: unknown;
}
This change in the block structure is incompatible with previously created block instances in the "old" block structure. Consequently, we need to add a block migration to migrate old blocks to the new format.
Block migrations are performed when a block instance is loaded from the database. As this operation happens just in time, we sometimes refer to them as live migrations.
Block instances will be migrated to the new block structure before being sent to a client. The clients do not need to support the old and new block structures.
A block may require multiple changes during its lifetime.
Each change in the block structure is declared by increasing the block's version field.
The block's version is stored alongside the block data in the database.
Initially, a block's version is either undefined or 0.
export const HeadlineBlock = createBlock(HeadlineBlockData, HeadlineBlockInput, {
name: "Headline",
migrate: {
version: 1, // Current version in the code
},
});