Skip to content

Commit

Permalink
Improve DB reset process
Browse files Browse the repository at this point in the history
* Use a more appropriate name for the function that does the work
* Clean just the `public` schema, rather than drop/create the whole DB.  This means that running the tests no longer requires superuser privs, and that we don't have to temporarily hide out in `template1`.
* Drop the `catch_unwind`, because it's needed any more.
  • Loading branch information
mpalmer committed May 9, 2024
1 parent dc95b47 commit b729476
Showing 1 changed file with 7 additions and 20 deletions.
27 changes: 7 additions & 20 deletions refinery/tests/postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,39 +66,26 @@ mod postgres {
vec![migration1, migration2, migration3, migration4, migration5]
}

fn clean_database() {
fn prep_database() {
let uri = db_uri();
let db_name = uri.split('/').last().unwrap();

let mut client = Client::connect(
&(uri.strip_suffix(db_name).unwrap().to_string() + "template1"),
NoTls,
)
.unwrap();
let mut client = Client::connect(&db_uri(), NoTls).unwrap();

client
.execute(
"SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE datname=$1",
&[&db_name],
)
.unwrap();
client
.execute(&"DROP DATABASE IF EXISTS $1".replace("$1", db_name), &[])
.execute("DROP SCHEMA IF EXISTS public CASCADE", &[])
.unwrap();
client
.execute(&"CREATE DATABASE $1".replace("$1", db_name), &[])
.execute("CREATE SCHEMA IF NOT EXISTS public", &[])
.unwrap();
}

fn run_test<T>(test: T)
where
T: FnOnce() + std::panic::UnwindSafe,
T: FnOnce()
{
clean_database();

let result = std::panic::catch_unwind(test);
prep_database();

assert!(result.is_ok())
test();
}

#[test]
Expand Down

0 comments on commit b729476

Please sign in to comment.