Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test to unmigrate test models #252

Merged
merged 2 commits into from
Apr 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
19 changes: 2 additions & 17 deletions examples/newtype/tests/rollback.rs
Original file line number Diff line number Diff line change
@@ -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::*;

Expand Down Expand Up @@ -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);