From 6425322469da025c422b05c0a4229e28f884e11c Mon Sep 17 00:00:00 2001 From: Denis Cornehl Date: Sat, 28 Sep 2024 13:47:43 +0200 Subject: [PATCH] use crate name normalization when trying to delete crates / releases --- ...4d9b1b4acd434f966d1da2db30c509c6089d8.json | 22 +++++++++++++++ src/db/delete.rs | 28 +++++++++++++++---- 2 files changed, 45 insertions(+), 5 deletions(-) create mode 100644 .sqlx/query-cec7e0053fc9710683bb8b2c7f74d9b1b4acd434f966d1da2db30c509c6089d8.json diff --git a/.sqlx/query-cec7e0053fc9710683bb8b2c7f74d9b1b4acd434f966d1da2db30c509c6089d8.json b/.sqlx/query-cec7e0053fc9710683bb8b2c7f74d9b1b4acd434f966d1da2db30c509c6089d8.json new file mode 100644 index 000000000..96cc1e631 --- /dev/null +++ b/.sqlx/query-cec7e0053fc9710683bb8b2c7f74d9b1b4acd434f966d1da2db30c509c6089d8.json @@ -0,0 +1,22 @@ +{ + "db_name": "PostgreSQL", + "query": "SELECT id FROM crates WHERE normalize_crate_name(name) = normalize_crate_name($1)", + "describe": { + "columns": [ + { + "ordinal": 0, + "name": "id", + "type_info": "Int4" + } + ], + "parameters": { + "Left": [ + "Varchar" + ] + }, + "nullable": [ + false + ] + }, + "hash": "cec7e0053fc9710683bb8b2c7f74d9b1b4acd434f966d1da2db30c509c6089d8" +} diff --git a/src/db/delete.rs b/src/db/delete.rs index c0932db92..31a56a650 100644 --- a/src/db/delete.rs +++ b/src/db/delete.rs @@ -105,12 +105,13 @@ pub async fn delete_version( } async fn get_id(conn: &mut sqlx::PgConnection, name: &str) -> Result { - Ok( - sqlx::query_scalar!("SELECT id FROM crates WHERE name = $1", name) - .fetch_optional(&mut *conn) - .await? - .ok_or_else(|| CrateDeletionError::MissingCrate(name.into()))?, + Ok(sqlx::query_scalar!( + "SELECT id FROM crates WHERE normalize_crate_name(name) = normalize_crate_name($1)", + name ) + .fetch_optional(&mut *conn) + .await? + .ok_or_else(|| CrateDeletionError::MissingCrate(name.into()))?) } // metaprogramming! @@ -233,6 +234,23 @@ mod tests { .is_some()) } + #[test] + fn test_get_id_uses_normalization() { + async_wrapper(|env| async move { + env.async_fake_release() + .await + .name("Some_Package") + .version("1.0.0") + .create_async() + .await?; + + let mut conn = env.async_db().await.async_conn().await; + assert!(get_id(&mut conn, "some-package").await.is_ok()); + + Ok(()) + }) + } + #[test_case(true)] #[test_case(false)] fn test_delete_crate(archive_storage: bool) {