Jump to…
snowinitial commitqoxwzsukwmkx1mo
Matt W1//! Rebuild `df-db` whenever a migration changes.
Matt W2//!
Matt W3//! `sqlx::migrate!` embeds the contents of `migrations/` into the binary at
Matt W4//! **compile time**. Cargo cannot see that dependency by itself: nothing in
Matt W5//! `src/` mentions those files, so adding a migration does not make the crate
Matt W6//! look dirty and the old set stays embedded.
Matt W7//!
Matt W8//! That failure is silent and nasty. The binary starts, reports "migrations up
Matt W9//! to date", and then every query against a column the missing migration was
Matt W10//! supposed to add fails at runtime — which is exactly how it presented when it
Matt W11//! happened here: a `column r.search does not exist` from a build that thought
Matt W12//! it had applied migration 3.
Matt W13//!
Matt W14//! Emitting the directory as a dependency makes the whole thing ordinary again.
Matt W15
Matt W16fn main() {
Matt W17 // The directory itself, so an added or removed file is noticed…
Matt W18 println!("cargo:rerun-if-changed=../../migrations");
Matt W19
Matt W20 // …and each file, so an *edited* one is too. A directory's mtime does not
Matt W21 // change when a file inside it is modified in place.
Matt W22 if let Ok(entries) = std::fs::read_dir("../../migrations") {
Matt W23 for entry in entries.flatten() {
Matt W24 println!("cargo:rerun-if-changed={}", entry.path().display());
Matt W25 }
Matt W26 }
Matt W27}

27 lines · Rust