expression

SQLx: embed versioned migrations

A migration is a versioned schema change. SQLx can embed ordered migration files during compilation. The fixture lists embedded versions without connecting to a server. Keep applied migration files unchanged and add new versions for subsequent changes. A build script tracks additions to the migration directory. No database connection is required by this example. Expected stdout (not executed): migration count: 2 Common mistake: Editing an already-applied migration instead of adding a new one.

Formal Syntax & Structure

rust syntaxrust
sqlx::migrate!("./migrations")

Working Example

Example Snippetrust
#[tokio::main(flavor = "current_thread")]async fn main() -> Result<(), Box<dyn std::error::Error>> {    let migrator = sqlx::migrate!("./migrations");    println!("migration count: {}", migrator.iter().count());    Ok(())}
Rust Reference