@@ -3,9 +3,10 @@ use chrono::Utc;
3
3
use console:: style;
4
4
use sqlx:: migrate:: { AppliedMigration , Migrate , MigrateError , MigrationType , Migrator } ;
5
5
use sqlx:: { AnyConnection , Connection } ;
6
+ use std:: borrow:: Cow ;
6
7
use std:: collections:: { HashMap , HashSet } ;
8
+ use std:: fmt:: Write ;
7
9
use std:: fs:: { self , File } ;
8
- use std:: io:: Write ;
9
10
use std:: path:: Path ;
10
11
use std:: time:: Duration ;
11
12
@@ -30,7 +31,7 @@ fn create_file(
30
31
31
32
let mut file = File :: create ( & path) . context ( "Failed to create migration file" ) ?;
32
33
33
- file . write_all ( migration_type. file_content ( ) . as_bytes ( ) ) ?;
34
+ std :: io :: Write :: write_all ( & mut file , migration_type. file_content ( ) . as_bytes ( ) ) ?;
34
35
35
36
Ok ( ( ) )
36
37
}
@@ -107,6 +108,14 @@ See: https://docs.rs/sqlx/0.5/sqlx/macro.migrate.html
107
108
Ok ( ( ) )
108
109
}
109
110
111
+ fn short_checksum ( checksum : & [ u8 ] ) -> String {
112
+ let mut s = String :: with_capacity ( checksum. len ( ) * 2 ) ;
113
+ for b in checksum {
114
+ write ! ( & mut s, "{:02x?}" , b) . expect ( "should not fail to write to str" ) ;
115
+ }
116
+ s
117
+ }
118
+
110
119
pub async fn info ( migration_source : & str , uri : & str ) -> anyhow:: Result < ( ) > {
111
120
let migrator = Migrator :: new ( Path :: new ( migration_source) ) . await ?;
112
121
let mut conn = AnyConnection :: connect ( uri) . await ?;
@@ -121,16 +130,39 @@ pub async fn info(migration_source: &str, uri: &str) -> anyhow::Result<()> {
121
130
. collect ( ) ;
122
131
123
132
for migration in migrator. iter ( ) {
133
+ let applied = applied_migrations. get ( & migration. version ) ;
134
+
135
+ let ( status_msg, mismatched_checksum) = if let Some ( applied) = applied {
136
+ if applied. checksum != migration. checksum {
137
+ ( style ( "installed (different checksum)" ) . red ( ) , true )
138
+ } else {
139
+ ( style ( "installed" ) . green ( ) , false )
140
+ }
141
+ } else {
142
+ ( style ( "pending" ) . yellow ( ) , false )
143
+ } ;
144
+
124
145
println ! (
125
146
"{}/{} {}" ,
126
147
style( migration. version) . cyan( ) ,
127
- if applied_migrations. contains_key( & migration. version) {
128
- style( "installed" ) . green( )
129
- } else {
130
- style( "pending" ) . yellow( )
131
- } ,
132
- migration. description,
148
+ status_msg,
149
+ migration. description
133
150
) ;
151
+
152
+ if mismatched_checksum {
153
+ println ! (
154
+ "applied migration had checksum {}" ,
155
+ short_checksum(
156
+ & applied
157
+ . map( |a| a. checksum. clone( ) )
158
+ . unwrap_or_else( || Cow :: Owned ( vec![ ] ) )
159
+ ) ,
160
+ ) ;
161
+ println ! (
162
+ "local migration has checksum {}" ,
163
+ short_checksum( & migration. checksum)
164
+ )
165
+ }
134
166
}
135
167
136
168
Ok ( ( ) )
0 commit comments