Record migrations

Migrating large documents one record at a time with defineRecordMigration.

A migration written with defineMigration receives the whole document, which limits it to documents of at most 8 MiB. For collections whose documents can grow beyond that, defineRecordMigration migrates a document one record at a time, in bounded memory. This page explains records and how to write such a migration.

Records

The server splits a document into records:

  • the root record: the document's value, with every array and tree inside it left empty;
  • one record per array item;
  • one record per tree node.

A record's value likewise holds the arrays and trees inside it as empty containers; their items and nodes are records of their own. So a board with 200 cards is one root record and 200 item records, and each run sees one of them.

Defining one

import { defineRecordMigration } from "@voidhash/mimic-server/migrate";

export const boardRecords = defineRecordMigration({
  version: 2,
  name: "board-records",
  from: BoardV1,
  to: BoardV2,
  migrateRecord: (record, context) => {
    if (record.kind === "root") {
      return { value: renameAuthor(record.value) };
    }
    if (record.location.join("/") !== "cards") {
      return "keep";
    }
    return { value: withId(record.value, globalThis.crypto.randomUUID()) };
  },
});

renameAuthor and withId stand for your own functions that take an encoded value and return the changed one.

version, name, from and to mean the same as in defineMigration. migrateRecord is called once for every record of the document.

The record

FieldMeaning
kind"root", "item" or "node"
valueThe record's encoded value
posThe position key of an item or node
parentThe parent id of a node
pathThe path of the record in the document
keyThe record's key: the canonical JSON of path
locationThe field names on the path, without ids: [] for the root, ["cards"] for an item of the root's cards

Use location to decide what kind of record you were given.

The answer

migrateRecord returns one of:

  • "keep": the record stays as it is.
  • "delete": the record is removed. If its value holds containers with items, those items must be deleted too, by their own runs.
  • { value, pos?, parent? }: the record's new value and, for an item or a node, optionally a new position or parent.

The framework checks the value you return and writes only the record it gave you. The root's value must keep its arrays and trees empty.

Unlike defineMigration, a record migration works with encoded values, not with typed proxies. The document that results from all the runs together must be valid for the target schema.

Reading other records

context.read(path) returns another record of the document as it was before the migration, as an Option. Every read is remembered: when a record that this one read changes, this one is migrated again.

migrateRecord: (record, context) => {
  if (record.location.join("/") !== "cards") return "keep";
  const board = context.read([]); // the root record
  // …derive the card's new value from the board's settings
},

The context also has documentKey, incarnation and stagedAt (the fixed clock of the run).

Rules

Everything in What a migration can use applies. In addition:

  • Each record is migrated in a fresh, isolated run. migrateRecord must depend only on the record, what it reads through context.read, and the seeded runtime. Do not keep state between calls.
  • Randomness is seeded per record, so an id generated for a card is the same every time that card is migrated.
  • One run may read at most 10,000 records and 64 MiB.
  • Each record is at most 8 MiB.

Testing

runRecordMigration from @voidhash/mimic-server/migrate/artifact runs a record migration on a whole document in Node exactly as the server does, one run per record, and checks that the result is valid for the target schema. See Testing migrations.

Building

buildMigrationArtifact recognizes a record migration and builds an artifact that the server runs once per record. The build and activation steps are the same as for any migration; see Building and activating.