Skip to content

Evolving a schema

Change a collection's schema over time: versions, migrations, apps you can't force to update.

A schema changes. If every client could be updated the moment you deploy, you would change schema.json, fix the data and be done. Mobile apps, desktop apps and other people’s scripts don’t update when you do, so backd lets a collection keep its history: one schema file per version, a small migration between versions, and a server that moves the stored documents up in the background.

Everything on this page is optional. A collection with a plain schema.json works as always; adopt versions when the first breaking change comes (rename schema.json to schema.v1.json and change nothing else).

Versions and files

main/assets/
  schema.v1.json
  schema.v2.json
  schema.v2.ts       the migration from v1 to v2 (TypeScript)
  schema.v3.json     the current version: the highest number
  schema.v3.ts       the migration from v2 to v3
  schema.d.ts        generated by `backd types`: the types the migrations use
  • The highest schema.v<N>.json is the current schema. The server keeps only that one in memory; older files are history, and the input of the tools that follow.
  • Every document records the version it was written under in _meta.schema_version (a document without it is version 1).
  • A migration is a TypeScript function from one version’s document to the next. It is pure and synchronous: no clock, no randomness, no network. That is what lets the server run it more than once on the same document and get the same answer.
  • The file rules, the build and the job are in Versioned schemas.

What happens when you deploy a new version

  1. You add schema.v3.json and schema.v3.ts, run backd types (it writes schema.d.ts) and backd-server functions build (it type-checks and bundles the migrations), and deploy.
  2. At every start, a worker counts the documents below version 3. If there are any, it queues a background job that migrates them in batches while every request is served.
  3. The job moves each document up, validates the result against the current schema and stores it. A document that fails stays as it was and is reported (backd data migrations).

Reads can return mixed versions

While the job runs, a list can hold documents of several versions. There is no maintenance window, and nothing runs on reads, so the server returns every document as stored, with its _meta.schema_version. Your app has to cope for a while:

  • TypeScript and JavaScript call upgrade.mainAssets(doc) from migrations.ts, which runs the server’s own migrations in the app: a document of any kept version comes back as the current one.
  • Dart, Go and Python get one type per version and convert by hand, in a switch or a type guard.

Then it writes the whole converted document with PUT and If-Match. The client pages show it in each language.

What a write stores

Every write is validated against the current schema. It is stamped with the version the app declares in X-Backd-Schema-Version (the clients send it for you), or with the current version when it declares nothing. A version 2 app that edits a document, after version 3 is deployed, stores it as version 2; the next start’s job moves it up again, so nothing an old app wrote is lost.

Apps you can’t force to update

This is the hard case, and this is the only way to keep those apps working. It takes more than one release, and for a while the data is duplicated. Split every breaking change in two:

  1. Expand. The new version adds the new fields next to the old ones, all optional, so writes from the previous version stay valid. Its migration fills the new fields from the old ones. Apps of the new version write both.
  2. Contract. A later version stops declaring the old fields, without forbidding them (no additionalProperties: false for them): apps one version behind may still send them. Its migration drops them, and you raise min_schema_version to turn away the apps that only read the old fields.

Move every client to the current version as soon as you can. Each version still in use is one more branch in every app’s conversion code.

min_schema_version and 426

# collection.yaml
min_schema_version: 2

A request that declares a version below it for that collection answers 426 upgrade_required with {"collection": "main/assets", "min": 2, "declared": 1}, on reads as well (an old app would misread the new shape). The clients raise it as UpgradeRequiredError: show “please update”. It is a compatibility signal, not a security control, and requests that declare nothing (scripts, functions) are never refused.

The build tells you before you deploy

backd-server functions build compares each version with the one below it and warns, never fails, about a new required field, a narrower type, a dropped field and the rest: see Versioned schemas. A finding becomes a note when min_schema_version already turns the affected apps away.

An example: body becomes description

The example realm examples/config/evolve is built and run by the test suite. body becomes description, and every asset gets a visibility, without breaking the apps already installed.

VersionThe document holdsApps that workMigration
v1bodyv1none
v2 (expand)body and description (the same text); visibilityv1, v2schema.v2.ts: copies body to description, sets visibility
v3 (contract)description, visibility; body is no longer declaredv2, v3; min_schema_version: 2 turns v1 apps awayschema.v3.ts: drops body, fills a missing visibility

Version 2 only adds optional fields, so every version 1 write stays valid and the compatibility check is silent:

examples/config/evolve/main/assets/schema.v2.ts

// main/assets, v1 → v2 (expand): description duplicates body, so v1 apps keep reading body;
// every asset gets a visibility.
import type { V1, V2, Meta } from "./schema.d.ts";

export default function (doc: V1, meta: Meta): V2 {
  return {
    ...doc,
    description: doc.body ?? "",
    visibility: doc.published_at ? "public" : meta.org ? "team" : "public",
  };
}

Version 3 stops declaring body (and does not forbid it, so version 2 apps that still send it are not refused):

examples/config/evolve/main/assets/schema.v3.ts

// main/assets, v2 → v3 (contract): body goes; description is the only text.
import type { V2, V3 } from "./schema.d.ts";

export default function (doc: V2): V3 {
  const { body, ...rest } = doc;
  return { ...rest, description: doc.description ?? body ?? "", visibility: doc.visibility ?? "team" };
}

What happens to one asset over time:

  1. Version 3 is deployed. At start the job finds the assets below version 3 and migrates them while every request is served. Lists mix versions until it ends; version 3 apps run upgrade() on the version 2 ones.
  2. A version 2 app, not updated yet, edits an asset and sends body and description. The write is valid against version 3 (body is not forbidden) and is stamped version 2. Version 3 apps reading it upgrade it in memory, which drops body. The next start’s job stores it as version 3.
  3. A version 1 app asks for the list and gets 426 upgrade_required: it shows “please update”.
  4. Later, visibility can become required in version 4, with min_schema_version: 3, once no version 2 app is left. The check warns about it until then.

Operating it

  • See where things stand: backd data migrations (documents below the current version, the latest migration and the documents that did not migrate), the admin UI, and the schema_documents_below_current metric.
  • Run it on demand or rehearse it: backd data migrate --realm R --database D --collection C (--dry-run writes nothing, --wait follows it).
  • Deploy in this order: the server first (a version above the current one is 400 schema_version_unknown), then the apps; contract last. See Deploying.
  • Migrations need an executor and a realm with auth enabled, because they run as background jobs.
Edit this page · Last updated 29 Nov 2026