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 b33ee0f commit f4d4a60
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 13 deletions.
8 changes: 2 additions & 6 deletions butane/tests/r2d2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,7 @@ fn r2d2_sqlite() {
let mut conn1 = pool.get().unwrap();
assert_eq!(pool.state().connections, 3);
assert_eq!(pool.state().idle_connections, 2);
setup_db(
Box::new(butane::db::sqlite::SQLiteBackend::new()),
&mut conn1,
true,
);
setup_db(&mut conn1);

let _conn2 = pool.get().unwrap();
assert_eq!(pool.state().idle_connections, 1);
Expand All @@ -42,7 +38,7 @@ fn r2d2_pq() {
let mut conn1 = pool.get().unwrap();
assert_eq!(pool.state().connections, 3);
assert_eq!(pool.state().idle_connections, 2);
setup_db(Box::new(butane::db::pg::PgBackend::new()), &mut conn1, true);
setup_db(&mut conn1);

let _conn2 = pool.get().unwrap();
assert_eq!(pool.state().idle_connections, 1);
Expand Down
20 changes: 20 additions & 0 deletions butane/tests/unmigrate.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//! 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::Connection;
use butane::migrations::{Migration, Migrations};
use butane_test_helper::*;

fn unmigrate(mut connection: Connection) {
let mem_migrations = create_current_migrations(&connection);

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);
23 changes: 16 additions & 7 deletions butane_test_helper/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ use std::process::{ChildStderr, Command, Stdio};
use std::sync::Mutex;

use block_id::{Alphabet, BlockId};
use butane_core::db::{connect, get_backend, pg, sqlite, Backend, Connection, ConnectionSpec};
use butane_core::db::{
connect, get_backend, pg, sqlite, Backend, BackendConnection, Connection, ConnectionSpec,
};
use butane_core::migrations::{self, MemMigrations, Migration, Migrations, MigrationsMut};
use once_cell::sync::Lazy;
use uuid::Uuid;
Expand Down Expand Up @@ -179,16 +181,15 @@ pub fn pg_connstr(data: &PgSetupData) -> String {
data.connstr.clone()
}

/// Populate the database schema.
pub fn setup_db(backend: Box<dyn Backend>, conn: &mut Connection, migrate: bool) {
/// Create a [`MemMigrations`]` for the "current" migration.
pub fn create_current_migrations(connection: &Connection) -> MemMigrations {
let backend = connection.backend();

let mut root = std::env::current_dir().unwrap();
root.push(".butane/migrations");
let mut disk_migrations = migrations::from_root(&root);
let disk_current = disk_migrations.current();
log::info!("Loading migrations from {:?}", disk_current);
if !migrate {
return;
}
// Create an in-memory Migrations and write only to that. This
// allows concurrent tests to avoid stomping on each other and is
// also faster than real disk writes.
Expand All @@ -208,6 +209,12 @@ pub fn setup_db(backend: Box<dyn Backend>, conn: &mut Connection, migrate: bool)
.expect("expected to create migration without error"),
"expected to create migration"
);
mem_migrations
}

/// Populate the database schema.
pub fn setup_db(conn: &mut Connection) {
let mem_migrations = create_current_migrations(conn);
log::info!("created current migration");
mem_migrations.migrate(conn).unwrap();
}
Expand Down Expand Up @@ -240,7 +247,9 @@ macro_rules! maketest {
let $dataname = butane_test_helper::[<$backend _setup>]();
log::info!("connecting to {}..", &$connstr);
let mut conn = backend.connect(&$connstr).expect("Could not connect backend");
butane_test_helper::setup_db(backend, &mut conn, $migrate);
if $migrate {
butane_test_helper::setup_db(&mut conn);
}
log::info!("running test on {}..", &$connstr);
$fname(conn);
butane_test_helper::[<$backend _teardown>]($dataname);
Expand Down

0 comments on commit f4d4a60

Please sign in to comment.