diff --git a/butane/tests/r2d2.rs b/butane/tests/r2d2.rs index c4183e0..42f7226 100644 --- a/butane/tests/r2d2.rs +++ b/butane/tests/r2d2.rs @@ -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); @@ -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); diff --git a/butane/tests/unmigrate.rs b/butane/tests/unmigrate.rs new file mode 100644 index 0000000..683b769 --- /dev/null +++ b/butane/tests/unmigrate.rs @@ -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); diff --git a/butane_test_helper/src/lib.rs b/butane_test_helper/src/lib.rs index 9bb1e8c..bec1926 100644 --- a/butane_test_helper/src/lib.rs +++ b/butane_test_helper/src/lib.rs @@ -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; @@ -179,16 +181,15 @@ pub fn pg_connstr(data: &PgSetupData) -> String { data.connstr.clone() } -/// Populate the database schema. -pub fn setup_db(backend: Box, 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. @@ -208,6 +209,12 @@ pub fn setup_db(backend: Box, 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(); } @@ -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); diff --git a/examples/newtype/tests/rollback.rs b/examples/newtype/tests/rollback.rs index a629518..86f67f5 100644 --- a/examples/newtype/tests/rollback.rs +++ b/examples/newtype/tests/rollback.rs @@ -1,5 +1,5 @@ use butane::db::{BackendConnection, Connection}; -use butane::migrations::{Migration, Migrations}; +use butane::migrations::Migrations; use butane::DataObject; use butane_test_helper::*; @@ -31,27 +31,12 @@ fn migrate_and_rollback(mut connection: Connection) { // Migrate forward. let base_dir = std::path::PathBuf::from(".butane"); let migrations = butane_cli::get_migrations(&base_dir).unwrap(); - let to_apply = migrations.unapplied_migrations(&connection).unwrap(); migrations.migrate(&mut connection).unwrap(); insert_data(&connection); // Rollback migrations. - for migration in to_apply.iter().rev() { - if connection.backend_name() == "pg" && migration.name() == "20240401_095709389_init" { - // Postgres error db error: ERROR: cannot drop table blog because other objects depend on it - // DETAIL: constraint post_blog_fkey on table post depends on table blog - // HINT: Use DROP ... CASCADE to drop the dependent objects too. - let err = migration.downgrade(&mut connection).unwrap_err(); - eprintln!("Rolled back {} failed: {err:?}", migration.name()); - return; - } - - migration - .downgrade(&mut connection) - .unwrap_or_else(|err| panic!("rollback of {} failed: {err}", migration.name())); - eprintln!("Rolled back {}", migration.name()); - } + migrations.unmigrate(&mut connection).unwrap(); } testall_no_migrate!(migrate_and_rollback);