Migrations

Versioned migration of stored templates. Template reference in the DocStudio developer reference.

Public from docstudio/core: migrate, createMigrationRegistry, parseVersion, compareVersions, formatVersion, cloneJson and the related types. Decision record: ADR 0014.

Purpose

Stored templates declare schemaVersion. When the engine's schema changes, migrate upgrades an older template to the supported version on read, so published versions stay immutable and old documents keep rendering. The supported version is TEMPLATE_SCHEMA_VERSION (currently 1.0.0). Schema 1.0.0 is the first version, so the shipped chain is empty; the first real migration arrives with the first schema change and its ADR.

Versions

parseVersion accepts major.minor.patch with each part a decimal number of at most six digits and no leading zeros. Prerelease and build suffixes are not supported. Anything else, including a missing or non-string schemaVersion, is INVALID_VERSION.

Using migrate

const result = migrate(storedTemplate, { registry });

migrate(input, { registry?, target?, limits? }) returns { template, applied, diagnostics }. template is an unvalidated JSON object (run validateTemplate on it next) or undefined when migration failed. applied lists each { from, to } step. The compile pipeline runs migrate first.

Situation Result
Version equals the target A copy, nothing applied
Older version with a complete chain Steps applied in order, each step stamping its own to version
Older version, no path to the target MIGRATION_GAP error at /schemaVersion
Newer major than the target UNSUPPORTED_VERSION error
Newer minor or patch, same major A copy with a NEWER_VERSION warning; validation decides whether unknown fields are acceptable
A step that would pass the target MIGRATION_GAP (steps never overshoot)
A migration throws, or returns anything that is not a bounded JSON object MIGRATION_FAILED; the thrown message is not included
Input beyond the JSON limits LIMIT_EXCEEDED

The input is never mutated, including when it is frozen. The result is an independent copy, and running migrate on its own output applies nothing. A chain is limited to 64 steps.

Writing a migration

A migration is { from, to, apply } registered with createMigrationRegistry([...]) or registry.register(...). from and to are versions with to higher than from; one migration per source version. apply receives a private mutable copy and returns the migrated object. It must be pure, must not depend on the clock or on data, and does not need to set schemaVersion (the engine overwrites it). Keep each migration's fixtures (an input at the old version and the expected output) beside it, and add the new schema version to the conformance tests in the same change.

Registration problems are returned as diagnostics: INVALID_REGISTRATION for a malformed or non-increasing range and DUPLICATE_REGISTRATION for a second migration from the same version.

Safety

Migration code is host code and runs in the host trust domain, like custom components. The engine bounds what it accepts and returns: input and output pass the JSON limits (depth, size, strings, keys), and cloning is iterative so deeply nested input cannot overflow the stack. Keys such as __proto__ are kept as plain data and never touch prototypes.

Performance

Median on macOS (Node 24) for a one-step migration: 259 nodes 2.0 ms, 1,555 nodes 13 ms, 9,331 nodes 85 ms (about 9 microseconds per node, dominated by the two bounds scans and two copies). See validation/artifacts/benchmarks/core-m8-migration.json.