Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

registry(download): look for extension_name if name is not found #501

Merged
merged 1 commit into from
Oct 19, 2023
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
20 changes: 20 additions & 0 deletions registry/sqlx-data.json
Original file line number Diff line number Diff line change
Expand Up @@ -626,6 +626,26 @@
},
"query": "SELECT id FROM extensions WHERE name = $1"
},
"6991d9aae9d130b3d209ea5816847f661979e07c07f754c15e9b05eee3d61cb6": {
"describe": {
"columns": [
{
"name": "extension_id",
"ordinal": 0,
"type_info": "Int4"
}
],
"nullable": [
true
],
"parameters": {
"Left": [
"Text"
]
}
},
"query": "SELECT extension_id FROM versions WHERE extension_name = $1"
},
"69ef7c7c79e69f31731a41417a0047562f0806a7c73eb4bb98c9ed554fff3b7c": {
"describe": {
"columns": [],
Expand Down
28 changes: 26 additions & 2 deletions registry/src/routes/download.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
//! Functionality for downloading extensions and maintaining download counts
use crate::config::Config;
use crate::download::{check_version, latest_version};
use crate::errors::Result;
use crate::uploader::extension_location;
use crate::{config::Config, extensions::get_extension_id};
use actix_web::{get, web, HttpResponse};
use sqlx::{Pool, Postgres};
use tracing::info;
Expand All @@ -16,7 +16,9 @@ pub async fn download(
path: web::Path<(String, String)>,
) -> Result<HttpResponse> {
let (name, mut version) = path.into_inner();
let extension_id = get_extension_id(&name, conn.as_ref()).await?;
let Ok(extension_id) = get_extension_id_fallback(&name, &conn).await else {
return Ok(HttpResponse::NotFound().body("No extension with the given name was found"));
};

// Use latest version if 'latest' provided as version
if version == "latest" {
Expand Down Expand Up @@ -50,3 +52,25 @@ async fn increase_download_count(pool: &Pool<Postgres>, extension_id: i32) -> Re

Ok(())
}

/// Given an extension name, try to find it in the `extensions` table (more common scenario).
///
/// If it's not found, try to find it in `versions` under `extension_name`.
pub async fn get_extension_id_fallback(extension_name: &str, conn: &Pool<Postgres>) -> Result<i64> {
if let Ok(record) = sqlx::query!("SELECT id FROM extensions WHERE name = $1", extension_name)
.fetch_one(conn)
.await
{
return Ok(record.id);
}

let record = sqlx::query!(
"SELECT extension_id FROM versions WHERE extension_name = $1",
extension_name
)
.fetch_one(conn)
.await?;

// Safe unwrap: if `extension_name` is in versions, `extension_id` must be as well
Ok(record.extension_id.unwrap() as i64)
}
Loading