@@ -493,7 +493,7 @@ impl TestEnvironment {
493493 let runtime = self . runtime ( ) ;
494494 let instance_metrics = self . instance_metrics ( ) ;
495495 self . runtime ( )
496- . spawn_blocking ( move || TestDatabase :: new ( & config, & runtime, instance_metrics) )
496+ . spawn_blocking ( move || TestDatabase :: new ( & config, runtime, instance_metrics) )
497497 . await
498498 . unwrap ( )
499499 . expect ( "failed to initialize the db" )
@@ -574,15 +574,16 @@ impl Context for TestEnvironment {
574574pub ( crate ) struct TestDatabase {
575575 pool : Pool ,
576576 schema : String ,
577+ runtime : Arc < Runtime > ,
577578}
578579
579580impl TestDatabase {
580- fn new ( config : & Config , runtime : & Runtime , metrics : Arc < InstanceMetrics > ) -> Result < Self > {
581+ fn new ( config : & Config , runtime : Arc < Runtime > , metrics : Arc < InstanceMetrics > ) -> Result < Self > {
581582 // A random schema name is generated and used for the current connection. This allows each
582583 // test to create a fresh instance of the database to run within.
583584 let schema = format ! ( "docs_rs_test_schema_{}" , rand:: random:: <u64 >( ) ) ;
584585
585- let pool = Pool :: new_with_schema ( config, runtime, metrics, & schema) ?;
586+ let pool = Pool :: new_with_schema ( config, & runtime, metrics, & schema) ?;
586587
587588 runtime. block_on ( {
588589 let schema = schema. clone ( ) ;
@@ -629,7 +630,11 @@ impl TestDatabase {
629630 }
630631 } ) ?;
631632
632- Ok ( TestDatabase { pool, schema } )
633+ Ok ( TestDatabase {
634+ pool,
635+ schema,
636+ runtime,
637+ } )
633638 }
634639
635640 pub ( crate ) fn pool ( & self ) -> Pool {
@@ -652,6 +657,11 @@ impl TestDatabase {
652657
653658impl Drop for TestDatabase {
654659 fn drop ( & mut self ) {
660+ let migration_result = self . runtime . block_on ( async {
661+ let mut conn = self . async_conn ( ) . await ;
662+ db:: migrate ( & mut conn, Some ( 0 ) ) . await
663+ } ) ;
664+
655665 if let Err ( e) = self . conn ( ) . execute (
656666 format ! ( "DROP SCHEMA {} CASCADE;" , self . schema) . as_str ( ) ,
657667 & [ ] ,
@@ -660,6 +670,8 @@ impl Drop for TestDatabase {
660670 }
661671 // Drop the connection pool so we don't leak database connections
662672 self . pool . shutdown ( ) ;
673+
674+ migration_result. expect ( "downgrading database works" ) ;
663675 }
664676}
665677
0 commit comments