Skip to content

Commit

Permalink
Add test to unmigrate test models
Browse files Browse the repository at this point in the history
  • Loading branch information
jayvdb committed Apr 20, 2024
1 parent 3cfc92e commit cab8670
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions butane/tests/unmigrate.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//! Test the "current" migration created by the butane_test_helper due to
//! all of the other tests in the butane/tests directory.
#![cfg(test)]
use butane::db::{BackendConnection, Connection};
use butane::migrations::{self, MemMigrations, Migration, Migrations, MigrationsMut};
use butane_test_helper::*;

fn unmigrate(mut connection: Connection) {
let base_dir = std::path::PathBuf::from(".butane");
let root = base_dir.join("migrations");
let mut disk_migrations = migrations::from_root(root);

// this reproduces the migration created in butane_test_helper::setup_db
let disk_current = disk_migrations.current();
let mut mem_migrations = MemMigrations::new();
let mem_current = mem_migrations.current();
migrations::copy_migration(disk_current, mem_current).unwrap();

assert!(
disk_current.db().unwrap().tables().count() != 0,
"No tables to migrate"
);

let backend_name = connection.backend_name();
let backend = butane::db::get_backend(backend_name).unwrap();
assert!(
mem_migrations
.create_migration(&nonempty::nonempty![backend], "init", None)
.expect("expected to create migration without error"),
"expected to create migration"
);

let migrations = mem_migrations.unapplied_migrations(&connection).unwrap();
assert_eq!(migrations.len(), 0);

let migration = mem_migrations.latest().unwrap();
migration.downgrade(&mut connection).unwrap();

let migrations = mem_migrations.unapplied_migrations(&connection).unwrap();
assert_eq!(migrations.len(), 1);
}
testall!(unmigrate);

0 comments on commit cab8670

Please sign in to comment.