SCHEMA MIGRATIONS FOR SQLITE · 0.3.0

Change the shape of SQLite. Keep a way back.

Where SQLite cannot change a table in place, migratr builds it again and copies every row across. Every migration goes in a ledger. Before anything destructive, the database is snapshotted.

$ curl -fsSL https://smugglr.dev/migratr/install | bash

plan

Every statement a run would execute, before it runs. It changes nothing.

ledger

One row per applied migration, with a checksum. Edited files are refused.

snapshot

A copy of the database before every destructive step. restore puts it back.

In order: plan, snapshot, build the new tables, copy the rows parents first, drop the old children before the old parent, rename parent first, record the ledger row. restore --yes puts the snapshot back.

THE REBUILD

3 users in. 3 users out.

3 posts in. 3 posts out.

When SQLite cannot change a table in place, migratr builds it again and copies every row across, rowids with them. Tables that reference it are rebuilt with it, parents first, so their keys still point home.

In the run on this page, dropping users.handle rebuilt users and posts together. Same rows before and after; the handles stayed behind in the snapshot.

THE LEDGER

Every migration is written down. Edit one after the fact, and migratr stops.

Each applied migration is a row in _migratr_migrations with a checksum of what was applied. Change the file later and the next up, down or status refuses until the file is put back. The change belongs in a new migration.

status, after the applied file was edited
migrationstate
create_usersapplied
create_postsapplied
link_posts_to_usersapplied
remove_handle_from_usersCHECKSUM MISMATCH

FOREIGN KEYS

Add a foreign key to a table that already has rows. The rows are the check.

Before anything changes, migratr counts the rows whose key names no parent. Post 4 names author 9, and there is no user 9, so plan and up both refuse and say how many. Delete the stray row and the key goes on, every row and index kept.

posts, against users 1, 2 and 3: 1 row names no parent
idauthor_idtitle
11hello
21again
32notes
49stray

Also typed operations: drop a foreign key, CHECK and composite UNIQUE constraints, triggers, and partial, expression, descending and collated indexes.

THE WAY BACK

A snapshot before every drop.

One command to go back.

app.db, after the drop

idname
1Ada
2Brian
3Cleo

after restore --yes

idnamehandle
1Adaada
2Brianbrian
3Cleocleo

Before a step that drops a table, column, index or trigger, or runs raw SQL, migratr copies the database into .migratr/snapshots/. restore puts the copy back; without --yes it changes nothing and says what it would discard. down reverses only what has an exact inverse, and does not restore data in dropped objects.

LOCAL FIRST

Your file, your machine. The snapshot sits right next to it.

The database migratr works on is a local SQLite file, and snapshot and restore act on that file. Since 0.3.0 it can also ship a migration to a remote database, the place, through the smugglr binary: migratr dev, up --smuggl and down --smuggl, with smugglr 0.6.3 or later. Moving rows between places is smugglr's job; migratr changes the shape.

$ curl -fsSL https://smugglr.dev/migratr/install | bash

Or take the archive for your machine from the v0.3.0 release. To embed migrations in a Rust program, see Library.

FOR AGENTS

Below this line is the record: every command and its output from runs of the published 0.3.0 binary, in the order they ran, nothing collapsed. sqlite3 blocks read the same database. The scratch directory prints as a dot. The runs set "loud": false in migratr.jsonc; by default migratr 0.3.0 also writes its settings, and each statement a run executes, to stderr.

01 install

$ curl -fsSL https://smugglr.dev/migratr/install | bash

Archives: https://github.com/rafters-studio/migratr/releases/tag/v0.3.0, with checksums.txt.

$ cat checksums.txt
d92c08090ce9199cedb626ade47a3b7504f7e8262c686dd7eaaa1054c7487f81  migratr-macos-arm64.tar.gz
00beab452e64238e6300e9c224842fecd908db60d5f12e5754ce7852ae15293c  migratr-macos-x64.tar.gz
1996761eaec30dcabeece2a439766234b702e67057d2fbe661c94b424cbdecac  migratr-linux-x64.tar.gz
d508072ed92cf979c1f4e37f106c4ba636de3541e8470b6743de302581423ed6  migratr-windows-x64.zip
$ migratr --version
migratr 0.3.0

02 new, plan, up

$ migratr new create_users id:integer:pk name:text:notnull handle:text:unique
created migrations/20261112063551_create_users.json
$ migratr new create_posts id:integer:pk author_id:integer title:text:notnull
created migrations/20261112063552_create_posts.json
$ migratr --db app.db plan
plan up; database is 0 bytes
20261112063551_create_users
  CREATE TABLE IF NOT EXISTS _migratr_migrations (version INTEGER PRIMARY KEY, name TEXT NOT NULL, checksum TEXT NOT NULL, applied_at TEXT NOT NULL, up_hash TEXT, down_hash TEXT)
  CREATE TABLE "users" ("id" INTEGER, "name" TEXT NOT NULL, "handle" TEXT UNIQUE, PRIMARY KEY ("id"))
  INSERT INTO _migratr_migrations (version, name, checksum, applied_at, up_hash, down_hash) VALUES (20261112063551, 'create_users', 'ce8a05122f27103c0715a62a74dd28b697c4913c1af185ed01dd331df4b4fc74', strftime('%Y-%m-%dT%H:%M:%fZ', 'now'), 'b7472ff311778e7b6c71fd26077194406bde246d742a2fde13396f4627c1a194', 'aaf2045087c9a7d046d144f42006f713f080bb9c52253c1fa4c942d016988fa8')
20261112063552_create_posts
  CREATE TABLE IF NOT EXISTS _migratr_migrations (version INTEGER PRIMARY KEY, name TEXT NOT NULL, checksum TEXT NOT NULL, applied_at TEXT NOT NULL, up_hash TEXT, down_hash TEXT)
  CREATE TABLE "posts" ("id" INTEGER, "author_id" INTEGER, "title" TEXT NOT NULL, PRIMARY KEY ("id"))
  INSERT INTO _migratr_migrations (version, name, checksum, applied_at, up_hash, down_hash) VALUES (20261112063552, 'create_posts', '20f47bf02d33b53ce88312dee1d30351c7f17cc6b182a0ee785524c59af5de10', strftime('%Y-%m-%dT%H:%M:%fZ', 'now'), '1bf8197c018f7734477d11aba29af12edaba1d24e8c9a881985f7af1964d0437', '0505b88b9e1083ba3caa279b4f6ab71f6c27dcff03069bf180e8cb86802b85a7')
$ migratr --db app.db up --json
{"applied":[20261112063551,20261112063552],"command":"up","status":"ok"}
$ sqlite3 app.db "select * from users; select * from posts;"
1|Ada|ada
2|Brian|brian
3|Cleo|cleo
1|1|hello
2|1|again
3|2|notes
4|9|stray

03 add_foreign_key

The file below was written by hand after migratr new link_posts_to_users.

$ cat migrations/20261112063553_link_posts_to_users.json
{
  "up": [
    {
      "op": "add_foreign_key",
      "table": "posts",
      "columns": ["author_id"],
      "references": { "table": "users", "columns": ["id"] },
      "on_delete": "CASCADE",
      "on_update": "NO ACTION"
    }
  ]
}
$ migratr --db app.db plan
error [foreign_key_violation]: table posts: found 1 rows violating foreign keys
$ echo $?
1
$ migratr --db app.db up --json
{"code":"foreign_key_violation","command":"up","message":"table posts: found 1 rows violating foreign keys","status":"error"}
$ echo $?
1

Post 4 deleted with sqlite3, then:

$ migratr --db app.db plan
plan up; database is 20480 bytes
20261112063553_link_posts_to_users
  CREATE TABLE IF NOT EXISTS _migratr_migrations (version INTEGER PRIMARY KEY, name TEXT NOT NULL, checksum TEXT NOT NULL, applied_at TEXT NOT NULL, up_hash TEXT, down_hash TEXT)
  CREATE TABLE "_migratr_rebuild" ("id" INTEGER, "author_id" INTEGER, "title" TEXT NOT NULL, PRIMARY KEY ("id"), FOREIGN KEY ("author_id") REFERENCES "users" ("id") ON DELETE CASCADE ON UPDATE NO ACTION)
  INSERT INTO "_migratr_rebuild" (rowid, "id", "author_id", "title") SELECT rowid, "id", "author_id", "title" FROM "posts"
  DROP TABLE "posts"
  ALTER TABLE "_migratr_rebuild" RENAME TO "posts"
  INSERT INTO _migratr_migrations (version, name, checksum, applied_at, up_hash, down_hash) VALUES (20261112063553, 'link_posts_to_users', '185bdae9eb924882a4a23388ce6d94008d1e1f52bd3ee459eb5c599431eac555', strftime('%Y-%m-%dT%H:%M:%fZ', 'now'), 'fa3a4cc7b9fdbe9c9d3eff4d9b2cd0cb7c2a058b244c49ad5e3540b2d00c7ca6', 'eb99d00dc0fc4ad196fcee782ff71671e0fb83d67f660d925a40751e5e80bef4')

migratr --db app.db up then applied link_posts_to_users. Its output is not shown here.

04 drop_column, rebuilt as a family

$ migratr new remove_handle_from_users
created migrations/20261112063554_remove_handle_from_users.json
$ cat migrations/20261112063554_remove_handle_from_users.json
{
  "up": [
    {
      "column": {
        "name": "handle",
        "not_null": false,
        "type": "TEXT",
        "unique": true
      },
      "op": "drop_column",
      "table": "users"
    }
  ]
}
$ migratr --db app.db plan
plan up; database is 24576 bytes
20261112063554_remove_handle_from_users DESTRUCTIVE, snapshot 20261112063554_20261112063554_up.db
  CREATE TABLE IF NOT EXISTS _migratr_migrations (version INTEGER PRIMARY KEY, name TEXT NOT NULL, checksum TEXT NOT NULL, applied_at TEXT NOT NULL, up_hash TEXT, down_hash TEXT)
  CREATE TABLE "_migratr_rebuild" ("id" INTEGER, "name" TEXT NOT NULL, PRIMARY KEY ("id"))
  CREATE TABLE "_migratr_rebuild_2" ("id" INTEGER, "author_id" INTEGER, "title" TEXT NOT NULL, PRIMARY KEY ("id"), FOREIGN KEY ("author_id") REFERENCES "_migratr_rebuild" ("id") ON DELETE CASCADE ON UPDATE NO ACTION)
  INSERT INTO "_migratr_rebuild" (rowid, "id", "name") SELECT rowid, "id", "name" FROM "users"
  INSERT INTO "_migratr_rebuild_2" (rowid, "id", "author_id", "title") SELECT rowid, "id", "author_id", "title" FROM "posts"
  DROP TABLE "posts"
  DROP TABLE "users"
  ALTER TABLE "_migratr_rebuild" RENAME TO "users"
  ALTER TABLE "_migratr_rebuild_2" RENAME TO "posts"
  INSERT INTO _migratr_migrations (version, name, checksum, applied_at, up_hash, down_hash) VALUES (20261112063554, 'remove_handle_from_users', '946d46564ece3b8264257cf1c23bf220bac7713e20fa447ef581da10c992fe71', strftime('%Y-%m-%dT%H:%M:%fZ', 'now'), '0b78a25ac9e49a78e71380b77156e20e79b13cc72041811e5dafb2a8ab17fce4', 'd2c2dacf4a8735946697342815e62c2d6a91ec5018d8f8587b800de887800bdd')
$ migratr --db app.db up
applied 1 migration(s): 20261112063554
$ sqlite3 app.db "select * from users; select * from posts;"
1|Ada
2|Brian
3|Cleo
1|1|hello
2|1|again
3|2|notes

05 ledger and checksum

$ sqlite3 app.db "select version, name, checksum from _migratr_migrations;"
20261112063551|create_users|ce8a05122f27103c0715a62a74dd28b697c4913c1af185ed01dd331df4b4fc74
20261112063552|create_posts|20f47bf02d33b53ce88312dee1d30351c7f17cc6b182a0ee785524c59af5de10
20261112063553|link_posts_to_users|185bdae9eb924882a4a23388ce6d94008d1e1f52bd3ee459eb5c599431eac555
20261112063554|remove_handle_from_users|946d46564ece3b8264257cf1c23bf220bac7713e20fa447ef581da10c992fe71
$ migratr --db app.db status
20261112063551_create_users applied 2026-11-12T06:35:52.256Z
20261112063552_create_posts applied 2026-11-12T06:35:52.256Z
20261112063553_link_posts_to_users applied 2026-11-12T06:35:53.347Z
20261112063554_remove_handle_from_users applied 2026-11-12T06:35:54.400Z
latest snapshot: ./.migratr/snapshots/20261112063554_20261112063554_up.db

"handle" changed to "nickname" in the applied file, then:

$ migratr --db app.db status
20261112063551_create_users applied 2026-11-12T06:35:52.256Z
20261112063552_create_posts applied 2026-11-12T06:35:52.256Z
20261112063553_link_posts_to_users applied 2026-11-12T06:35:53.347Z
20261112063554_remove_handle_from_users applied 2026-11-12T06:35:54.400Z CHECKSUM MISMATCH
latest snapshot: ./.migratr/snapshots/20261112063554_20261112063554_up.db
$ migratr --db app.db up --json
{"code":"checksum_mismatch","command":"up","message":"migration 20261112063554_remove_handle_from_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"}
$ echo $?
1

06 restore

The file put back as applied, then:

$ migratr --db app.db restore
error [restore_not_confirmed]: restore needs confirmation; it would discard everything written since the snapshot of 20261112063554 (ledger version 20261112063553); ledger versions removed: 20261112063554
$ echo $?
1
$ migratr --db app.db restore --yes
restored; discards everything written since the snapshot of 20261112063554 (ledger version 20261112063553); ledger versions removed: 20261112063554
$ sqlite3 app.db "select * from users;"
1|Ada|ada
2|Brian|brian
3|Cleo|cleo
$ migratr --db app.db status
20261112063551_create_users applied 2026-11-12T06:35:52.256Z
20261112063552_create_posts applied 2026-11-12T06:35:52.256Z
20261112063553_link_posts_to_users applied 2026-11-12T06:35:53.347Z
20261112063554_remove_handle_from_users pending
latest snapshot: ./.migratr/snapshots/20261112063554_20261112063554_up.db

07 down: exact inverses only

A second database, notes.db, whose newest migration is a raw_sql with no down:

$ migratr new create_notes id:integer:pk body:text
created migrations/20261112063555_create_notes.json
$ migratr new shout_notes
created migrations/20261112063556_shout_notes.json
$ migratr --db notes.db up
applied 2 migration(s): 20261112063555, 20261112063556
$ migratr --db notes.db plan down
error [irreversible]: migration 20261112063556 cannot be reversed: operation 0 is raw SQL with no down
$ echo $?
1
$ migratr --db notes.db down --json
{"code":"irreversible","command":"down","message":"migration 20261112063556 cannot be reversed: operation 0 is raw SQL with no down","status":"error"}
$ echo $?
1

08 --json and exit codes

With --json a command writes one JSON document to stdout and nothing else; logs go to stderr. It carries command and status (ok or error). A failure adds code and message and exits 2 for a usage error, 1 otherwise; up --discover and down --discover exit 3 when the run would stop. Codes are stable across patch releases.

codecause
usageBad arguments, or a missing --db.
parseA migration file or spec could not be parsed.
duplicate_versionTwo migration files share a version.
ioA file or directory could not be read or written.
checksum_mismatchAn applied migration’s file was edited.
missing_fileA ledger row has no migration file.
apply_failedA migration failed and was rolled back.
irreversibleA migration in the down range has an operation with no inverse.
unparseable_tableA table’s CREATE TABLE could not be parsed for a rebuild.
foreign_key_violationRows in a table up or plan would rebuild name no parent row.
no_such_foreign_keydrop_foreign_key named a key the table does not have.
trigger_mismatchA create_trigger’s name or table differs from its statement.
column_in_useA dropped column is used by another object.
rebuild_not_firstAn operation that needs a table rebuild is not first in its migration.
snapshot_failedA snapshot could not be written, so the step did not run.
restore_not_confirmedrestore ran without --yes.
unknown_objectnew named an object that is not in schema.json.
databaseSQLite reported an error.
smugglr_missingsmugglr 0.6.3 or later was not found; the message names the version.
not_adoptableThe place has tables but no ledger, and its shape is not what the migrations make.
autoincrementThe place has an AUTOINCREMENT table.
shape_changedThe place's fingerprint differs from the recorded one; run with --discover.
no_rewind_point--smuggl ran with no rewind point in the ledger; run migratr dev.
ship_failedA ship failed; the local database is back at the rewind point. Run it again.
smugglrsmugglr refused or failed; the message is smugglr's own.

09 limits

  • The working database is a local SQLite file. A place (a file, an https:// address or d1://) is reached only through smugglr 0.6.3 or later. Snapshot and restore act on the local file, not on the place.
  • Only the newest snapshot of a step is kept; migratr dev's rewind point and kept snapshots are never pruned. A snapshot that cannot be written stops the step (snapshot_failed).
  • down reverses structure only; data in dropped objects is not restored.
  • A rebuild is refused before anything runs when the table's CREATE TABLE does not parse, when another object uses the dropped column, or when the rebuild is not the first operation in its migration.
  • 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.

10 pages