migratr 0.3.0
The CLI and the library run the same code. The migratr crate embeds a migrations directory in your program and applies it at startup, with the same ledger, rebuilds and snapshots.
cargo add migratrembed!
migratr::embed!("migrations") reads a migrations directory at compile time, relative to the crate's CARGO_MANIFEST_DIR, and expands to a Migrator. It parses every file with the library's own parser, so a malformed file or a repeated version stops the build instead of failing at startup. It takes one string literal.
Migrator
| Method | What it does |
|---|---|
up(&self, exec) |
Applies every pending migration. Returns an UpReport with applied. |
down(&self, exec, steps) |
Reverses the newest steps applied migrations. Returns a DownReport with reverted; it prints as the CLI's sentence. |
status(&self, exec) |
Every embedded migration with whether it is applied. Refuses, as up does, on a missing file or a checksum mismatch. |
plan(&self, exec) |
The migrations up would apply, in order. |
Each method takes an &mut impl Executor and returns Result<_, MigrateError>. The error variants are the CLI's error codes.
Example
This program was built against migratr = "0.2.0" and rusqlite = { version = "0.40", features = ["bundled"] } with the create_users migration from Migrations in migrations/.
use migratr::{Migrator, RusqliteExecutor};
use rusqlite::Connection;
static MIGRATIONS: std::sync::LazyLock<Migrator> =
std::sync::LazyLock::new(|| migratr::embed!("migrations"));
fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut exec = RusqliteExecutor::new(Connection::open("app.db")?);
let report = MIGRATIONS.up(&mut exec)?;
println!("applied: {:?}", report.applied);
for m in MIGRATIONS.status(&mut exec)? {
println!("{} {} applied={}", m.version, m.name, m.applied);
}
let down = MIGRATIONS.down(&mut exec, 1)?;
println!("{down}");
Ok(())
}$ cargo run
applied: [20261007235008]
20261007235008 create_users applied=true
reverted 1 migration(s): 20261007235008. Data in dropped objects is not restored.Captured from cargo run in a scratch project against the published 0.2.0 crates. Against 0.3 the program is the same except for the dependency line: use migratr = "0.3" and rusqlite 0.41, and Rust 1.89 or later.
The executor
Executor is the boundary between migratr and a SQLite connection. An implementation reads the schema (read_schema), reads the ledger (read_ledger), runs a list of statements in one transaction (run_atomic, which keeps an option to suspend foreign keys that up no longer uses), and writes a consistent copy of the database for a snapshot (snapshot). It returns Ok(None) from snapshot when the database is not durable and needs none. Two methods, missing_parents and foreign_key_violations, are optional: they default to None, so an existing executor still compiles. RusqliteExecutor implements both; another executor can through migratr::missing_parents_through and migratr::foreign_key_violations_through over a SqlReader. Without them a rebuild gets no count, and a row a foreign key refuses fails inside the atomic run as apply_failed, with nothing changed.
The rusqlite feature is on by default and provides RusqliteExecutor, built from a rusqlite::Connection, plus restore and latest_snapshot. To supply your own executor, turn the default off:
migratr = { version = "0.3", default-features = false }That keeps the parser, up, down, Migrator and the Executor trait, and drops rusqlite, so migratr sits beside another SQLite library such as sqlx without a second libsqlite3. migratr's CI builds this configuration next to sqlx on every change.
Upgrading from 0.2
0.3 is a minor release with three changes that can break a build.
LedgerRow gains the public fields up_hash and down_hash and is not #[non_exhaustive], so code that builds a LedgerRow literal fails to compile against 0.3. Every crate declares rust-version = "1.89", and an older toolchain refuses to build. RusqliteExecutor::new takes a rusqlite::Connection from rusqlite 0.41 (libsqlite3-sys 0.39), the release that bundles the SQLite fix for RUSTSEC-2026-0459. libsqlite3-sys links sqlite3, so only one version of it can be in a build: a dependent that uses rusqlite anywhere in its graph must move to 0.41 with migratr.
New exports: Note (RewindPoint at version 0, Fingerprint at version 1); record_note, which writes or replaces a note in one atomic run and creates the ledger when it is absent; snapshot_for_dev(db_path, source, kind) with DevSnapshot; has_keyword, which says whether a statement holds a keyword as a bare word rather than inside a quoted identifier; and RusqliteExecutor::shape_differences, the structural comparison dev uses. read_ledger_through reads the hashes when the ledger has the columns and reads an older ledger without them. The ledger's schema change is described under the ledger.
What it does not do
Migrator applies and reverses migrations; it does not write schema.json. The CLI does that after each up and down; in a library, migratr::write_schema writes it on request. dev and --smuggl belong to the CLI; the library works on the connection it is given.