Migrations

How a collection's schema changes - what a migration is, the steps it goes through, and what clients and services see.

A collection's schema changes through a migration: a piece of code that turns every document from the current schema version into the next one. This page explains how migrations work from start to finish. The following pages cover each step in detail.

What a migration is

A migration takes a collection from version n - 1 to version n. It consists of:

  • the schema documents have now (the source) and the schema they should have (the target), both as primitives;
  • optionally, a function that rewrites a document's content.

You write it in TypeScript with defineMigration from @voidhash/mimic-server/migrate. A build step bundles it into a migration artifact, which you push to the database. The database runs the artifact itself, in a sandbox.

A collection is created at version 1, and each migration takes it to the next version, starting from the schema the previous one produced. In a project's migration registry the migrations are numbered 1, 2, 3 from the collection's baseline; mimic-kit numbers them as the engine does when it applies them (see Building and activating).

Why the server runs it

A migration is not a script you run once against the database. The server keeps the artifact and applies it to each document when the document is next loaded, which may be long after activation and in another server process. For that to be safe, the result must be the same wherever and whenever it runs.

So a migration runs in a sandbox with no network, no file system, no timers, a fixed clock and seeded randomness: its result depends only on the artifact and the document. See Writing a migration for what that means for your code.

The lifecycle

A pushed migration moves through these states:

StateMeaning
verifyingThe server checks the migration against every document of the collection
sealingActivation was requested; the server confirms that every document passes
activeThe collection is at the new version; documents are being migrated
completedEvery document is at the new version
abortedThe migration was abandoned before it became active

Push

You push the artifact. The server checks that it loads and behaves deterministically, and starts verifying: it runs the migration against every document, without changing any, and records which ones pass. Nothing changes for clients. Documents remain at the old version and editable.

Fix what fails

The migration's status lists the documents it fails on, with a code and a path into the document, never the content. Either fix the migration (abort, then push a corrected artifact) or fix the documents.

Activate

Once no document fails, you activate the migration. The server first seals the collection: from then on every write is checked against the migration too, and a write that would make a document fail it is rejected. When a fresh pass over every document finds them all passing, the migration becomes active.

Activation is refused while the status lists failing documents.

Completion

The collection's version is now the migration's target. Each document is migrated when it is next opened, and the server migrates the documents nobody opens in the background. When all are at the new version, the migration is completed.

A migration can be aborted at any point before it is active. An active migration cannot be undone; to go back, write another migration.

The guarantee

A migration becomes active only after it was checked against every document of the collection, and no write can make a checked document fail before activation. There is no state in which some documents migrated and others turned out not to be migratable.

What clients see

Clients built for the old schema keep working until activation. After it, a client that states its schema version (see Schema versions) and is still on the old one is held read-only until it is updated, and its unsent edits written for the old version are reported as not saved. A client built for the new schema that connects before activation is read-only until the server has caught up.

Clients that do not state their schema version are not held read-only. Their writes are checked against the document's current schema, and a value they cannot read stops them from syncing.

What services see

A backend service that passes consumer to the SDK (see Identifying a service) states which collection versions it reads and writes. The engine keeps no record of services and holds no activation back: it refuses that service's request schema_mismatch when the document it reads, or the schema its request names, is outside the declared range. So deploy services that support both versions first, then activate.

A typical rollout

  1. Write the migration and the new schema. Test it.
  2. Deploy backend services that can read and write both versions.
  3. Build the artifact and push it. Wait for verification.
  4. Resolve any failing documents.
  5. Activate, and release the client built for the new schema.

Next