1
1
mod common;
2
2
3
- use common:: TestDatabase ;
3
+ use common:: { MigrateCommand , TestDatabase } ;
4
4
5
5
#[ tokio:: test]
6
6
async fn run_reversible_migrations ( ) {
@@ -11,10 +11,10 @@ async fn run_reversible_migrations() {
11
11
20230401000000 ,
12
12
20230501000000 ,
13
13
] ;
14
- // Without --target-version specified.k
14
+ // Without --target-version specified.
15
15
{
16
16
let db = TestDatabase :: new ( "migrate_run_reversible_latest" , "migrations_reversible" ) ;
17
- db. run_migration ( false , None , false ) . success ( ) ;
17
+ db. run_migration ( MigrateCommand :: Run , None , false ) . success ( ) ;
18
18
assert_eq ! ( db. applied_migrations( ) . await , all_migrations) ;
19
19
}
20
20
// With --target-version specified.
@@ -25,17 +25,17 @@ async fn run_reversible_migrations() {
25
25
) ;
26
26
27
27
// Move to latest, explicitly specified.
28
- db. run_migration ( false , Some ( 20230501000000 ) , false )
28
+ db. run_migration ( MigrateCommand :: Run , Some ( 20230501000000 ) , false )
29
29
. success ( ) ;
30
30
assert_eq ! ( db. applied_migrations( ) . await , all_migrations) ;
31
31
32
32
// Move to latest when we're already at the latest.
33
- db. run_migration ( false , Some ( 20230501000000 ) , false )
33
+ db. run_migration ( MigrateCommand :: Run , Some ( 20230501000000 ) , false )
34
34
. success ( ) ;
35
35
assert_eq ! ( db. applied_migrations( ) . await , all_migrations) ;
36
36
37
37
// Upgrade to an old version.
38
- db. run_migration ( false , Some ( 20230301000000 ) , false )
38
+ db. run_migration ( MigrateCommand :: Run , Some ( 20230301000000 ) , false )
39
39
. failure ( ) ;
40
40
assert_eq ! ( db. applied_migrations( ) . await , all_migrations) ;
41
41
}
@@ -47,34 +47,34 @@ async fn run_reversible_migrations() {
47
47
) ;
48
48
49
49
// First version
50
- db. run_migration ( false , Some ( 20230101000000 ) , false )
50
+ db. run_migration ( MigrateCommand :: Run , Some ( 20230101000000 ) , false )
51
51
. success ( ) ;
52
52
assert_eq ! ( db. applied_migrations( ) . await , vec![ 20230101000000 ] ) ;
53
53
54
54
// Dry run upgrade to latest.
55
- db. run_migration ( false , None , true ) . success ( ) ;
55
+ db. run_migration ( MigrateCommand :: Run , None , true ) . success ( ) ;
56
56
assert_eq ! ( db. applied_migrations( ) . await , vec![ 20230101000000 ] ) ;
57
57
58
58
// Dry run upgrade + 2
59
- db. run_migration ( false , Some ( 20230301000000 ) , true )
59
+ db. run_migration ( MigrateCommand :: Run , Some ( 20230301000000 ) , true )
60
60
. success ( ) ;
61
61
assert_eq ! ( db. applied_migrations( ) . await , vec![ 20230101000000 ] ) ;
62
62
63
63
// Upgrade to non-existent version.
64
- db. run_migration ( false , Some ( 20230901000000999 ) , false )
64
+ db. run_migration ( MigrateCommand :: Run , Some ( 20230901000000999 ) , false )
65
65
. failure ( ) ;
66
66
assert_eq ! ( db. applied_migrations( ) . await , vec![ 20230101000000 ] ) ;
67
67
68
68
// Upgrade + 1
69
- db. run_migration ( false , Some ( 20230201000000 ) , false )
69
+ db. run_migration ( MigrateCommand :: Run , Some ( 20230201000000 ) , false )
70
70
. success ( ) ;
71
71
assert_eq ! (
72
72
db. applied_migrations( ) . await ,
73
73
vec![ 20230101000000 , 20230201000000 ]
74
74
) ;
75
75
76
76
// Upgrade + 2
77
- db. run_migration ( false , Some ( 20230401000000 ) , false )
77
+ db. run_migration ( MigrateCommand :: Run , Some ( 20230401000000 ) , false )
78
78
. success ( ) ;
79
79
assert_eq ! ( db. applied_migrations( ) . await , all_migrations[ ..4 ] ) ;
80
80
}
@@ -93,55 +93,139 @@ async fn revert_migrations() {
93
93
// Without --target-version
94
94
{
95
95
let db = TestDatabase :: new ( "migrate_revert_incremental" , "migrations_reversible" ) ;
96
- db. run_migration ( false , None , false ) . success ( ) ;
96
+ db. run_migration ( MigrateCommand :: Run , None , false ) . success ( ) ;
97
97
98
98
// Dry-run
99
- db. run_migration ( true , None , true ) . success ( ) ;
99
+ db. run_migration ( MigrateCommand :: Revert , None , true )
100
+ . success ( ) ;
100
101
assert_eq ! ( db. applied_migrations( ) . await , all_migrations) ;
101
102
102
103
// Downgrade one
103
- db. run_migration ( true , None , false ) . success ( ) ;
104
+ db. run_migration ( MigrateCommand :: Revert , None , false )
105
+ . success ( ) ;
104
106
assert_eq ! ( db. applied_migrations( ) . await , all_migrations[ ..4 ] ) ;
105
107
106
108
// Downgrade one
107
- db. run_migration ( true , None , false ) . success ( ) ;
109
+ db. run_migration ( MigrateCommand :: Revert , None , false )
110
+ . success ( ) ;
108
111
assert_eq ! ( db. applied_migrations( ) . await , all_migrations[ ..3 ] ) ;
109
112
}
110
113
// With --target-version
111
114
{
112
115
let db = TestDatabase :: new ( "migrate_revert_incremental" , "migrations_reversible" ) ;
113
- db. run_migration ( false , None , false ) . success ( ) ;
116
+ db. run_migration ( MigrateCommand :: Run , None , false ) . success ( ) ;
114
117
115
118
// Dry-run downgrade to version 3.
116
- db. run_migration ( true , Some ( 20230301000000 ) , true ) . success ( ) ;
119
+ db. run_migration ( MigrateCommand :: Revert , Some ( 20230301000000 ) , true )
120
+ . success ( ) ;
117
121
assert_eq ! ( db. applied_migrations( ) . await , all_migrations) ;
118
122
119
123
// Downgrade to version 3.
120
- db. run_migration ( true , Some ( 20230301000000 ) , false )
124
+ db. run_migration ( MigrateCommand :: Revert , Some ( 20230301000000 ) , false )
121
125
. success ( ) ;
122
126
assert_eq ! ( db. applied_migrations( ) . await , all_migrations[ ..3 ] ) ;
123
127
124
128
// Try downgrading to the same version.
125
- db. run_migration ( true , Some ( 20230301000000 ) , false )
129
+ db. run_migration ( MigrateCommand :: Revert , Some ( 20230301000000 ) , false )
126
130
. success ( ) ;
127
131
assert_eq ! ( db. applied_migrations( ) . await , all_migrations[ ..3 ] ) ;
128
132
129
133
// Try downgrading to a newer version.
130
- db. run_migration ( true , Some ( 20230401000000 ) , false )
134
+ db. run_migration ( MigrateCommand :: Revert , Some ( 20230401000000 ) , false )
131
135
. failure ( ) ;
132
136
assert_eq ! ( db. applied_migrations( ) . await , all_migrations[ ..3 ] ) ;
133
137
134
138
// Try downgrading to a non-existent version.
135
- db. run_migration ( true , Some ( 9999 ) , false ) . failure ( ) ;
139
+ db. run_migration ( MigrateCommand :: Revert , Some ( 9999 ) , false )
140
+ . failure ( ) ;
136
141
assert_eq ! ( db. applied_migrations( ) . await , all_migrations[ ..3 ] ) ;
137
142
138
143
// Ensure we can still upgrade
139
- db. run_migration ( false , Some ( 20230401000000 ) , false )
144
+ db. run_migration ( MigrateCommand :: Run , Some ( 20230401000000 ) , false )
140
145
. success ( ) ;
141
146
assert_eq ! ( db. applied_migrations( ) . await , all_migrations[ ..4 ] ) ;
142
147
143
148
// Downgrade to zero.
144
- db. run_migration ( true , Some ( 0 ) , false ) . success ( ) ;
149
+ db. run_migration ( MigrateCommand :: Revert , Some ( 0 ) , false )
150
+ . success ( ) ;
145
151
assert_eq ! ( db. applied_migrations( ) . await , vec![ ] as Vec <i64 >) ;
146
152
}
147
153
}
154
+ #[ tokio:: test]
155
+ async fn skip_reversible_migrations ( ) {
156
+ let all_migrations: Vec < i64 > = vec ! [
157
+ 20230101000000 ,
158
+ 20230201000000 ,
159
+ 20230301000000 ,
160
+ 20230401000000 ,
161
+ 20230501000000 ,
162
+ ] ;
163
+ // Without --target-version specified.
164
+ {
165
+ let db = TestDatabase :: new ( "migrate_skip_reversible_latest" , "migrations_reversible" ) ;
166
+ db. run_migration ( MigrateCommand :: Skip , None , false )
167
+ . success ( ) ;
168
+ assert_eq ! ( db. applied_migrations( ) . await , all_migrations) ;
169
+ }
170
+ // With --target-version specified.
171
+ {
172
+ let db = TestDatabase :: new (
173
+ "migrate_skip_reversible_latest_explicit" ,
174
+ "migrations_reversible" ,
175
+ ) ;
176
+
177
+ // Move to latest, explicitly specified.
178
+ db. run_migration ( MigrateCommand :: Run , Some ( 20230501000000 ) , false )
179
+ . success ( ) ;
180
+ assert_eq ! ( db. applied_migrations( ) . await , all_migrations) ;
181
+
182
+ // Skip to latest when we're already at the latest.
183
+ db. run_migration ( MigrateCommand :: Skip , Some ( 20230501000000 ) , false )
184
+ . success ( ) ;
185
+ assert_eq ! ( db. applied_migrations( ) . await , all_migrations) ;
186
+
187
+ // Upgrade to an old version.
188
+ db. run_migration ( MigrateCommand :: Skip , Some ( 20230301000000 ) , false )
189
+ . failure ( ) ;
190
+ assert_eq ! ( db. applied_migrations( ) . await , all_migrations) ;
191
+ }
192
+ // With --target-version, incrementally upgrade.
193
+ {
194
+ let db = TestDatabase :: new (
195
+ "migrate_skip_reversible_incremental" ,
196
+ "migrations_reversible" ,
197
+ ) ;
198
+
199
+ // Run first version
200
+ db. run_migration ( MigrateCommand :: Run , Some ( 20230101000000 ) , false )
201
+ . success ( ) ;
202
+ assert_eq ! ( db. applied_migrations( ) . await , vec![ 20230101000000 ] ) ;
203
+
204
+ // Skip and dry run upgrade to latest.
205
+ db. run_migration ( MigrateCommand :: Skip , None , true ) . success ( ) ;
206
+ assert_eq ! ( db. applied_migrations( ) . await , vec![ 20230101000000 ] ) ;
207
+
208
+ // Skip and dry run upgrade + 2
209
+ db. run_migration ( MigrateCommand :: Skip , Some ( 20230301000000 ) , true )
210
+ . success ( ) ;
211
+ assert_eq ! ( db. applied_migrations( ) . await , vec![ 20230101000000 ] ) ;
212
+
213
+ // Skip to to non-existent version.
214
+ db. run_migration ( MigrateCommand :: Skip , Some ( 20230901000000999 ) , false )
215
+ . failure ( ) ;
216
+ assert_eq ! ( db. applied_migrations( ) . await , vec![ 20230101000000 ] ) ;
217
+
218
+ // Upgrade + 1
219
+ db. run_migration ( MigrateCommand :: Run , Some ( 20230201000000 ) , false )
220
+ . success ( ) ;
221
+ assert_eq ! (
222
+ db. applied_migrations( ) . await ,
223
+ vec![ 20230101000000 , 20230201000000 ]
224
+ ) ;
225
+
226
+ // Skip + 2
227
+ db. run_migration ( MigrateCommand :: Skip , Some ( 20230401000000 ) , false )
228
+ . success ( ) ;
229
+ assert_eq ! ( db. applied_migrations( ) . await , all_migrations[ ..4 ] ) ;
230
+ }
231
+ }
0 commit comments