Skip to content

Commit c9079c4

Browse files
authoredApr 21, 2024··
Add test to unmigrate test models (#252)
1 parent 3109517 commit c9079c4

File tree

3 files changed

+38
-13
lines changed

3 files changed

+38
-13
lines changed
 

‎butane/tests/r2d2.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,7 @@ fn r2d2_sqlite() {
1919
let mut conn1 = pool.get().unwrap();
2020
assert_eq!(pool.state().connections, 3);
2121
assert_eq!(pool.state().idle_connections, 2);
22-
setup_db(
23-
Box::new(butane::db::sqlite::SQLiteBackend::new()),
24-
&mut conn1,
25-
true,
26-
);
22+
setup_db(&mut conn1);
2723

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

4743
let _conn2 = pool.get().unwrap();
4844
assert_eq!(pool.state().idle_connections, 1);

‎butane/tests/unmigrate.rs

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//! Test the "current" migration created by the butane_test_helper due to
2+
//! all of the other tests in the butane/tests directory.
3+
#![cfg(test)]
4+
use butane::db::Connection;
5+
use butane::migrations::{Migration, Migrations};
6+
use butane_test_helper::*;
7+
8+
fn unmigrate(mut connection: Connection) {
9+
let mem_migrations = create_current_migrations(&connection);
10+
11+
let migrations = mem_migrations.unapplied_migrations(&connection).unwrap();
12+
assert_eq!(migrations.len(), 0);
13+
14+
let migration = mem_migrations.latest().unwrap();
15+
migration.downgrade(&mut connection).unwrap();
16+
17+
let migrations = mem_migrations.unapplied_migrations(&connection).unwrap();
18+
assert_eq!(migrations.len(), 1);
19+
}
20+
testall!(unmigrate);

‎butane_test_helper/src/lib.rs

+16-7
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ use std::process::{ChildStderr, Command, Stdio};
99
use std::sync::Mutex;
1010

1111
use block_id::{Alphabet, BlockId};
12-
use butane_core::db::{connect, get_backend, pg, sqlite, Backend, Connection, ConnectionSpec};
12+
use butane_core::db::{
13+
connect, get_backend, pg, sqlite, Backend, BackendConnection, Connection, ConnectionSpec,
14+
};
1315
use butane_core::migrations::{self, MemMigrations, Migration, Migrations, MigrationsMut};
1416
use once_cell::sync::Lazy;
1517
use uuid::Uuid;
@@ -179,16 +181,15 @@ pub fn pg_connstr(data: &PgSetupData) -> String {
179181
data.connstr.clone()
180182
}
181183

182-
/// Populate the database schema.
183-
pub fn setup_db(backend: Box<dyn Backend>, conn: &mut Connection, migrate: bool) {
184+
/// Create a [`MemMigrations`]` for the "current" migration.
185+
pub fn create_current_migrations(connection: &Connection) -> MemMigrations {
186+
let backend = connection.backend();
187+
184188
let mut root = std::env::current_dir().unwrap();
185189
root.push(".butane/migrations");
186190
let mut disk_migrations = migrations::from_root(&root);
187191
let disk_current = disk_migrations.current();
188192
log::info!("Loading migrations from {:?}", disk_current);
189-
if !migrate {
190-
return;
191-
}
192193
// Create an in-memory Migrations and write only to that. This
193194
// allows concurrent tests to avoid stomping on each other and is
194195
// also faster than real disk writes.
@@ -208,6 +209,12 @@ pub fn setup_db(backend: Box<dyn Backend>, conn: &mut Connection, migrate: bool)
208209
.expect("expected to create migration without error"),
209210
"expected to create migration"
210211
);
212+
mem_migrations
213+
}
214+
215+
/// Populate the database schema.
216+
pub fn setup_db(conn: &mut Connection) {
217+
let mem_migrations = create_current_migrations(conn);
211218
log::info!("created current migration");
212219
mem_migrations.migrate(conn).unwrap();
213220
}
@@ -240,7 +247,9 @@ macro_rules! maketest {
240247
let $dataname = butane_test_helper::[<$backend _setup>]();
241248
log::info!("connecting to {}..", &$connstr);
242249
let mut conn = backend.connect(&$connstr).expect("Could not connect backend");
243-
butane_test_helper::setup_db(backend, &mut conn, $migrate);
250+
if $migrate {
251+
butane_test_helper::setup_db(&mut conn);
252+
}
244253
log::info!("running test on {}..", &$connstr);
245254
$fname(conn);
246255
butane_test_helper::[<$backend _teardown>]($dataname);

0 commit comments

Comments
 (0)
Please sign in to comment.