Reference
Migrations
Schema and permission changes roll out with clientdb migrate: a dry run first, then versioned migrations, online re-routing, recomputed memberships and pruning. Old clients reload.
Every deploy runs one command after your database migrations. It brings clientdb's stored state in line with the new code: rows routed by changed rules move to the right devices, rows written outside clientdb are picked up, and old log entries are pruned.
npx prisma migrate deploy
npx clientdb migrate src/sync/engine.tsclientdb: migrated (applied nothing · rerouted todo · reconciled 3 todo · recomputed viewer groups)
clientdb: pruned (1204 log entries · 380 transaction ids · archived 2 project)The module is the one that exports your engine (add --export <name> when it exports more than one). The command runs engine.migrate(), then engine.prune() when the engine sets retention, prints what they did and closes the pool. It needs a direct database connection (see Postgres and Neon).
Dry run
--dry-run prints what the command would do and changes nothing. Run it in CI or before a risky deploy:
npx clientdb migrate src/sync/engine.ts --dry-runclientdb: dry run, nothing applied (would apply nothing · schema changes addField todo.dueAt · would reroute todo · would adopt nothing · no capture triggers on Tag (run npx clientdb-prisma triggers) · would prune 1204 log entries, 380 transaction ids, archived 2 project)From code, engine.migrationPlan() returns the same as an object: pending migrations, schema changes, models to re-route, rows to adopt, missing capture triggers, and what pruning would delete. Its refusal says why migrate() would stop, or is null.
Old clients reload, by design
The schema has a stable hash, also computed per model and per field. It covers fields, relations (with onDelete and eager) and each model's load mode, so a client still running an older bundle can't, for example, treat a model that became lazy as fully on the device. Every bootstrap and pull carries the hash, and a client whose hash differs stops with SchemaOutdatedError. There are no compatibility shims or support windows: every client runs the current schema.
On the client, db.$status becomes "outdated". Show a "new version available" prompt and reload.
With storage on, only the models whose shape changed are dropped and fetched again (GET …/bootstrap?models=…); everything else stays on disk.
What engine.migrate() does
It takes a session lock, so concurrent deploys run it one at a time, and compares the stored schema and compiled rules with the current ones:
- applies versioned migrations (engine-owned tables), once each, each in its own transaction;
- re-routes models whose rules or shape changed, online, in batches. Changing only
load,eageroronDeleteneeds no migration: it changes the hash clients compare, not the stored data; - recomputes everyone's groups when group kinds change;
- on Prisma tables, syncs rows written outside clientdb that devices haven't received (seed scripts, tools that ran before the change triggers existed), as
engine.reconcile()does.
import { engine } from "../src/sync/engine";
const { applied, rerouted, reconciled, viewersRecomputed } = await engine.migrate();
console.log({ applied, rerouted, reconciled, viewersRecomputed }); // reconciled: { todo: 3 }, rows per model
await engine.close();Because every run reconciles, run it again after seeding a database without triggers. See Writes made outside clientdb.
Prisma tables
prisma migrate owns all DDL. clientdb's part, per deploy, is the command above.
- New or removed models: regenerate the change triggers so every model's table has them, as a Prisma migration. The dry run lists tables without triggers.
mkdir -p prisma/migrations/20261202_clientdb_triggers
npx clientdb-prisma triggers src/sync/schema.ts > prisma/migrations/20261202_clientdb_triggers/migration.sql
npx prisma migrate dev- Upgrading clientdb: run
npx clientdb-prisma modelsagain and migrate, so clientdb's tables have every column the new version uses.clientdb migratestops with an error naming any missing column. - Data-only migrations (
migrationsbelow, withoutsqlDDL) also work on Prisma tables.
Engine-owned tables
With a TypeScript schema, clientdb owns the tables, and a schema change needs a versioned migration: migrate() refuses one without it, naming the changes.
import { createEngine, migration } from "@clientdb/server";
import { permissions } from "./permissions";
export const engine = createEngine({
permissions,
migrations: [
migration(1, {
name: "todo due date",
sql: `ALTER TABLE m_todo ADD COLUMN "dueAt" bigint`, // with dueAt: t.timestamp().nullable() in the schema
resync: ["todo"],
}),
],
});sqlruns in the migration's transaction;run: async ({ client, engine }) => { … }changes data in code instead.resynclists models whose rows the migration changed in bulk: devices get the changed rows, or one reload of the model aboveresyncInvalidateThresholdrows. Outside migrations,engine.resync(model)does the same, andengine.resync()covers every synced model.
Changing permissions
Changing a read rule changes where rows go. engine.migrate() detects the new rules and re-routes the affected models online; devices receive the rows they gained and lose the ones they lost, like any access change.
Renaming a rule's name, or changing the shape of a condition without one, creates a new group kind: engine.migrate() recomputes everyone's groups and re-routes the affected models. See Group ids and names.