The `embed_migrations!` macro doesn't pick up new migration files due to cargo build cache
Author: fbecartCreated Apr 5, 2022Updated Jul 13, 2026
Labelsbug
Setup
Versions
- diesel: 1.4.8
- diesel_migrations: 1.4.0
- migrations_macros: 1.4.2
Problem Description
I use the embed_migrations! to generate a binary to run Diesel migrations. Cargo will sometimes reuse the previously built binary instead of building a new one, because it didn't realize its cache became invalid. This happens when I'm only adding new files to the migrations directory.
What are you trying to accomplish?
Build a binary to run the Diesel migrations.
What is the expected output?
The binary produced by cargo build should run all of the migration files, including the most recent ones.
What is the actual output?
Cargo didn't rebuild the crate, and the binary is now outdated. As a result, if I run the binary, recent migrations will be missing.
Are you seeing any additional errors?
No
Steps to reproduce
/src/bin/run-migrations.rs:
#[macro_use]
extern crate diesel_migrations;
use common_infra::config;
use diesel::{pg::PgConnection, prelude::*};
// path relative to Cargo.toml
embed_migrations!("./migrations");
fn main() -> Result<(), Box<dyn std::error::Error>> {
let connspec = config::get_plain_url("Database")?;
let conn = PgConnection::establish(&connspec)?;
info!("Applying migrations");
embedded_migrations::run_with_output(&conn, &mut std::io::stdout())?;
Ok(())
}- Run
cargo build - Add a new migration
- Run
cargo buildagain, and observe that cargo skipped the build.
The resulting binary misses the additional SQL migration.
Checklist
- I have already looked over the issue tracker and the discussion forum for similar possible closed issues.
- This issue can be reproduced on Rust's stable channel. (Your issue will be closed if this is not the case)
- This issue can be reproduced without requiring a third party crate
Workaround
It is possible to invalidate the build cache with a /build.rs file:
use std::fs::create_dir_all;
fn main() -> Result<(), Box<dyn std::error::Error>> {
println!("cargo:rerun-if-changed=migrations");
Ok(())
}
Source: diesel-rs/diesel