Skip to content

Commit 7fb54d3

Browse files
authored
Add checksum mismtaches to sqlx-cli migrate info (launchbadge#1680)
This fixes launchbadge#870
1 parent 347374b commit 7fb54d3

File tree

1 file changed

+40
-8
lines changed

1 file changed

+40
-8
lines changed

sqlx-cli/src/migrate.rs

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ use chrono::Utc;
33
use console::style;
44
use sqlx::migrate::{AppliedMigration, Migrate, MigrateError, MigrationType, Migrator};
55
use sqlx::{AnyConnection, Connection};
6+
use std::borrow::Cow;
67
use std::collections::{HashMap, HashSet};
8+
use std::fmt::Write;
79
use std::fs::{self, File};
8-
use std::io::Write;
910
use std::path::Path;
1011
use std::time::Duration;
1112

@@ -30,7 +31,7 @@ fn create_file(
3031

3132
let mut file = File::create(&path).context("Failed to create migration file")?;
3233

33-
file.write_all(migration_type.file_content().as_bytes())?;
34+
std::io::Write::write_all(&mut file, migration_type.file_content().as_bytes())?;
3435

3536
Ok(())
3637
}
@@ -107,6 +108,14 @@ See: https://docs.rs/sqlx/0.5/sqlx/macro.migrate.html
107108
Ok(())
108109
}
109110

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+
110119
pub async fn info(migration_source: &str, uri: &str) -> anyhow::Result<()> {
111120
let migrator = Migrator::new(Path::new(migration_source)).await?;
112121
let mut conn = AnyConnection::connect(uri).await?;
@@ -121,16 +130,39 @@ pub async fn info(migration_source: &str, uri: &str) -> anyhow::Result<()> {
121130
.collect();
122131

123132
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+
124145
println!(
125146
"{}/{} {}",
126147
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
133150
);
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+
}
134166
}
135167

136168
Ok(())

0 commit comments

Comments
 (0)