Skip to content

Commit

Permalink
revert migrations after each test again
Browse files Browse the repository at this point in the history
  • Loading branch information
syphar committed Nov 22, 2023
1 parent 26f9666 commit 7f551f6
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions src/test/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ impl TestEnvironment {
let runtime = self.runtime();
let instance_metrics = self.instance_metrics();
self.runtime()
.spawn_blocking(move || TestDatabase::new(&config, &runtime, instance_metrics))
.spawn_blocking(move || TestDatabase::new(&config, runtime, instance_metrics))
.await
.unwrap()
.expect("failed to initialize the db")
Expand Down Expand Up @@ -574,15 +574,16 @@ impl Context for TestEnvironment {
pub(crate) struct TestDatabase {
pool: Pool,
schema: String,
runtime: Arc<Runtime>,
}

impl TestDatabase {
fn new(config: &Config, runtime: &Runtime, metrics: Arc<InstanceMetrics>) -> Result<Self> {
fn new(config: &Config, runtime: Arc<Runtime>, metrics: Arc<InstanceMetrics>) -> Result<Self> {
// A random schema name is generated and used for the current connection. This allows each
// test to create a fresh instance of the database to run within.
let schema = format!("docs_rs_test_schema_{}", rand::random::<u64>());

let pool = Pool::new_with_schema(config, runtime, metrics, &schema)?;
let pool = Pool::new_with_schema(config, &runtime, metrics, &schema)?;

runtime.block_on({
let schema = schema.clone();
Expand Down Expand Up @@ -629,7 +630,11 @@ impl TestDatabase {
}
})?;

Ok(TestDatabase { pool, schema })
Ok(TestDatabase {
pool,
schema,
runtime,
})
}

pub(crate) fn pool(&self) -> Pool {
Expand All @@ -652,6 +657,11 @@ impl TestDatabase {

impl Drop for TestDatabase {
fn drop(&mut self) {
let migration_result = self.runtime.block_on(async {
let mut conn = self.async_conn().await;
db::migrate(&mut conn, Some(0)).await
});

if let Err(e) = self.conn().execute(
format!("DROP SCHEMA {} CASCADE;", self.schema).as_str(),
&[],
Expand All @@ -660,6 +670,8 @@ impl Drop for TestDatabase {
}
// Drop the connection pool so we don't leak database connections
self.pool.shutdown();

migration_result.expect("downgrading database works");
}
}

Expand Down

0 comments on commit 7f551f6

Please sign in to comment.