ORM-13: Implement Migrator::rollback(steps) #13

Open
opened 2026-10-13 12:49:39 +01:00 by NoctisWolfshadow · 0 comments
Owner

Depends on: #11

Context

Down migrations are optional, so rollback has to fail early and cleanly when one is missing instead of stopping halfway.

Tasks

  • Pre-check that the last steps applied migrations exist in the list and all have down; otherwise return an error before changing anything.
  • Run each down in descending order, each in its own transaction, deleting its tracking row. Honour rebuilds_tables through the shared function from #12.
  • Return the versions rolled back.

Example

let migrator = Migrator::new(migrations());
migrator.run()?;                          // applied [1, 2]

let rolled_back = migrator.rollback(1)?;  // runs migration 2's `down`
assert_eq!(rolled_back, [2]);

migrator.run()?;                          // re-applies 2
#[test]
fn rollback_without_down_changes_nothing() {
    let list = MigrationList::new(vec![
        Migration::new(1, "a", "CREATE TABLE a (id INTEGER);").down("DROP TABLE a;"),
        Migration::new(2, "b", "CREATE TABLE b (id INTEGER);"), // no down
    ])
    .unwrap();
    let migrator = Migrator::new(list);
    migrator.run().unwrap();

    assert!(migrator.rollback(2).is_err());
    // nothing was rolled back, not even migration 2's neighbour
    assert!(migrator.status().unwrap().iter().all(|s| s.state == MigrationState::Applied));
}

Acceptance criteria

  • Rolling back 1 step and N steps works.
  • A missing down returns an error with the database unchanged.
  • run() after a rollback re-applies the rolled-back migrations.
**Depends on:** #11 ## Context Down migrations are optional, so rollback has to fail early and cleanly when one is missing instead of stopping halfway. ## Tasks - [ ] Pre-check that the last `steps` applied migrations exist in the list and all have `down`; otherwise return an error before changing anything. - [ ] Run each `down` in descending order, each in its own transaction, deleting its tracking row. Honour `rebuilds_tables` through the shared function from #12. - [ ] Return the versions rolled back. ## Example ```rust let migrator = Migrator::new(migrations()); migrator.run()?; // applied [1, 2] let rolled_back = migrator.rollback(1)?; // runs migration 2's `down` assert_eq!(rolled_back, [2]); migrator.run()?; // re-applies 2 ``` ```rust #[test] fn rollback_without_down_changes_nothing() { let list = MigrationList::new(vec![ Migration::new(1, "a", "CREATE TABLE a (id INTEGER);").down("DROP TABLE a;"), Migration::new(2, "b", "CREATE TABLE b (id INTEGER);"), // no down ]) .unwrap(); let migrator = Migrator::new(list); migrator.run().unwrap(); assert!(migrator.rollback(2).is_err()); // nothing was rolled back, not even migration 2's neighbour assert!(migrator.status().unwrap().iter().all(|s| s.state == MigrationState::Applied)); } ``` ## Acceptance criteria - [ ] Rolling back 1 step and N steps works. - [ ] A missing `down` returns an error with the database unchanged. - [ ] `run()` after a rollback re-applies the rolled-back migrations.
Sign in to join this conversation.
No description provided.