From 7f551f6b5eec3aa449c22a46330b94dadf9d62f3 Mon Sep 17 00:00:00 2001 From: Denis Cornehl Date: Wed, 22 Nov 2023 08:33:06 +0100 Subject: [PATCH] revert migrations after each test again --- src/test/mod.rs | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/src/test/mod.rs b/src/test/mod.rs index eb69296c1..c6df8bc0a 100644 --- a/src/test/mod.rs +++ b/src/test/mod.rs @@ -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") @@ -574,15 +574,16 @@ impl Context for TestEnvironment { pub(crate) struct TestDatabase { pool: Pool, schema: String, + runtime: Arc, } impl TestDatabase { - fn new(config: &Config, runtime: &Runtime, metrics: Arc) -> Result { + fn new(config: &Config, runtime: Arc, metrics: Arc) -> Result { // 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::()); - 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(); @@ -629,7 +630,11 @@ impl TestDatabase { } })?; - Ok(TestDatabase { pool, schema }) + Ok(TestDatabase { + pool, + schema, + runtime, + }) } pub(crate) fn pool(&self) -> Pool { @@ -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(), &[], @@ -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"); } }