@@ -3,75 +3,89 @@ use assert_cmd::{assert::Assert, Command};
3
3
use sqlx:: _unstable:: config:: Config ;
4
4
use sqlx:: { migrate:: Migrate , Connection , SqliteConnection } ;
5
5
use std:: {
6
- env:: temp_dir,
7
- fs:: remove_file,
6
+ env, fs,
8
7
path:: { Path , PathBuf } ,
9
8
} ;
10
9
11
10
pub struct TestDatabase {
12
11
file_path : PathBuf ,
13
- migrations : String ,
12
+ migrations_path : PathBuf ,
13
+ pub config_path : Option < PathBuf > ,
14
14
}
15
15
16
16
impl TestDatabase {
17
17
pub fn new ( name : & str , migrations : & str ) -> Self {
18
- let migrations_path = Path :: new ( "tests" ) . join ( migrations) ;
19
- let file_path = Path :: new ( & temp_dir ( ) ) . join ( format ! ( "test-{}.db" , name) ) ;
20
- let ret = Self {
18
+ // Note: only set when _building_
19
+ let temp_dir = option_env ! ( "CARGO_TARGET_TMPDIR" ) . map_or_else ( env:: temp_dir, PathBuf :: from) ;
20
+
21
+ let test_dir = temp_dir. join ( "migrate" ) ;
22
+
23
+ fs:: create_dir_all ( & test_dir)
24
+ . unwrap_or_else ( |e| panic ! ( "error creating directory: {test_dir:?}: {e}" ) ) ;
25
+
26
+ let file_path = test_dir. join ( format ! ( "test-{name}.db" ) ) ;
27
+
28
+ if file_path. exists ( ) {
29
+ fs:: remove_file ( & file_path)
30
+ . unwrap_or_else ( |e| panic ! ( "error deleting test database {file_path:?}: {e}" ) ) ;
31
+ }
32
+
33
+ let this = Self {
21
34
file_path,
22
- migrations : String :: from ( migrations_path. to_str ( ) . unwrap ( ) ) ,
35
+ migrations_path : Path :: new ( "tests" ) . join ( migrations) ,
36
+ config_path : None ,
23
37
} ;
38
+
24
39
Command :: cargo_bin ( "cargo-sqlx" )
25
40
. unwrap ( )
26
41
. args ( [
27
42
"sqlx" ,
28
43
"database" ,
29
44
"create" ,
30
45
"--database-url" ,
31
- & ret . connection_string ( ) ,
46
+ & this . connection_string ( ) ,
32
47
] )
33
48
. assert ( )
34
49
. success ( ) ;
35
- ret
50
+ this
51
+ }
52
+
53
+ pub fn set_migrations ( & mut self , migrations : & str ) {
54
+ self . migrations_path = Path :: new ( "tests" ) . join ( migrations) ;
36
55
}
37
56
38
57
pub fn connection_string ( & self ) -> String {
39
58
format ! ( "sqlite://{}" , self . file_path. display( ) )
40
59
}
41
60
42
61
pub fn run_migration ( & self , revert : bool , version : Option < i64 > , dry_run : bool ) -> Assert {
43
- let ver = match version {
44
- Some ( v) => v. to_string ( ) ,
45
- None => String :: from ( "" ) ,
46
- } ;
47
- Command :: cargo_bin ( "cargo-sqlx" )
48
- . unwrap ( )
49
- . args (
50
- [
51
- vec ! [
52
- "sqlx" ,
53
- "migrate" ,
54
- match revert {
55
- true => "revert" ,
56
- false => "run" ,
57
- } ,
58
- "--database-url" ,
59
- & self . connection_string( ) ,
60
- "--source" ,
61
- & self . migrations,
62
- ] ,
63
- match version {
64
- Some ( _) => vec ! [ "--target-version" , & ver] ,
65
- None => vec ! [ ] ,
66
- } ,
67
- match dry_run {
68
- true => vec ! [ "--dry-run" ] ,
69
- false => vec ! [ ] ,
70
- } ,
71
- ]
72
- . concat ( ) ,
73
- )
74
- . assert ( )
62
+ let mut command = Command :: cargo_bin ( "sqlx" ) . unwrap ( ) ;
63
+ command
64
+ . args ( [
65
+ "migrate" ,
66
+ match revert {
67
+ true => "revert" ,
68
+ false => "run" ,
69
+ } ,
70
+ "--database-url" ,
71
+ & self . connection_string ( ) ,
72
+ "--source" ,
73
+ ] )
74
+ . arg ( & self . migrations_path ) ;
75
+
76
+ if let Some ( config_path) = & self . config_path {
77
+ command. arg ( "--config" ) . arg ( config_path) ;
78
+ }
79
+
80
+ if let Some ( version) = version {
81
+ command. arg ( "--target-version" ) . arg ( version. to_string ( ) ) ;
82
+ }
83
+
84
+ if dry_run {
85
+ command. arg ( "--dry-run" ) ;
86
+ }
87
+
88
+ command. assert ( )
75
89
}
76
90
77
91
pub async fn applied_migrations ( & self ) -> Vec < i64 > {
@@ -88,10 +102,34 @@ impl TestDatabase {
88
102
. map ( |m| m. version )
89
103
. collect ( )
90
104
}
105
+
106
+ pub fn migrate_info ( & self ) -> Assert {
107
+ let mut command = Command :: cargo_bin ( "sqlx" ) . unwrap ( ) ;
108
+ command
109
+ . args ( [
110
+ "migrate" ,
111
+ "info" ,
112
+ "--database-url" ,
113
+ & self . connection_string ( ) ,
114
+ "--source" ,
115
+ ] )
116
+ . arg ( & self . migrations_path ) ;
117
+
118
+ if let Some ( config_path) = & self . config_path {
119
+ command. arg ( "--config" ) . arg ( config_path) ;
120
+ }
121
+
122
+ command. assert ( )
123
+ }
91
124
}
92
125
93
126
impl Drop for TestDatabase {
94
127
fn drop ( & mut self ) {
95
- remove_file ( & self . file_path ) . unwrap ( ) ;
128
+ // Only remove the database if there isn't a failure.
129
+ if !std:: thread:: panicking ( ) {
130
+ fs:: remove_file ( & self . file_path ) . unwrap_or_else ( |e| {
131
+ panic ! ( "error deleting test database {:?}: {e}" , self . file_path)
132
+ } ) ;
133
+ }
96
134
}
97
135
}
0 commit comments