expression
Diesel: test a migration reversal
A down migration reverses the schema change where possible. Test it on disposable data before relying on it; dropping a table destroys its data. This fixture verifies the migration tracking by applying, reverting and applying again. Uses a disposable in-memory SQLite database. Expected stdout (not executed): reapplied: 1 Common mistake: Treating a destructive down migration as a safe production recovery plan.
Formal Syntax & Structure
rust syntaxrust
conn.revert_last_migration(MIGRATIONS)?Working Example
Example Snippetrust
use diesel::prelude::*;use diesel::sqlite::SqliteConnection;
use diesel_migrations::{embed_migrations, EmbeddedMigrations, MigrationHarness};const MIGRATIONS: EmbeddedMigrations = embed_migrations!("migrations");
fn main() -> Result<(), Box<dyn std::error::Error>> { let mut conn = SqliteConnection::establish(":memory:")?; conn.run_pending_migrations(MIGRATIONS).map_err(|e| e.to_string())?; conn.revert_last_migration(MIGRATIONS).map_err(|e| e.to_string())?; let n = conn.run_pending_migrations(MIGRATIONS).map_err(|e| e.to_string())?.len(); println!("reapplied: {n}"); Ok(())}