migratr 0.3.0

Every command. Every flag. Every exit code. Output below is pasted from runs of the released migratr binary; the runs used a scratch directory and a throwaway database.

migratr [--db PATH] [--dir PATH] [--config PATH] [--json] <new|dev|up|down|status|plan|restore> ...

Before any command runs, migratr reads migratr.jsonc. By default every command also writes what it is doing to stderr; see Loud.

Global flags

Flag Default Description
--db <PATH> none The SQLite database file. Required by every command except new. Without it the command fails with usage.
--dir <PATH> migrations The migrations directory.
--config <PATH> nearest migratr.jsonc The settings file to read instead of the nearest migratr.jsonc in the working directory or a parent.
--json off Write one JSON document describing the outcome to stdout.
-h, --help Print help.
-V, --version Print the version.

The flags are global: they may come before or after the subcommand.

Commands

Command What it does
new <NAME> [SPECS...] Scaffold a migration file.
dev [--discover] Copy the place down and record a rewind point.
up [--to V] [--smuggl [--discover]] Apply pending migrations, locally or through the place.
down [--steps N] [--smuggl [--discover]] Reverse the newest N applied migrations (default 1).
status List each migration as applied or pending.
plan [up [--to V] | down [--steps N]] Print the statements a run would execute. Changes nothing.
restore [--snapshot PATH] --yes Replace the database with a snapshot.

After every successful up and down, migratr writes schema.json in the migrations directory.

new

migratr new <NAME> [SPECS...]

Writes <YYYYMMDDHHMMSS>_<NAME>.json into --dir, creating the directory if needed. The name picks the operation, Rails style. Column specs are name[:type][:modifier...]; the modifiers are pk, notnull, unique and default=<expr>.

Name Operation Specs
create_<table> create_table Required: at least one column.
add_<column>_to_<table> add_column The column.
drop_<table, index or trigger> drop_table, drop_index or drop_trigger None. The definition is copied from schema.json.
remove_<column>_from_<table> drop_column None. The definition is copied from schema.json.
anything else an empty up None.
$ migratr new create_users id:integer:pk email:text:notnull:unique
created migrations/20261007235008_create_users.json
[exit 0]
$ migratr new create_posts id:integer:pk title:text:notnull --json
{"command":"new","path":"migrations/20261009000001_create_posts.json","status":"ok"}
[exit 0]

A drop of something schema.json does not describe fails with unknown_object. schema.json is written by a successful up or down, so run one first.

$ migratr new drop_ghosts --json
{"code":"unknown_object","command":"new","message":"table or index ghosts is not in schema.json","status":"error"}
[exit 1]

up

migratr --db PATH up [--to V]
Flag Default Description
--to <V> all pending Stop after this version.
--smuggl off Ship the migrations to the place through the rewind point. See dev and the place.
--discover off With --smuggl: report what the run would do and change nothing.

Applies each pending migration in version order. A migration and its ledger row go in one atomic run; a failure rolls it back. up refuses to run when an applied file was edited or a ledger row has no file. up also records, per migration, the SHA-256 of the statements it ran up and of the statements that revert it; see the ledger.

$ migratr --db app.db up
applied 2 migration(s): 20261007235008, 20261007235009
[exit 0]
$ migratr --db app.db up --json
{"applied":[20261007235010],"command":"up","status":"ok"}
[exit 0]

down

migratr --db PATH down [--steps N]
Flag Default Description
--steps <N> 1 Reverse the newest N applied migrations, newest first.
--smuggl off Ship the reverts to the place through the rewind point.
--discover off With --smuggl: report what the run would do and change nothing.

down reverses structure only. Data in dropped objects is not restored. If any operation in the range has no exact inverse, the whole range is refused before anything runs; see the rule for down.

$ migratr --db app.db down --steps 2
reverted 2 migration(s): 20261007235010, 20261007235009. Data in dropped objects is not restored. Latest snapshot: ./.migratr/snapshots/20261007235010_20261007235009_down.db
[exit 0]
$ migratr --db app.db down --json
{"command":"down","data_restored":false,"reverted":[20261007235008],"snapshot":"./.migratr/snapshots/20261007235010_20261007235008_down.db","status":"ok"}
[exit 0]

status

migratr --db PATH status

Lists each migration file as applied (with the time) or pending. An applied migration whose file was edited is marked CHECKSUM MISMATCH. A ledger row with no file is reported as missing. The ledger's notes follow, each as its kind and its JSON (rewind_point {...}, fingerprint {...}); a note is never reported as a missing file. The last line names the latest snapshot. After dev, the latest snapshot is the rewind point, and it is what restore picks by default.

$ migratr --db app.db status
20261007235008_create_users pending
20261007235009_add_name_to_users pending
latest snapshot: none
[exit 0]
$ migratr --db app.db status
20261007235008_create_users applied 2026-10-07T23:50:09.749Z
20261007235009_add_name_to_users applied 2026-10-07T23:50:17.955Z CHECKSUM MISMATCH
20261007235010_remove_name_from_users applied 2026-10-07T23:50:17.956Z
latest snapshot: ./.migratr/snapshots/20261007235017_20261007235010_up.db
[exit 0]
$ migratr --db app.db status --json
{"command":"status","latest_snapshot":null,"migrations":[{"applied_at":"2026-10-07T23:50:09.749Z","checksum_mismatch":false,"name":"create_users","state":"applied","version":20261007235008},{"applied_at":"2026-10-07T23:50:09.749Z","checksum_mismatch":false,"name":"add_name_to_users","state":"applied","version":20261007235009}],"missing":[],"status":"ok"}
[exit 0]

plan

migratr --db PATH plan [--to V]
migratr --db PATH plan up [--to V]
migratr --db PATH plan down [--steps N]

plan with no direction plans up. It prints the statements a run would execute, marks destructive steps, names the snapshot each would take, and reports the database size. It changes nothing: dry-run and run print the same statements.

$ migratr --db app.db plan down
plan down; database is 16384 bytes
20261007235009_add_name_to_users DESTRUCTIVE, snapshot 20261007235009_20261007235009_down.db
  ALTER TABLE "users" DROP COLUMN "name"
  DELETE FROM _migratr_migrations WHERE version = 20261007235009
[exit 0]
$ migratr --db app.db plan --json
{"command":"plan","database_bytes":16384,"direction":"up","status":"ok","steps":[]}
[exit 0]

restore

migratr --db PATH restore [--snapshot PATH] --yes
Flag Default Description
--snapshot <PATH> the newest The snapshot to restore.
--yes off Confirm the restore. Without it nothing changes.

Restore acts on the local database file. Without --yes it fails with restore_not_confirmed and says what it would discard.

$ migratr --db app.db restore
error [restore_not_confirmed]: restore needs confirmation; it would discard everything written since the snapshot of 20261007235010 (ledger version 20261007235008); ledger versions removed: none
[exit 1]
$ migratr --db app.db restore --yes
restored; discards everything written since the snapshot of 20261007235010 (ledger version 20261007235008); ledger versions removed: none
[exit 0]
$ migratr --db app.db restore --yes --json
{"command":"restore","removed":[],"restored":true,"snapshot_time":"20261007235010","snapshot_version":20261007235008,"status":"ok"}
[exit 0]

dev and the place

The place is a remote database that holds the real schema: a file path, an https:// address or d1://<name>, set as smuggl in migratr.jsonc. It is not a new kind of target for --db; the local database stays the working copy, and the place is reached only through smugglr. dev, up --smuggl and down --smuggl need smugglr 0.6.3 or later, the first release with exec, found through the SMUGGLR environment variable or else smugglr on the PATH. Without it they refuse with smugglr_missing and name the version to install.

migratr --db PATH dev [--discover]
migratr --db PATH up --smuggl [--to V] [--discover]
migratr --db PATH down --smuggl [--steps N] [--discover]

dev. Reads the place with smugglr exec <place> --read and builds a new database in .migratr/dev beside the local one: the place's tables, then its rows through smugglr sync (smugglr's ignore list applies, so an ignored table arrives with its shape and no rows), then the ledger's migration rows, then the indexes, triggers and views. Triggers come last, so none fires on a copied row. It snapshots the new database as <time>_<version>_dev.db and checks with smugglr sync --dryrun that the snapshot and the new database move nothing. The new database's ledger then records a rewind_point note (the snapshot and its SHA-256) beside a fingerprint note (the place's structural fingerprint and the migration versions the place held). Only then is the old local database kept as <time>_<version>_kept.db and the new one renamed into place. A failed dev leaves the local database as it was. Pruning never removes kept or dev snapshots.

The first dev against a place that has tables but no migratr ledger adopts it only when its shape matches what the migrations make, compared by structure: columns, foreign keys, indexes, CHECK and UNIQUE items, and trigger and view text, normalized for quoting, case and whitespace. On a match it records every migration. Otherwise it stops with not_adoptable and names each difference by table. A later dev compares the place's fingerprint with the recorded one and stops with shape_changed if they differ; if they match, it carries only the migration rows the place held, so a migration applied locally since then reads as pending again. A place with an AUTOINCREMENT table is refused with autoincrement, naming each table, because migratr allows no auto columns. A full-text table comes down with its rows. Every stop happens before anything changes.

up --smuggl and down --smuggl. Before anything changes they check three things: the rewind point's file still has the SHA-256 dev recorded, the ledger holds a rewind point (otherwise no_rewind_point, which says to run migratr dev), and the place's fingerprint is the recorded one (otherwise shape_changed). They then snapshot the working database, put the rewind point back at the local path, and sync it with the place, so the place's own inserts, updates and deletes since dev arrive while rows made only in the working database stay home. Each migration is applied or reverted locally, then its statements, as migratr plan gives them, go to smugglr exec --up|--down --version <v> --expect <fingerprint>. smugglr runs them on the place only when their hash is the one recorded for that version and the place has the expected fingerprint. The place must end with the local database's fingerprint.

On success with autoclean on, the rewind-point snapshot, its ledger row and the working database's snapshot are removed, and each is named in the output. On any failure the local database goes back to the rewind point with both notes, nothing is removed, and the command exits 1 with ship_failed. Run the same command again: a retry runs locally the migrations the failed run already took to the place, sends only the rest, and expects the fingerprint after the ones already taken. down --smuggl stops when the place holds migrations the rewind point does not, which can happen with autoclean off, because reverting from there would undo the wrong migration on the place.

--discover. dev --discover prints smugglr sync --discover for the local database and the place. up --smuggl --discover and down --smuggl --discover (it requires --smuggl) print smugglr's --discover --json report for the rewind point and the place, the migrations the run would send with their statements, and the tables, columns and row counts each drop would destroy on the place. They change nothing, and exit 0 when the run could go ahead or 3 when it would stop. A smugglr report that is not JSON is an error, not a run with unknown counts.

Limits: the place must be reachable by smugglr; snapshot and restore act on the local file, not the place; a place's virtual tables are ignored by the sync, and their rows arrive through their shadow tables.

migratr.jsonc

Before any command runs, migratr reads the nearest migratr.jsonc in the working directory or a parent, or the file --config names. The file is JSON that may contain // and /* */ comments.

Key Default Meaning
smuggl unset The place: a file path, an https:// address or d1://<name>.
loud true Write what the command is doing to stderr. See Loud.
safe true Read and checked; no command acts on it yet.
autoclean true Whether a successful ship removes its rewind point and working snapshot.

A key that is not one of these four, or a value of the wrong type, is a usage error: exit 2, nothing changes. Because the nearest file above the working directory is read on every command, a malformed one fails every command with exit 2.

Loud

With loud on, which is the default, every command writes to stderr the settings file used (or that none was found), each key with its value and whether the file or the default set it, and, for up and down, each migration as it starts and each statement it runs. Each run ends with the note loud can be turned off in migratr.jsonc: "loud": false. Stdout, including --json documents, is unchanged. A script that treats any stderr output as failure should set "loud": false. The output blocks on this page show stdout.

JSON output

With --json, a command writes one JSON document to stdout and nothing else; logs go to stderr. The document carries command and status (ok or error). A success adds the outcome's fields. A failure adds code and message and the process exits non-zero.

Command Fields on success
new path
up applied (versions, in order)
down reverted (versions, newest first), data_restored, snapshot
status migrations (each with version, name, state, applied_at, checksum_mismatch), missing, notes (each {kind, value}), latest_snapshot
plan direction, database_bytes, steps
restore restored, removed, snapshot_time, snapshot_version

Exit codes and error codes

Exit 0 on success. A usage error exits 2. A --discover that reports the run would stop exits 3. Every other failure exits 1. Error codes are stable across patch releases.

Code Exit Cause
usage 2 Bad arguments, or a missing --db.
parse 1 A migration file or spec could not be parsed.
duplicate_version 1 Two migration files share a version.
io 1 A file or directory could not be read or written.
checksum_mismatch 1 An applied migration's file was edited.
missing_file 1 A ledger row has no migration file.
apply_failed 1 A migration failed and was rolled back.
irreversible 1 A migration in the down range has an operation with no inverse.
unparseable_table 1 A table's CREATE TABLE could not be parsed for a rebuild.
foreign_key_violation 1 Rows in a table up or plan would rebuild name no parent row.
no_such_foreign_key 1 drop_foreign_key named a key the table does not have.
trigger_mismatch 1 A create_trigger's name or table differs from its statement.
column_in_use 1 A dropped column is used by another object.
rebuild_not_first 1 An operation that needs a table rebuild is not first in its migration.
snapshot_failed 1 A snapshot could not be written, so the step did not run.
restore_not_confirmed 1 restore ran without --yes.
unknown_object 1 new named an object that is not in schema.json.
database 1 SQLite reported an error.
smugglr_missing 1 smugglr 0.6.3 or later was not found; the message names the version.
not_adoptable 1 The place has tables but no ledger, and its shape is not what the migrations make.
autoincrement 1 The place has an AUTOINCREMENT table.
shape_changed 1 The place's fingerprint differs from the recorded one; run with --discover.
no_rewind_point 1 --smuggl ran with no rewind point in the ledger; run migratr dev.
ship_failed 1 A ship failed; the local database is back at the rewind point. Run it again.
smugglr 1 smugglr refused or failed; the message is smugglr's own.

In text mode an error prints as error [<code>]: <message> on stderr.

$ migratr up
error [usage]: --db PATH is required
[exit 2]
$ migratr up --json
{"code":"usage","command":"up","message":"--db PATH is required","status":"error"}
[exit 2]
$ migratr --db app.db up --json
{"code":"checksum_mismatch","command":"up","message":"migration 20261007235009_add_name_to_users was edited after it was applied. Applied migrations are not edited: restore the file to what was applied, then write a new migration for the change","status":"error"}
[exit 1]
$ migratr --db app.db down --json
{"code":"irreversible","command":"down","message":"migration 20261008000000 cannot be reversed: operation 0 is raw SQL with no down","status":"error"}
[exit 1]
$ migratr --db app.db status --json
{"code":"parse","command":"status","message":"20261009000000_bad.json: at up[0]: unknown variant `nope`, expected one of `create_table`, `drop_table`, `rename_table`, `add_column`, `drop_column`, `rename_column`, `create_index`, `drop_index`, `add_foreign_key`, `drop_foreign_key`, `create_trigger`, `drop_trigger`, `raw_sql`","status":"error"}
[exit 1]
$ migratr --db nodir.db --dir nonexistent status --json
{"code":"io","command":"status","message":"nonexistent: No such file or directory (os error 2)","status":"error"}
[exit 1]

Errors raised by the argument parser itself, before migratr runs (an unknown subcommand, a flag missing its value), print the parser's message as plain text, exit 2, and are not JSON documents even with --json.

$ migratr bogus
error: unrecognized subcommand 'bogus'

Usage: migratr [OPTIONS] <COMMAND>

For more information, try '--help'.
[exit 2]

The [exit N] lines are added to the blocks above to show the process exit status; the binary does not print them. All blocks were captured from the 0.1.1 binary, except the parse block listing operations, captured from 0.2.0. The 0.3.0 commands (dev, --smuggl, --discover) have no output blocks here yet; their behaviour is described above from the release notes.