migratr 0.3.0
The file
A migration is a JSON file named <YYYYMMDDHHMMSS>_<snake_name>.json: a UTC timestamp, an underscore, a lowercase name of letters, digits and underscores. The timestamp is the version. Two files with the same version are refused with duplicate_version.
$ migratr --db a.db status --json
{"code":"duplicate_version","command":"status","message":"duplicate migration version 20261007235008: 20261007235008_create_users.json, 20261007235008_dup.json","status":"error"}The body is one object with an up array of operations. Each operation names itself in an op field. Unknown fields are rejected, and a parse error names the JSON path of the bad field. This is the file migratr new wrote for create_users id:integer:pk email:text:notnull:unique:
{
"up": [
{
"columns": [
{
"name": "id",
"not_null": false,
"primary_key": 1,
"type": "INTEGER",
"unique": false
},
{
"name": "email",
"not_null": true,
"type": "TEXT",
"unique": true
}
],
"op": "create_table",
"table": "users",
"without_rowid": false
}
]
}$ migratr new add_name_to_users name:text
created migrations/20261007235009_add_name_to_users.jsonOperations
op |
Fields | Exact inverse |
|---|---|---|
create_table |
table, columns, without_rowid, checks, unique (optional) |
DROP TABLE |
drop_table |
table, definition (a table with its sql) |
Recreated from definition.sql |
rename_table |
from, to |
Renamed back |
add_column |
table, column |
drop_column |
drop_column |
table, column |
add_column |
rename_column |
table, from, to |
Renamed back |
create_index |
name, table, columns, unique, where (optional) |
drop_index |
drop_index |
definition (an index: name, table, columns, unique, where) |
CREATE INDEX, partial clause included |
add_foreign_key |
table, columns, references (table, columns), on_delete, on_update |
drop_foreign_key |
drop_foreign_key |
table, definition (the key being dropped) |
add_foreign_key |
create_trigger |
name, table, sql |
drop_trigger |
drop_trigger |
name, table, sql |
create_trigger |
raw_sql |
up, down (optional) |
Only when down is given |
create_table writes checks and then unique after the PRIMARY KEY item, in the order given. A check is an expression, or {"name", "expr"}, written as CONSTRAINT "name" CHECK (expr); a unique entry is a list of columns. Both are omitted from the file when empty, so files written before 0.2.0 keep their checksums. Order is part of the checksum.
Each entry in an index's columns is a bare column name or an object with exactly one of name or expr, plus optional desc and collate. A key that is only a name has the same checksum whether it is written "a", {"name": "a"} or {"name": "a", "desc": false}. where makes the index partial; reversing a create_index, or a drop_index of a partial index, keeps the clause.
A column carries name, type (verbatim), and optionally not_null, primary_key (1-based position), unique, default, collation, check, references (table, column, on_update, on_delete) and generated (expr, stored).
drop_table, drop_column and drop_index carry the full definition of what they drop, so that down can recreate the structure. migratr new copies that definition from schema.json, which is why drop_<name> for an object schema.json does not describe fails with unknown_object.
Where SQLite cannot do an operation in place, migratr rebuilds the table: indexes, triggers and dependent views are recreated, and rowids and sqlite_sequence carry over. A rebuild leaves foreign keys on and creates no TEMP objects, so the same rebuild runs on a local file and on a D1 database. A table that other tables reference is rebuilt together with them: new tables are created, rows are copied parents first, the old children are dropped before the old parent, and the new tables are renamed parent first. A rebuild is refused before anything runs when the table's CREATE TABLE does not parse (unparseable_table), when another object uses the dropped column (column_in_use), or when the rebuild is not the first operation in its migration (rebuild_not_first).
Foreign keys
add_foreign_key rebuilds its table with a table-level FOREIGN KEY item and keeps every row and index. A key that references its own table works the same way. The rows already in the table are the check: before any change, up counts the rows whose key columns are all non-NULL and name no parent row, and refuses with foreign_key_violation, naming the table and the count. plan makes the same count and refuses with the same error. A rebuild of any other kind counts the same way, over the tables it rebuilds only; violations elsewhere in the database no longer refuse it.
$ migratr --db app.db plan
error [foreign_key_violation]: table posts: found 1 rows violating foreign keys
[exit 1]drop_foreign_key removes either a table-level FOREIGN KEY item or the one column's inline REFERENCES clause. When the table has neither, it is refused with no_such_foreign_key. Each of the two is the other's inverse, so down reverses both.
Triggers
create_trigger runs sql verbatim and hashes it as written, whitespace included. If name or table differs from what the statement creates, the migration is refused before anything runs:
$ migratr --db app.db up --json
{"code":"trigger_mismatch","command":"up","message":"migration 20270101000004: operation 0 is create_trigger wrong on table posts, but its statement creates trigger right_name on table posts","status":"error"}
[exit 1]drop_trigger carries the same three fields and is the inverse of create_trigger.
Operations in a file run in order, and a migration with its ledger row is one atomic run. A failure rolls the run back (apply_failed).
The ledger
Applied migrations are recorded in the table _migratr_migrations, created on the first up:
CREATE TABLE IF NOT EXISTS _migratr_migrations (version INTEGER PRIMARY KEY, name TEXT NOT NULL, checksum TEXT NOT NULL, applied_at TEXT NOT NULL)Since 0.3.0 the table also carries up_hash and down_hash, both TEXT. The next up, down, record_applied or record_note on a database whose ledger lacks them runs ALTER TABLE _migratr_migrations ADD COLUMN up_hash TEXT and ... ADD COLUMN down_hash TEXT inside the same atomic run as the first migration it applies or reverts, and keeps every existing row. Rows written before 0.3.0 keep NULL hashes. The migration file format is unchanged and every existing file keeps its checksum.
up rehearses the pending migrations on an in-memory copy of the database's schema, the way plan works out statements. For each migration it runs the up, then the down that reverts it, and stores the SHA-256 of each statement list, joined with newlines, in up_hash and down_hash. The ledger's own statements are left out of both. A hash is NULL when it cannot be known: for an irreversible down, for every migration from one whose up fails on the copy onward, for a row record_applied writes, and in builds without the rusqlite feature. The hashes are what lets smugglr exec check that the statements sent to a place are the ones recorded for that version.
Ledger versions 0 and 1 are reserved for notes: version 0 holds the rewind_point note and version 1 the fingerprint note, both written by migratr dev. status lists them and the ledger readers otherwise leave them out, so a migration numbered 0 or 1 would not be seen. Versions are timestamps, so no real migration takes those numbers.
The checksum is the SHA-256 of the canonical JSON of the version, the name and the up operations. Edit an applied file and the next up, down or status sees a different checksum:
$ 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$ 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"}The fix is in the message: restore the file, then write a new migration. A ledger row whose file is gone is refused the same way, with missing_file.
Snapshots
Before a destructive step in up or down, migratr copies the database into .migratr/snapshots/ next to it. A step is destructive when it contains drop_table, drop_column, drop_index, drop_trigger or raw_sql. plan marks those steps and names the snapshot:
$ 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 = 20261007235009Snapshot files are named <timestamp>_<version>_<direction>.db. Only the newest snapshot is kept, with one exception: migratr dev writes <time>_<version>_dev.db and <time>_<version>_kept.db snapshots (the rewind point and the previous local database), and pruning never removes them. They count toward the latest snapshot, so after dev the latest snapshot, and the default for restore, is the rewind point. A snapshot that cannot be written stops the step (snapshot_failed). restore replaces the database with the newest snapshot, or the one named by --snapshot, and changes nothing without --yes. Snapshot and restore act on the local file.
The rule for down
down follows the Rails rule: it reverses only what has an exact inverse. Before anything runs, every operation in the requested range is checked, last operation first. If one has no inverse, the whole range is refused with irreversible and nothing changes.
A raw_sql operation with no down has no inverse:
$ 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"}Give the raw_sql a down and it reverses. A file with a rename, an index and a raw_sql with a down produced this plan:
$ migratr --db a.db plan down
plan down; database is 20480 bytes
20270101000000_misc DESTRUCTIVE, snapshot 20261007235139_20270101000000_down.db
SELECT 1
DROP INDEX "people_mail"
ALTER TABLE "people" RENAME COLUMN "mail" TO "email"
ALTER TABLE "people" RENAME TO "users"
DELETE FROM _migratr_migrations WHERE version = 20270101000000The statements run in reverse order of the file. down reverses structure only: data in dropped objects is not restored, and the command says so. To get data back, use restore.