Migrations
Learn how to change your schema while older clients keep working.Why Jazz migrations are different
Most migration systems are one-way: rewrite every row to the new shape, then cut over. That assumes you can stop the world long enough to upgrade — which doesn't hold when your clients are local-first, frequently offline, and updating their app on their own schedule.
Jazz keeps every schema version usable and translates rows between them on read and write. Clients on different versions stay interoperable, and nothing on disk is rewritten when you ship a new schema.
Schemas and lenses
Every unique version of your schema.ts has a hash which can be used to refer to it. When creating migrations, you describe the changes required to move between two schema versions. This is known as a 'lens'. Fetching all intermediate lenses allows clients with any published schema version to read data created with any other published schema version.
Choose data from the left, then pick a schema version on the client device. The row is loaded as-is, and the client applies all the lenses needed to interpret it using its schema.
a01f5c
title: s.string()
311995
title: s.string() completed: s.boolean()
73b65d
title: s.string() done: s.boolean()
Rows retain the physical schema identity under which they were written. A read in another compatible schema uses lenses to project those rows into the requested shape. Non-adjacent reads compose lenses in sequence to bridge multiple schema versions.
In practice, this lets you:
- Ship a schema change without waiting for every user to update their app.
- Roll out platform-by-platform (mobile, desktop, web) on independent cadences.
- Accept writes from clients that have been offline since before the new schema landed.
Workflow
-
If this is the first migration you are creating, run:
npx jazz-tools migrations createThis creates an initial snapshot of your schema in
migrations/snapshots/. No migration file is created yet because there is no previous schema to diff against.If
schema.tsstill passes{ migrations: "none" }todefineApp, switch it to{ migrations }now, as in Shipping migrations with your app. -
Edit
schema.ts— change the data shape as needed. -
Validate locally — run
npx jazz-tools validateafter each schema change. It regeneratesmigrations/index.tsand surfaces any policy diagnostics without publishing. Your main backend runs the same checks when it starts, sovalidateis also a fast check before you deploy, or in CI. -
Create a migration stub for the updated schema — run:
npx jazz-tools migrations create --name <your-migration-name>By default, Jazz diffs the latest committed snapshot in
migrations/snapshots/against the current schema and writes a stub migration file intomigrations/. It also saves a snapshot of the generated schema. -
Review and customise — the migration, if needed (see below).
-
Deploy your main backend — restart it locally, or deploy it as usual. When it starts,
createJazzsends the schema, permissions and migrations your code imports to its store as one bundle. There is no separate deploy or push command. If the bundle has no migration path from the schema already in the store, the store refuses it with an error naming the missing migration:jazz.ready()rejects with it, and Jazz's routes answer 503 and log it. In development (NODE_ENV=development) the 503 carries the error too, so the browser console shows it. Callawait jazz.ready()at startup if you want a refused bundle to stop the process. First deployments and permission-only changes need no migration.With several instances, roll out to one first and check it starts before replacing the rest, and keep the previous version ready to roll back to.
Each deployment's sessions run under that deployment's own bundle, so two deployments can run side by side, and rolling back your backend rolls back its permissions too.
Browser tabs opened before the deploy keep running the old app and its schema until they reload; Jazz has no reload prompt yet, so add your own if you need one. Serve
index.htmlwithCache-Control: no-cache, or browsers can keep loading the old bundle even after a reload.
The jazz-tools CLI only scaffolds and checks migrations (migrations create, validate,
migrations graph). It never publishes anything: only your main backend does.
Shipping migrations with your app
validate and migrations create also regenerate migrations/index.ts. It bundles every schema version and
every lens, and your app passes it to defineApp. They only write it once schema.ts imports it,
so if your app still has { migrations: "none" }, make this change first; the next validate
creates the file:
schema.tsimport { migrations } from "./migrations";export const app = s.defineApp(schema, { migrations });
Commit it with your schema. It changes with every migration, so merging two branches that each
added one always conflicts there: take either side, then run validate to regenerate it. Clients use it to read and write data from any version, including
before they first sync, so a client never has to rename or reshape stored data later. A client
whose schema version is missing from the bundle refuses to open with an error telling you to
regenerate it.
Index changes (indexOnly, compositeIndex) don't need a migration either: they change how
rows are found, not the rows, so the backend deploys them as they are.
Permission-only changes in permissions.ts don't need a migration; they go live when your main
backend restarts with them. See Declarative Permissions for details.
The migration file
The generated stub describes the diff as declarative operations which carry enough information to run in either direction. That is how older clients can still read data written under a newer schema: the same operations replay in reverse.
Generated stub
Here's a generated stub for adding a description column:
migrations/20260318-unnamed-a01f5c72ec47-311995e9a178.tsimport { schema as s, migration as m } from "jazz-tools";export default m.defineMigration({migrate: {todos: {description: m.add.string({ default: null }),},},fromHash: "a01f5c72ec47",toHash: "311995e9a178",from: {todos: s.table({title: s.string(),done: s.boolean(),parentId: s.uuid().optional(),projectId: s.uuid().optional(),owner_id: s.string(),},{parent: s.rel("todos", "parentId"),children: s.reverse("todos", "parent"),project: s.rel("projects", "projectId"),},),},to: {todos: s.table({title: s.string(),done: s.boolean(),description: s.string().optional(),parentId: s.uuid().optional(),projectId: s.uuid().optional(),owner_id: s.string(),},{parent: s.rel("todos", "parentId"),children: s.reverse("todos", "parent"),project: s.rel("projects", "projectId"),},),},});
Customising defaults
Review generated defaults before you publish. For example, you might replace a nullable default with a domain-specific value:
migrations/20260318-add-description-a01f5c72ec47-311995e9a178.tsimport { schema as s, migration as m } from "jazz-tools";// Example of editing a generated migration stub.export default m.defineMigration({migrate: {todos: {description: m.add.string({ default: "No description" }),},},fromHash: "a01f5c72ec47",toHash: "311995e9a178",from: {todos: s.table({title: s.string(),done: s.boolean(),parentId: s.uuid().optional(),projectId: s.uuid().optional(),owner_id: s.string(),},{parent: s.rel("todos", "parentId"),children: s.reverse("todos", "parent"),project: s.rel("projects", "projectId"),},),},to: {todos: s.table({title: s.string(),done: s.boolean(),description: s.string().optional(),parentId: s.uuid().optional(),projectId: s.uuid().optional(),owner_id: s.string(),},{parent: s.rel("todos", "parentId"),children: s.reverse("todos", "parent"),project: s.rel("projects", "projectId"),},),},});
Backwards defaults
When a newer schema drops a column that older clients still expect, define a backwards default so the lens can supply a value for those clients:
migrations/20260318-drop-legacy-priority-311995e9a178-73b65d082ab8.tsimport { schema as s, migration as m } from "jazz-tools";// Example: dropping a column with a backwards default.// Clients still on the older schema continue seeing legacy_priority.export default m.defineMigration({migrate: {todos: {legacy_priority: m.drop.int({ backwardsDefault: 0 }),},},fromHash: "311995e9a178",toHash: "73b65d082ab8",from: {todos: s.table({title: s.string(),done: s.boolean(),description: s.string().optional(),parentId: s.uuid().optional(),projectId: s.uuid().optional(),owner_id: s.string(),legacy_priority: s.int(),},{parent: s.rel("todos", "parentId"),children: s.reverse("todos", "parent"),project: s.rel("projects", "projectId"),},),},to: {todos: s.table({title: s.string(),done: s.boolean(),description: s.string().optional(),parentId: s.uuid().optional(),projectId: s.uuid().optional(),owner_id: s.string(),},{parent: s.rel("todos", "parentId"),children: s.reverse("todos", "parent"),project: s.rel("projects", "projectId"),},),},});
Operations
A migration's migrate maps each changed table to its column operations; tables themselves are
added, dropped and renamed next to it:
migrations/20260401-rename-and-split.tsimport { migration as m } from "jazz-tools";export default m.defineMigration({migrate: {todos: {notes: m.renameFrom("description"), // same type, new namepriority: m.add.int({ default: 0 }), // a new column and its value for old rowslegacy_flag: m.drop.boolean({ backwardsDefault: false }), // what older clients read},},renameTables: { projects: m.renameTableFrom("lists") },createTables: { labels: true },dropTables: { archived_todos: true },from: {/* the old schema's affected tables */},to: {/* the new schema's affected tables */},});
m.add.<type>({ default })adds a column (string,int,bigint,boolean,timestampand the other column types). The default fills it for rows written under the older schema.m.drop.<type>({ backwardsDefault })drops a column; older clients readbackwardsDefault.m.renameFrom("oldName")renames a column of the same type. Values carry over.renameTables,createTablesanddropTablescover tables that exist on only one side.
After editing a stub, run jazz-tools validate (and your type checker): it rejects a migration that
doesn't cover every change, while a stub with a TODO left in it can still pass type checks.
Adding enum variants
An enum column, or an array of enums, can gain variants at the end of its list. Rows written
under the older schema hold only variants the new one still declares, so they read unchanged and
sort as before, and the migration needs no step. A variant's position in the list is its sort
order, so adding one elsewhere, or reordering the list, is refused. jazz-tools migrations create writes a stub with
nothing to fill in, noting which column gains which variants:
migrations/20261113-add-blocked-status.tsimport { migration as m } from "jazz-tools";export default m.defineMigration({migrate: {tasks: {// Column "status" gains enum variant "blocked": existing rows are unchanged, so it needs no step.},},// fromHash, toHash, from and to as generated});
Clients still on the older schema read a row holding the new variant with its name as it is (here
"blocked"), even though their own types don't list it, so handle unknown values where they read
the column. Their own writes are still checked against the variants their schema declares: an
older client can't write "blocked".
Changes that aren't supported yet
- Renaming, removing or reordering enum variants. The CLI refuses a variant missing from the new list, or one that moved, and so does a deploy. Adding variants at the end works, as above.
- Changing a column's type. The CLI refuses it. Adding a column under a new name and dropping the old one works, but it does not carry values over: every row gets the new column's default, with no warning. There is no value-mapping step yet.
- Making an optional column required. There's no step for it yet, and adding a required column while dropping the optional one resets every row to the default, as above.
- Moving data between tables. A lens maps one table's rows; it can't copy values into another table.
For these, expand and contract over two releases. First add the new column or table next to the old one, have your app write both, and copy existing values with a backend job. Once every row is copied and no client still needs the old shape, drop the old column in a later release. Keep the copy job in the release before the drop: once a release drops a column, code on that release can no longer read it.
Adding relations to existing UUID columns
To add a relationship using an existing ownerId UUID column, declare owner: s.rel("users", "ownerId") in the table's relationships and follow the migration workflow above. Existing UUID values stay unchanged; Jazz emits an explicit identity table lens, which preserves row values while making the relation available under the new schema.
Handling concurrent migrations
When working on real apps, it's normal for multiple devs (or agents) to create migrations starting from the same schema but branching off in different directions.
For example, suppose your task app has a tasks table with a title column. Alice adds a dueDate column while Bob independently adds priority, both starting from the original schema:
Original tasks├──add dueDate───→ Tasks with due dates (Alice's backend)└──add priority──→ Tasks with priorities (Bob's backend)
Each deployment publishes its own bundle and its sessions run under it, so Alice's and Bob's backends (two preview deployments, say) can both serve the same data side by side. Nothing on the server becomes "current", and neither deployment blocks the other. A bundle's migration history must have a single root, which both branches share here.
Reading across the branches has a cost, though. Jazz reads a row written under another schema by
applying lenses backwards to the common ancestor and forwards again, and that route can discard
values. A task written with a due date loses that field when read from the priorities schema, and
adding dueDate again later along that route supplies a default instead of the task's original due
date.
So while both branches run, each side sees the other's rows without the other's new column. When you're ready to ship, pick one branch or converge both into a merged schema.
Picking one branch
To leave Alice's due-date change behind, deploy Bob's backend and stop deploying Alice's. Rolling back works the same way: redeploy older code, and its sessions run under its own bundle again. No reverse migration is needed, and you shouldn't write one: it would create a cycle, which your main backend rejects on start.
Converging into a merged schema
To preserve both changes, edit schema.ts so tasks contains both dueDate and priority. Create a migration into this merged schema from each branch:
┌──→ Due dates ──add priority──┐Original tasks ┤ ├──→ Due dates + priorities└──→ Priorities ──add dueDate──┘
Use the hashes shown by migrations graph:
npx jazz-tools migrations create <appId> --fromHash <due-dates-hash> --name add-priority-to-due-datesnpx jazz-tools migrations create <appId> --fromHash <priorities-hash> --name add-due-date-to-priorities
Both commands use the current schema.ts as their destination. Review the generated migrations:
- The migration from Alice's schema should preserve
dueDateand introducepriority. - The migration from Bob's schema should preserve
priorityand introducedueDate.
And then deploy your main backend.
Rows written under either branch now read with both columns intact. Jazz validates that alternative forward paths agree on the values and column identities they preserve or introduce.
Visualizing the migration graph
Run jazz-tools migrations graph to see the schemas and migrations. It reads the server's history,
so it needs a Jazz Cloud app or a remote()
store; against a backend's own sqlite() store it fails with a 404. The same holds for
--fromHash and --toHash below when the hash isn't saved locally.
npx jazz-tools migrations graph <appId> --server-url <url> --admin-secret <secret>
The graph includes all schemas and migrations present in the server and locally. Schemas and migrations that exist only on the server or locally are labelled accordingly.
The graph also marks the local schema.ts and, with "(latest bundle)", the schema of the bundle your
backends published most recently.
Choosing migration endpoints
By default, migrations create compares the latest local snapshot with the current schema.ts. With
no local snapshot, it starts from the latest bundle's schema.
Use explicit schema hashes to choose different endpoints, for example when connecting schemas developed on separate branches:
npx jazz-tools migrations create <appId> --fromHash <fromHash>npx jazz-tools migrations create <appId> --fromHash <fromHash> --toHash <toHash>
--toHash defaults to the current local schema. When a requested hash is not already saved locally, Jazz resolves it from the
server and saves a snapshot in migrations/snapshots/. Remote lookup requires the app ID, server URL, and admin secret;
provide the server URL and secret through --server-url and --admin-secret or their environment variables.
Compiling the schema
Use schema compile when working on a non-TypeScript Jazz application that needs to consume the schema written in TypeScript.
You don't need to run it as part of the normal migration workflow: migrations create saves
the required snapshots, and your main backend compiles the schema when it starts.
npx jazz-tools schema compile prints the compiled schema as JSON to stdout. It
also saves a snapshot of the schema in the local snapshot directory.
npx jazz-tools schema compilenpx jazz-tools schema compile --schema-dir ./packages/app
The command compiles the current local schema.ts. To create migrations against a historical
schema, use migrations create --fromHash <hash>; it fetches and saves missing snapshots as needed.
| Flag | Default | Description |
|---|---|---|
--schema-dir <path> | current directory | Path to app root containing schema.ts |
--migrations-dir <p> | ./migrations | Path to migrations directory and snapshot folder |
Migration flags
migrations create uses flags rather than positional hashes. When it needs to resolve missing
schema hashes from a server, pass <appId> as the leading positional argument.
| Flag | Default | Description |
|---|---|---|
--schema-dir <path> | current directory | Path to app root containing schema.ts |
--migrations-dir <p> | ./migrations | Path to migrations directory and snapshot folder |
--server-url <url> | JAZZ_SERVER_URL | Server URL used when resolving missing schema |
--admin-secret <sec> | JAZZ_ADMIN_SECRET | Admin secret used when resolving missing schema |
--fromHash <hash> | latest local snapshot, else the latest bundle's schema | Optional source schema hash |
--toHash <hash> | current schema | Optional target schema hash |
--name <name> | unnamed | Optional migration filename label |
Next steps
- Defining Tables — table and column definitions
- Column Types — full list of available column types