-
Notifications
You must be signed in to change notification settings - Fork 705
Expand file tree
/
Copy pathyank_version.rs
More file actions
81 lines (69 loc) · 2.23 KB
/
yank_version.rs
File metadata and controls
81 lines (69 loc) · 2.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
use crate::dialoguer;
use crates_io::db;
use crates_io::models::{Crate, GitIndexSyncQueueItem, Version};
use crates_io::schema::versions;
use crates_io::worker::jobs::{SyncToGitIndex, SyncToSparseIndex, UpdateDefaultVersion};
use crates_io_worker::BackgroundJob;
use diesel::prelude::*;
use diesel_async::scoped_futures::ScopedFutureExt;
use diesel_async::{AsyncConnection, AsyncPgConnection, RunQueryDsl};
#[derive(clap::Parser, Debug)]
#[command(
name = "yank-version",
about = "Yank a crate from the database and index."
)]
pub struct Opts {
/// Name of the crate
crate_name: String,
/// Version number that should be deleted
version: String,
/// Don't ask for confirmation: yes, we are sure. Best for scripting.
#[arg(short, long)]
yes: bool,
}
pub async fn run(opts: Opts) -> anyhow::Result<()> {
let mut conn = db::oneoff_connection().await?;
conn.transaction(|conn| yank(opts, conn).scope_boxed())
.await?;
Ok(())
}
async fn yank(opts: Opts, conn: &mut AsyncPgConnection) -> anyhow::Result<()> {
let Opts {
crate_name,
version,
yes,
} = opts;
let krate: Crate = Crate::by_name(&crate_name).first(conn).await?;
let v: Version = Version::belonging_to(&krate)
.filter(versions::num.eq(&version))
.select(Version::as_select())
.first(conn)
.await?;
if v.yanked {
println!("Version {version} of crate {crate_name} is already yanked");
return Ok(());
}
if !yes {
let prompt = format!(
"Are you sure you want to yank {crate_name}#{version} ({})?",
v.id
);
if !dialoguer::confirm(&prompt).await? {
return Ok(());
}
}
println!("yanking version {} ({})", v.num, v.id);
diesel::update(&v)
.set(versions::yanked.eq(true))
.execute(conn)
.await?;
GitIndexSyncQueueItem::queue(conn, &krate.name).await?;
let sparse_index_job = SyncToSparseIndex::new(&krate.name);
let update_default_version_job = UpdateDefaultVersion::new(krate.id);
tokio::try_join!(
SyncToGitIndex.enqueue(conn),
sparse_index_job.enqueue(conn),
update_default_version_job.enqueue(conn),
)?;
Ok(())
}