@@ -493,7 +493,7 @@ impl TestEnvironment {
493
493
let runtime = self . runtime ( ) ;
494
494
let instance_metrics = self . instance_metrics ( ) ;
495
495
self . runtime ( )
496
- . spawn_blocking ( move || TestDatabase :: new ( & config, & runtime, instance_metrics) )
496
+ . spawn_blocking ( move || TestDatabase :: new ( & config, runtime, instance_metrics) )
497
497
. await
498
498
. unwrap ( )
499
499
. expect ( "failed to initialize the db" )
@@ -574,15 +574,16 @@ impl Context for TestEnvironment {
574
574
pub ( crate ) struct TestDatabase {
575
575
pool : Pool ,
576
576
schema : String ,
577
+ runtime : Arc < Runtime > ,
577
578
}
578
579
579
580
impl 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 > {
581
582
// A random schema name is generated and used for the current connection. This allows each
582
583
// test to create a fresh instance of the database to run within.
583
584
let schema = format ! ( "docs_rs_test_schema_{}" , rand:: random:: <u64 >( ) ) ;
584
585
585
- let pool = Pool :: new_with_schema ( config, runtime, metrics, & schema) ?;
586
+ let pool = Pool :: new_with_schema ( config, & runtime, metrics, & schema) ?;
586
587
587
588
runtime. block_on ( {
588
589
let schema = schema. clone ( ) ;
@@ -629,7 +630,11 @@ impl TestDatabase {
629
630
}
630
631
} ) ?;
631
632
632
- Ok ( TestDatabase { pool, schema } )
633
+ Ok ( TestDatabase {
634
+ pool,
635
+ schema,
636
+ runtime,
637
+ } )
633
638
}
634
639
635
640
pub ( crate ) fn pool ( & self ) -> Pool {
@@ -652,6 +657,11 @@ impl TestDatabase {
652
657
653
658
impl Drop for TestDatabase {
654
659
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
+
655
665
if let Err ( e) = self . conn ( ) . execute (
656
666
format ! ( "DROP SCHEMA {} CASCADE;" , self . schema) . as_str ( ) ,
657
667
& [ ] ,
@@ -660,6 +670,8 @@ impl Drop for TestDatabase {
660
670
}
661
671
// Drop the connection pool so we don't leak database connections
662
672
self . pool . shutdown ( ) ;
673
+
674
+ migration_result. expect ( "downgrading database works" ) ;
663
675
}
664
676
}
665
677
0 commit comments