Building and activating
Building a migration artifact, pushing it to a database, watching verification, and activating it, with mimic-kit or the collection registry API.
Once a migration is written and tested, three things remain: build its
artifact, push it, and activate it. mimic-kit does all three from your
project's migration registry, and is the way to use in a deployment
pipeline. This page explains what it does, and how to do the same by hand
through the database's collection registry API. Changing a collection needs
an admin database key (or a superuser).
Versions
The engine creates a collection at schema version 1, and each migration
takes it one version further: a migration's descriptor names the version it
starts from (from_version) and the one it produces (to_version), and a
push whose from_version is not the collection's active version is refused
schema_mismatch.
- In a migration registry (
defineMigrationRegistry), a collection's migrations are numbered 1, 2, 3 and so on from its baseline.mimic-kitnumbers them as the engine does when it generates the folder: a new collection is created at the latest schema the registry defines, and each migration added later produces the next collection version. - A migration pushed by hand names in
versionthe collection version it produces: one past the active version (2 for a collection's first migration). The descriptor'sfrom_versionisversionminus one.
With mimic-kit
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, waiting for each migration to become activegenerate writes each new collection and each new migration's artifact into
the project's migrations folder, built reproducibly; commit the folder.
migrate creates the collections the database lacks, pushes each migration
it does not have yet, waits for verification, activates it and waits until
it is active, in the folder's order. Run again, it sends nothing for what is
applied. Its target and credential come from mimic.config.ts or the
environment; see Schema migrations.
Building an artifact by hand
A migration artifact is what the database stores and runs: a JavaScript
bundle of your migration and a descriptor that identifies it.
buildMigrationArtifact from @voidhash/mimic-server/migrate/artifact
produces both. It runs in Node and returns an Effect:
// scripts/build-migration.ts
import { Effect } from "effect";
import { buildMigrationArtifact } from "@voidhash/mimic-server/migrate/artifact";
const artifact = await Effect.runPromise(
buildMigrationArtifact({
module: "src/migrations/add-slug.ts",
exportName: "addSlug",
}),
);
console.log(artifact.artifactHash, artifact.descriptor.to_version);module is the file that exports the migration and exportName the name of
the export, a defineMigration or defineRecordMigration step.
The build bundles the migration, its schemas and everything they import into
one script, loads the bundle once to make sure it works, and reads the
versions, the name and the schemas from the bundle itself. It fails with
MigrationArtifactBuildError when the module cannot be bundled, the export
is missing, or the bundle does not load.
| Field | Meaning |
|---|---|
descriptor | The descriptor as an object: versions, schema fingerprints, the target schema and hashes |
descriptorJson | The descriptor's exact bytes. Send these unchanged: they are the artifact's identity |
bundle | The bundled script |
artifactHash | The artifact's identity, as the engine computes it |
Builds are reproducible: building the same sources again gives the same
artifactHash. Build the artifact in CI, from a commit, rather than on a
developer's machine. mimicd migrations build builds one too, into a
directory (descriptor.json and bundle.js).
Pushing
A push is POST /v1/admin/databases/{db}/collections/{c}/migrations with
the descriptor's exact text and the bundle in base64:
const collection = `${gateway}/v1/admin/databases/${database}/collections/${collectionId}`;
const headers = { authorization: `Bearer ${adminKey}`, "content-type": "application/json" };
const pushed = await fetch(`${collection}/migrations`, {
method: "POST",
headers,
body: JSON.stringify({
descriptor: artifact.descriptorJson,
payload: Buffer.from(artifact.bundle).toString("base64"),
}),
});
const { id } = await pushed.json(); // the migration's idPushing starts verification: the database checks the artifact in its sandboxes and then runs the migration against every document of the collection without changing any. Clients are not affected. The push is answered once the artifact was checked, which can take a while for a large artifact.
A push is refused, with the engine's { code, message }:
schema_mismatch, when itsfrom_versionis not the collection's active version or its source schema is not the active schema;migration_in_progress, while another migration of the collection is verifying, sealing, or active and not completed: it finishes or is aborted first;invalid_artifact, when the descriptor, the bundle or the checks fail;resource_exhausted, while the database is checking as many pushes as it allows at once. Try again a little later.
Pushing the artifact being verified again answers that migration, so a push whose answer was lost can be sent again.
Watching verification
GET …/migrations/{id} answers the migration's status:
| Field | Meaning |
|---|---|
id | The migration's id |
artifact | The artifact hash |
fromVersion, toVersion | The versions it migrates between |
state | verifying, sealing, active, completed or aborted |
documentsTotal | Documents in the collection |
documentsPassing | Documents the migration succeeds on |
documentsFailing | Documents it fails on |
documentsUnchecked | Documents not checked yet |
failures | Up to 1,000 failing documents: documentId, code, path, never content |
Verification is done when documentsUnchecked is 0. GET …/migrations
lists the collection's migrations, newest first, and
GET …/collections/{c} includes the current or most recent one.
Dealing with failures
For each failing document, either the migration is wrong or the document is.
- If the migration is wrong, abort it, fix the code, build a new artifact and push that.
- If the document is the problem, fix or delete the document, and then read the status again.
Reproduce a failure locally by reading the document and passing its value to
runDirectMigration; see
Testing migrations.
Activating
await fetch(`${collection}/migrations/${id}/activate`, { method: "POST", headers, body: "{}" });activate answers at once. The collection is first sealed: every write
is now also checked against the migration, and a write that would make a
document fail it is rejected migration_blocked. Once every document
passes, the migration becomes active and the collection is at the new
version. Read the migration until its state is active, then
completed: every document is then at the new version.
Activation is refused migration_failing while the status lists failing
documents, and answered unavailable until every partition finished a
verification pass: ask again shortly.
Aborting
await fetch(`${collection}/migrations/${id}/abort`, { method: "POST", headers, body: "{}" });A migration can be aborted while it is verifying or sealing. Once it is
active it cannot be aborted; to go back, write another migration.
After activation
GET …/collections/{c}reports the newversion, andGET …/collections/{c}/versionslists the new schema.- Release clients built with the new schema and the new
schemaVersion; see Schema versions. - Backend services can drop support for the old version in a later release.