sqlfu treats schema history as an explicit sequence of reviewed SQL files. You editDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/iterate/sqlfu/llms.txt
Use this file to discover all available pages before exploring further.
definitions.sql to declare what you want the schema to look like, then run sqlfu draft to produce the migration that gets you there. The diff engine, migration history table, and live schema form a chain of four named authorities that every command respects.
tl;dr: just run
sqlfu check — it says “all good” or gives you the recommended next action.The four authorities
Every sqlfu command operates on one or more of these four sources of truth. Understanding which authority each command reads from — and which it mutates — makes the whole model predictable.| Authority | Meaning | Representation |
|---|---|---|
| Desired Schema | The schema the application wants now | definitions.sql |
| Migrations | The ordered transition program | migrations/*.sql |
| Migration History | What a specific database claims it has applied | sqlfu_migrations table |
| Live Schema | What the database actually looks like right now | Inspected directly from the database |
Authority mismatches
When two authorities disagree, sqlfu gives the disagreement a name. Each name has a distinct meaning and a recommended next action.| Name | Comparison | Likely action |
|---|---|---|
| Repo Drift | Desired Schema ≠ Migrations | sqlfu draft |
| Pending Migrations | Migrations ahead of Migration History | sqlfu migrate |
| History Drift | Applied migrations no longer match the repo | Fix the repo first, or reconcile with sqlfu baseline + sqlfu goto |
| Schema Drift | Live Schema ≠ what Migration History implies | sqlfu baseline <target> or sqlfu goto <target> |
| Sync Drift | Live Schema ≠ Desired Schema | Depends on other mismatches |
sqlfu sync calls that bypass the history chain.
Migration workflow
Edit definitions.sql
Change table definitions, add columns, create indexes. This is the only file you author directly. It represents the schema you want the application to have.
Draft a migration
definitions.sql, and writes a new migration file into migrations/. The file is named with an ISO timestamp prefix by default (e.g. 2026-04-22T10.30.45.123Z_add_published_to_posts.sql).Review the migration
Open the generated file. The diff engine makes a best effort, but renames, destructive changes, and backfills are product decisions — review the SQL before applying it.
What each command mutates
Understanding mutation boundaries makes it safe to reach for the right command under pressure.sqlfu draft
Reads Migrations, replays them, compares the result to the Desired Schema, and writes a new migration file.
Mutates: Migrations
Does not touch: Migration History, Live Schema
sqlfu migrate
Applies pending migrations to the database and records them.
Mutates: Migration History, Live Schema
Does not touch: Desired Schema, Migrations
sqlfu baseline <target>
Declares that this database should be treated as having applied migrations through <target>, without changing the actual schema.
Mutates: Migration History
Does not touch: Desired Schema, Migrations, Live Schema
sqlfu goto <target>
Makes the database look like the schema implied by migrations through <target> and records that history.
Mutates: Migration History, Live Schema
Does not touch: Desired Schema, Migrations
sqlfu sync
Updates the Live Schema directly from the Desired Schema, skipping the migration chain entirely.
Mutates: Live Schema
Does not touch: Migrations, Migration History
After sync, the database may be schema-current but history-dirty. It is useful for local development but is not a history-preserving command.
sqlfu check
Mutates nothing. Verifies all four authority relationships and recommends the least-destructive next step it can justify from the evidence.
sqlfu check recommendations
sqlfu check matches named mismatch types to specific recommendations rather than generic failure text.
- Repo Drift only — recommend
sqlfu draft - Pending Migrations only — recommend
sqlfu migrate - Schema Drift only — recommend
sqlfu baseline <target>orsqlfu goto <target>. Ifcheckcan prove the Live Schema matches a replayed migration prefix exactly, it recommends that specific target. - Pending Migrations + Sync Drift — recommend
sqlfu migrate; the Sync Drift recommendation defers to that same step - Repo Drift + Sync Drift — recommend
sqlfu draft; the repo needs a migration before the database can become migration-current honestly - History Drift only — no single automatic recommendation.
checkexplains the likely causes and offers two paths: fix the repo if old migrations were edited, or reconcile deliberately withsqlfu baseline+sqlfu goto - Multiple mismatches — prefer the most upstream mismatch first: Repo Drift → History Drift → Pending Migrations → Schema Drift → Sync Drift
Migration presets
sqlfu tracks applied migrations in a bookkeeping table. Two presets are available:| Preset | Table | Checksum tracking | Default prefix |
|---|---|---|---|
'sqlfu' (default) | sqlfu_migrations | Yes | iso |
'd1' | d1_migrations | No | four-digit |
'd1' preset makes sqlfu read and write the same d1_migrations table that Cloudflare’s alchemy and wrangler tools manage. Under this preset, sqlfu cannot detect that an already-applied migration’s content was edited after the fact — there is no checksum column. This is a deliberate tradeoff for alchemy compatibility.
sqlfu.config.ts
Migration naming and prefix options
Migration files are ordered lexicographically by filename, so the prefix must be sortable. Two formats are supported:iso(default) — ISO timestamp prefix:2026-04-22T10.30.45.123Z_create_people.sqlfour-digit— Sequential integer prefix:0000_create_people.sql,0001_add_index.sql
migrations as an object:
sqlfu.config.ts
prefix field is defaulted from the preset but can be overridden explicitly. For example, {preset: 'd1', prefix: 'iso'} is valid if you want alchemy’s table with ISO-prefixed filenames.
Failed migrations
When a migration fails, the key question is not “did a migration fail?” but “is this database still honest about what it has applied?”sqlfu_migrations records success only. A row is written only after a migration’s SQL finishes successfully. There is no “failed” row and no failure-status column. This keeps the meaning of Migration History stable.
Preflight check. Before applying anything, sqlfu migrate checks that the database’s Live Schema still matches what its recorded Migration History implies. If History Drift or Schema Drift is present, migrate refuses to proceed and points at the reconciliation steps required.
Post-failure health check. When a migration execution fails, sqlfu migrate reruns a health check against the post-failure state:
- If the migration rolled back cleanly, the error message says the database is still healthy for
migrate. Fix the migration and runsqlfu migrateagain. - If the migration left the Live Schema out of sync with Migration History, the error says reconciliation is required and shows the same recommendation-style diagnostics as
sqlfu check.
sqlfu goto and sqlfu baseline — the same tools the four-authority model already has.
Durable Objects migration pattern
Cloudflare Durable Objects make the chain visible because each object instance has its own private SQLite database. On startup, each object must reconcile its private database with the migrations bundled into the deployed Worker code.Edit definitions.sql and draft migrations
Edit
definitions.sql, run sqlfu draft, and commit the generated migrations/*.sql files as part of the Worker bundle.Generate a TypeScript migrations bundle
migrations/.generated/migrations.ts — a plain TypeScript bundle containing the migration files. Commit this alongside your migration SQL files.sqlfu_migrations rows not present in the generated bundle, applyMigrations() fails before applying newer migrations. Under the default sqlfu preset it also checks applied migration checksums, so editing an already-applied migration file is reported as history drift.
Runtime migration API
For environments where you bundle migrations at build time — primarily Cloudflare Durable Objects — sqlfu exports two functions from the main package:applyMigrations(client, params)— applies a set of migrations to the client. Accepts bothSyncClientandAsyncClientand returns a matching sync or async result. The params object includesmigrations,preset, andprefixoptions.migrationsFromBundle(bundle)— converts the generatedMigrationBundle(frommigrations/.generated/migrations.ts) into theMigration[]array thatapplyMigrationsconsumes.
migrate function exported from migrations/.generated/migrations.ts wraps both calls for you, so most projects never call these functions directly. They are exported for custom migration runners or for testing against explicit migration sets.