Skip to main content

embed_migrations

Macro embed_migrations 

Source
embed_migrations!() { /* proc-macro */ }
Expand description

Bake every *.json migration file in a directory into the binary at compile time. Returns a &'static [(&'static str, &'static str)] of (name, json_content) pairs, lex-sorted by file stem.

Pair with rustango::migrate::migrate_embedded at runtime — same behaviour as migrate(pool, dir) but with no filesystem access. The path is interpreted relative to the user’s CARGO_MANIFEST_DIR (i.e. the crate that invokes the macro). Default is "./migrations" if no argument is supplied.

const EMBEDDED: &[(&str, &str)] = rustango::embed_migrations!();
// or:
const EMBEDDED: &[(&str, &str)] = rustango::embed_migrations!("./migrations");

rustango::migrate::migrate_embedded(&pool, EMBEDDED).await?;

Compile-time guarantees (rustango v0.4+, slice 5): every JSON file’s name field must equal its file stem, every prev reference must point to another migration in the same directory, and the JSON must parse. A broken chain — orphan prev, missing predecessor, malformed file — fails at macro-expansion time with a clear compile_error!. No other Django-shape Rust framework validates migration chains at compile time: Cot’s migrations are imperative Rust code (no static chain), Loco’s are SeaORM up/down (same), Rwf’s are raw SQL (no chain at all).

Each migration is included via include_str! so cargo’s rebuild detection picks up file content changes. Caveat: cargo doesn’t watch directory listings, so adding or removing a migration file inside the dir won’t auto-trigger a rebuild — run cargo clean (or just bump any other source file) when you add new migrations during embedded development.