SSR framework for Svelte 5 + Bun with islands-based selective hydration
On this page
Database migrations
Since: 0.10.0 (not released yet): The storage option and the migrations runner ship in 0.10.0.
Mochi ships with a built-in migrations manager for your Postgres or SQLite tables. Declare your app database with the storage option and Mochi applies pending .sql migrations against it on startup. A failed migration fails the boot — startOnFail: true is the escape hatch that boots anyway and logs the error.
default — see Persistence for all features.
// file: src/index.ts
import { Mochi } from 'mochi-framework';
await Mochi.serve({
port: 3333,
routes: {/* … */},
storage: { type: 'sqlite', path: './.db/app.db' },
// or: { type: 'postgres', url: process.env.DATABASE_URL! }
});-- file: migrations/sqlite/1_create-users.sql
CREATE TABLE users (
id integer PRIMARY KEY,
email text NOT NULL UNIQUE
);Migrations live in migrations/sqlite/ or migrations/postgres/ at the project root. Both folders can coexist — only the one matching storage runs, so an app that maintains both can switch database types freely. A comment-only .sql file is recorded without executing a statement, so a migration needed on only one database can keep a no-op placeholder at the matching id in the other folder.
How migrations work
- Files are named
<id><separator><name>.sql—1_init.sql,2-add-users.sql. Ids start at 1 and are consecutive; gaps and duplicates are boot errors. - Runs are forward-only — to undo something, add a new migration.
- Each migration runs in its own transaction, so a failed file leaves nothing half-applied and re-runs cleanly after you fix it.
- Mochi keeps track of the migrations that have already run and never reapplies them.
- Startup migrations run once per boot — in development, restart the dev server after adding a new
.sqlfile.
Two tables appear in your database: mochi_migrations records your applied migrations, and mochi_migrations_internal records framework-internal ones, applied before yours. Leave both to Mochi.
Framework-internal migrations never block a boot. When an older mochi-framework starts against a database that a newer release has already migrated (a rollback, or a rolling deploy with mixed versions), it logs a warning such as Database mochi migration is at version 3 … but you are booting an older version, applies none of its own, and starts. Some features might not work correctly until every instance runs the newer release.
A -- migrate:no-transaction comment in a file runs it outside a transaction on either database, for statements that refuse to run inside one (CREATE INDEX CONCURRENTLY, VACUUM). Write such a file idempotently (IF NOT EXISTS) — a crash mid-run, or a racing second process on SQLite, can execute it twice. Explicit BEGIN, COMMIT, or ROLLBACK statements in a file without -- migrate:no-transaction are rejected as an error (SAVEPOINT and ROLLBACK TO are fine).
Workers
Mochi.worker({ storage }) applies pending migrations on start(), before polling begins.
Concurrency
Concurrent runners are safe: Postgres replicas serialize on a session-level advisory lock and SQLite re-checks inside a write transaction, so each migration applies exactly once.
CLI
bunx mochi-framework migrate applies pending migrations without booting the server, reading storage from your entry’s Mochi.serve() or Mochi.worker() call (ignoring startOnFail so a failed migration always exits non-zero). See CLI.
| Option | Description |
|---|---|
--entry <path> | Entry calling Mochi.serve({ storage }) or Mochi.worker({ storage }). Default: ./src/index.ts. |
--validate | Check file naming and ordering in both folders — no entry import, no database. |
runMigrations
Run migrations yourself — from a deploy step, a seed script, or a test — by pointing the exported runner at a folder:
import { runMigrations } from 'mochi-framework';
const applied = await runMigrations({
storage: { type: 'postgres', url: process.env.DATABASE_URL! },
dir: 'migrations/postgres',
});
console.log(applied); // [{ id: 1, filename: '1_create-users.sql' }]