Skip to content

Commit 19213f3

Browse files
committed
Teach yank to update max_version where appropriate
A crate's max_version is updated when the version we are yanking is the current max_version and when unyanking a version newer than the current max_version.
1 parent 6127468 commit 19213f3

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

src/version.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,30 @@ impl Version {
193193
pub fn yank(&self, conn: &GenericConnection, yanked: bool) -> CargoResult<()> {
194194
conn.execute("UPDATE versions SET yanked = $1 WHERE id = $2",
195195
&[&yanked, &self.id])?;
196+
197+
let rows = conn.query("SELECT max_version FROM crates WHERE id = $1",
198+
&[&self.crate_id])?;
199+
let max_version = rows.iter()
200+
.next()
201+
.map(|r| r.get::<&str, String>("max_version"))
202+
.map(|v| semver::Version::parse(&v).unwrap())
203+
.unwrap();
204+
let zero = semver::Version::parse("0.0.0").unwrap();
205+
let max_update_stmt = conn.prepare("UPDATE crates SET max_version = $1 WHERE id = $2")?;
206+
if yanked && max_version == self.num {
207+
let new_max = conn.query("SELECT num FROM versions \
208+
WHERE crate_id = $1 AND yanked = FALSE AND id != $2",
209+
&[&self.crate_id, &self.id])?
210+
.iter()
211+
.map(|r| r.get::<&str, String>("num"))
212+
.filter_map(|v| semver::Version::parse(&v).ok())
213+
.max()
214+
.unwrap();
215+
max_update_stmt.execute(&[&new_max.to_string(), &self.crate_id])?;
216+
} else if !yanked && (self.num > max_version || max_version == zero) {
217+
max_update_stmt.execute(&[&self.num.to_string(), &self.crate_id])?;
218+
}
219+
196220
Ok(())
197221
}
198222
}

0 commit comments

Comments
 (0)