Schema migrations

How a migration is pushed, verified, sealed and activated on a deployment, the collection registry API, mimic-kit, and what to do when documents fail.

A collection's schema changes only by a migration: a JavaScript artifact that takes every document from one schema version to the next. Writing one is described under Migrations. This page is the operator's side: how a migration moves through a deployment, the API that drives it, and what to watch.

The life of a migration

StateWhat happens
verifyingEvery partition checks every live document of the collection against the migration, in passes, and counts the documents that pass and fail. Reads and writes are unchanged.
sealingAsked to activate, the deployment makes every write to the collection pass the migration too: one whose result fails it is rejected migration_blocked. Then it checks that every document passes.
activeThe target is the collection's schema version. Each document is migrated before it is next read or opened, and in the background at the database's migration_rate.
completedEvery document is stored at the target.
abortedThe migration was called off before it became active. Nothing was migrated.

A migration becomes active only once every document passes it, and no write can make a checked document fail in between. Activating is refused migration_failing while a partition's last pass found a failing document, and answered unavailable until every partition finished a pass.

Clients see the change as described in Schema versions: a client built for the old schema becomes read-only once its document is migrated.

The collection registry API

A database's collections and migrations are under /v1/admin/databases/{db}/collections. Reading needs read on the database, changing it admin.

RequestDoes
GET …/collectionsLists the collections
GET, PUT, DELETE …/collections/{c}Reads, creates ({ name, schema }) or removes one
GET …/collections/{c}/versionsIts schema versions, with their fingerprints
GET …/collections/{c}/migrationsIts migrations, newest first
POST …/collections/{c}/migrationsPushes a migration: { descriptor, payload, samples? }
GET …/collections/{c}/migrations/{m}A migration's status and progress
POST …/collections/{c}/migrations/{m}/activateActivates it
POST …/collections/{c}/migrations/{m}/abortAborts it
  • A collection is created under an id its caller draws, 32 lowercase hexadecimal digits never used before in the database, with a name and the serialized schema that becomes version 1. Created again the same way it is done.
  • A push carries the artifact's descriptor and its payload in base64, and optionally up to 8 sample documents. It is answered with the migration's id once the deployment has checked the artifact. Another push while a migration of the collection is unfinished is refused migration_in_progress.
  • A migration's status holds its state, its versions, and the counts of its last passes: documentsTotal, documentsPassing, documentsFailing, documentsUnchecked, and, once active, documentsBehind and partitionsActivating; and up to 1,000 failures, each a document id, a code and a path, never content.
  • A migration is active as soon as its target becomes the collection's version, but each partition serves its documents at the old version until its own gate catches up, usually within a second. partitionsActivating counts the partitions not there yet: at 0, every document is read at the target.
  • activate answers at once: the migration becomes active when every partition holds the seal. Read the migration to follow it.
  • DELETE of a collection starts its removal, which succeeds only if it holds no live document (otherwise the removal is lifted with the reason collection_not_empty). A removed collection's id is never used again.

With mimic-kit

mimic-kit, in the repository's packages/mimic-kit, keeps a project's migrations as a folder generated from its migration registry and applies it to a database through this API:

mimic-kit generate   # append what the registry defines past the folder
mimic-kit check      # fail if the folder is not as generated (run it in CI)
mimic-kit migrate    # apply the folder to the target, waiting for each migration to be served everywhere

The target comes from mimic.config.ts or the environment: MIMIC_URL (the deployment's base URL), MIMIC_DATABASE, and MIMIC_CREDENTIAL (a database key with admin, or user:password). migrate reads what the database holds, creates the collections it lacks, pushes and activates each migration not yet applied, and waits until it is active and no partition is still activating (--timeout, 600 seconds by default). It keeps no state of its own, so it can be run again after any failure, and a run that finds a migration already active still waits for its partitions.

When documents fail

  • During verification, the status lists failing documents with the check's code and path. Fix the migration and push it again after aborting this one, or fix the documents (a write that makes them pass, or a delete).
  • While sealing, writes whose result fails the migration are rejected migration_blocked, a recorded outcome. Clients report them as rejected edits.
  • A document that cannot be migrated after activation is held: every request and read but a delete is answered migration_failed, which clients do not retry, and the status lists it. A superuser's repair to a state of the target, or a delete, releases it.
  • A quarantined document (one found corrupt) blocks sealing until it is released (mimicd admin quarantine release) and repaired or deleted.

Background migration

After activation, documents are migrated as they are opened, and the rest in the background at the database's migration_rate: 20 documents per second by default, set in its limits record (migration_rate=0 stops it; documents are then migrated only as they are used). See Limits.

Rolling back

A migration can be aborted while it is verifying or sealing. After activation there is no abort: roll back with a new migration from the target to a version whose schema is the old one, or restore a backup taken before the activation into a new database (see Backups and restores).

Services and schema versions

A backend that sends X-Mimic-Consumer with a range of schema versions (the engine SDK's consumer option) is refused schema_mismatch for documents outside the range. The deployment keeps no record of services and holds no activation back for them: deploy services that support the new version before you activate.