Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 46 additions & 28 deletions src/admin/delete_version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
use crate::worker::jobs;
use crate::{admin::dialoguer, db, schema::versions};
use anyhow::Context;
use diesel::prelude::*;
use diesel::{Connection, ExpressionMethods, QueryDsl};
use diesel_async::async_connection_wrapper::AsyncConnectionWrapper;

#[derive(clap::Parser, Debug)]
#[command(
Expand All @@ -27,18 +28,25 @@
}

pub async fn run(opts: Opts) -> anyhow::Result<()> {
spawn_blocking(move || {
let crate_name = &opts.crate_name;
let mut conn = db::oneoff_async_connection()
.await
.context("Failed to establish database connection")?;

Check warning on line 33 in src/admin/delete_version.rs

View check run for this annotation

Codecov / codecov/patch

src/admin/delete_version.rs#L31-L33

Added lines #L31 - L33 were not covered by tests

let conn = &mut db::oneoff_connection().context("Failed to establish database connection")?;
let store = Storage::from_environment();

Check warning on line 35 in src/admin/delete_version.rs

View check run for this annotation

Codecov / codecov/patch

src/admin/delete_version.rs#L35

Added line #L35 was not covered by tests

let store = Storage::from_environment();
let crate_id: i32 = {
use diesel_async::RunQueryDsl;

Check warning on line 38 in src/admin/delete_version.rs

View check run for this annotation

Codecov / codecov/patch

src/admin/delete_version.rs#L37-L38

Added lines #L37 - L38 were not covered by tests

let crate_id: i32 = crates::table
crates::table

Check warning on line 40 in src/admin/delete_version.rs

View check run for this annotation

Codecov / codecov/patch

src/admin/delete_version.rs#L40

Added line #L40 was not covered by tests
.select(crates::id)
.filter(crates::name.eq(crate_name))
.first(conn)
.context("Failed to look up crate id from the database")?;
.filter(crates::name.eq(&opts.crate_name))
.first(&mut conn)
.await
.context("Failed to look up crate id from the database")
}?;

Check warning on line 46 in src/admin/delete_version.rs

View check run for this annotation

Codecov / codecov/patch

src/admin/delete_version.rs#L42-L46

Added lines #L42 - L46 were not covered by tests

{
let crate_name = &opts.crate_name;

Check warning on line 49 in src/admin/delete_version.rs

View check run for this annotation

Codecov / codecov/patch

src/admin/delete_version.rs#L49

Added line #L49 was not covered by tests

println!("Deleting the following versions of the `{crate_name}` crate:");
println!();
Expand All @@ -47,9 +55,20 @@
}
println!();

if !opts.yes && !dialoguer::confirm("Do you want to permanently delete these versions?")? {
if !opts.yes
&& !dialoguer::async_confirm("Do you want to permanently delete these versions?")
.await?

Check warning on line 60 in src/admin/delete_version.rs

View check run for this annotation

Codecov / codecov/patch

src/admin/delete_version.rs#L58-L60

Added lines #L58 - L60 were not covered by tests
{
return Ok(());
}
}

let opts = spawn_blocking::<_, _, anyhow::Error>(move || {

Check warning on line 66 in src/admin/delete_version.rs

View check run for this annotation

Codecov / codecov/patch

src/admin/delete_version.rs#L66

Added line #L66 was not covered by tests
use diesel::RunQueryDsl;

let conn: &mut AsyncConnectionWrapper<_> = &mut conn.into();

let crate_name = &opts.crate_name;

Check warning on line 71 in src/admin/delete_version.rs

View check run for this annotation

Codecov / codecov/patch

src/admin/delete_version.rs#L69-L71

Added lines #L69 - L71 were not covered by tests

conn.transaction(|conn| {
info!(%crate_name, %crate_id, versions = ?opts.versions, "Deleting versions from the database");
Expand Down Expand Up @@ -87,27 +106,26 @@
warn!(%crate_name, ?error, "Failed to enqueue index sync jobs");
}

let rt = tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.context("Failed to initialize tokio runtime")?;
Ok(opts)
}).await?;

Check warning on line 110 in src/admin/delete_version.rs

View check run for this annotation

Codecov / codecov/patch

src/admin/delete_version.rs#L109-L110

Added lines #L109 - L110 were not covered by tests

for version in &opts.versions {
debug!(%crate_name, %version, "Deleting crate file from S3");
if let Err(error) = rt.block_on(store.delete_crate_file(crate_name, version)) {
warn!(%crate_name, %version, ?error, "Failed to delete crate file from S3");
}
let crate_name = &opts.crate_name;

Check warning on line 112 in src/admin/delete_version.rs

View check run for this annotation

Codecov / codecov/patch

src/admin/delete_version.rs#L112

Added line #L112 was not covered by tests

debug!(%crate_name, %version, "Deleting readme file from S3");
match rt.block_on(store.delete_readme(crate_name, version)) {
Err(object_store::Error::NotFound { .. }) => {}
Err(error) => {
warn!(%crate_name, %version, ?error, "Failed to delete readme file from S3")
}
Ok(_) => {}
for version in &opts.versions {
debug!(%crate_name, %version, "Deleting crate file from S3");
if let Err(error) = store.delete_crate_file(crate_name, version).await {
warn!(%crate_name, %version, ?error, "Failed to delete crate file from S3");
}

Check warning on line 118 in src/admin/delete_version.rs

View check run for this annotation

Codecov / codecov/patch

src/admin/delete_version.rs#L114-L118

Added lines #L114 - L118 were not covered by tests

debug!(%crate_name, %version, "Deleting readme file from S3");
match store.delete_readme(crate_name, version).await {
Err(object_store::Error::NotFound { .. }) => {}
Err(error) => {
warn!(%crate_name, %version, ?error, "Failed to delete readme file from S3")

Check warning on line 124 in src/admin/delete_version.rs

View check run for this annotation

Codecov / codecov/patch

src/admin/delete_version.rs#L120-L124

Added lines #L120 - L124 were not covered by tests
}
Ok(_) => {}

Check warning on line 126 in src/admin/delete_version.rs

View check run for this annotation

Codecov / codecov/patch

src/admin/delete_version.rs#L126

Added line #L126 was not covered by tests
}
}

Ok(())
}).await
Ok(())

Check warning on line 130 in src/admin/delete_version.rs

View check run for this annotation

Codecov / codecov/patch

src/admin/delete_version.rs#L130

Added line #L130 was not covered by tests
}
8 changes: 7 additions & 1 deletion src/admin/dialoguer.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
use crate::tasks::spawn_blocking;
use ::dialoguer::{theme::Theme, Confirm};

pub fn confirm(msg: &str) -> dialoguer::Result<bool> {
pub async fn async_confirm(msg: impl Into<String>) -> anyhow::Result<bool> {
let msg = msg.into();
spawn_blocking(move || confirm(msg).map_err(anyhow::Error::from)).await
}

Check warning on line 7 in src/admin/dialoguer.rs

View check run for this annotation

Codecov / codecov/patch

src/admin/dialoguer.rs#L4-L7

Added lines #L4 - L7 were not covered by tests

pub fn confirm(msg: impl Into<String>) -> dialoguer::Result<bool> {

Check warning on line 9 in src/admin/dialoguer.rs

View check run for this annotation

Codecov / codecov/patch

src/admin/dialoguer.rs#L9

Added line #L9 was not covered by tests
Confirm::with_theme(&CustomTheme)
.with_prompt(msg)
.default(false)
Expand Down