Skip to content

Commit

Permalink
registry: keep track of download counts (#487)
Browse files Browse the repository at this point in the history
  • Loading branch information
vrmiguel authored Oct 13, 2023
1 parent 7082f32 commit dedfe1c
Show file tree
Hide file tree
Showing 5 changed files with 179 additions and 69 deletions.
11 changes: 11 additions & 0 deletions registry/migrations/20231012133409_add-download-count.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
ALTER TABLE versions
ADD COLUMN download_count integer NOT NULL DEFAULT 0;

UPDATE versions
SET download_count = COALESCE(downloads, 0);

ALTER TABLE versions
DROP COLUMN downloads;

ALTER TABLE extensions
DROP COLUMN downloads;
143 changes: 85 additions & 58 deletions registry/sqlx-data.json
Original file line number Diff line number Diff line change
Expand Up @@ -523,29 +523,24 @@
"ordinal": 3,
"type_info": "Timestamptz"
},
{
"name": "downloads",
"ordinal": 4,
"type_info": "Int4"
},
{
"name": "description",
"ordinal": 5,
"ordinal": 4,
"type_info": "Varchar"
},
{
"name": "homepage",
"ordinal": 6,
"ordinal": 5,
"type_info": "Varchar"
},
{
"name": "documentation",
"ordinal": 7,
"ordinal": 6,
"type_info": "Varchar"
},
{
"name": "repository",
"ordinal": 8,
"ordinal": 7,
"type_info": "Varchar"
}
],
Expand All @@ -557,7 +552,6 @@
true,
true,
true,
true,
true
],
"parameters": {
Expand Down Expand Up @@ -683,60 +677,60 @@
"ordinal": 4,
"type_info": "Timestamptz"
},
{
"name": "downloads",
"ordinal": 5,
"type_info": "Int4"
},
{
"name": "features",
"ordinal": 6,
"ordinal": 5,
"type_info": "Jsonb"
},
{
"name": "yanked",
"ordinal": 7,
"ordinal": 6,
"type_info": "Bool"
},
{
"name": "license",
"ordinal": 8,
"ordinal": 7,
"type_info": "Varchar"
},
{
"name": "extension_size",
"ordinal": 9,
"ordinal": 8,
"type_info": "Int4"
},
{
"name": "published_by",
"ordinal": 10,
"ordinal": 9,
"type_info": "Varchar"
},
{
"name": "checksum",
"ordinal": 11,
"ordinal": 10,
"type_info": "Bpchar"
},
{
"name": "links",
"ordinal": 12,
"ordinal": 11,
"type_info": "Varchar"
},
{
"name": "extension_name",
"ordinal": 13,
"ordinal": 12,
"type_info": "Varchar"
},
{
"name": "system_dependencies",
"ordinal": 14,
"ordinal": 13,
"type_info": "Jsonb"
},
{
"name": "libraries",
"ordinal": 15,
"ordinal": 14,
"type_info": "Jsonb"
},
{
"name": "download_count",
"ordinal": 15,
"type_info": "Int4"
}
],
"nullable": [
Expand All @@ -755,7 +749,7 @@
true,
true,
true,
true
false
],
"parameters": {
"Left": [
Expand Down Expand Up @@ -794,60 +788,60 @@
"ordinal": 4,
"type_info": "Timestamptz"
},
{
"name": "downloads",
"ordinal": 5,
"type_info": "Int4"
},
{
"name": "features",
"ordinal": 6,
"ordinal": 5,
"type_info": "Jsonb"
},
{
"name": "yanked",
"ordinal": 7,
"ordinal": 6,
"type_info": "Bool"
},
{
"name": "license",
"ordinal": 8,
"ordinal": 7,
"type_info": "Varchar"
},
{
"name": "extension_size",
"ordinal": 9,
"ordinal": 8,
"type_info": "Int4"
},
{
"name": "published_by",
"ordinal": 10,
"ordinal": 9,
"type_info": "Varchar"
},
{
"name": "checksum",
"ordinal": 11,
"ordinal": 10,
"type_info": "Bpchar"
},
{
"name": "links",
"ordinal": 12,
"ordinal": 11,
"type_info": "Varchar"
},
{
"name": "extension_name",
"ordinal": 13,
"ordinal": 12,
"type_info": "Varchar"
},
{
"name": "system_dependencies",
"ordinal": 14,
"ordinal": 13,
"type_info": "Jsonb"
},
{
"name": "libraries",
"ordinal": 15,
"ordinal": 14,
"type_info": "Jsonb"
},
{
"name": "download_count",
"ordinal": 15,
"type_info": "Int4"
}
],
"nullable": [
Expand All @@ -866,7 +860,7 @@
true,
true,
true,
true
false
],
"parameters": {
"Left": [
Expand Down Expand Up @@ -943,6 +937,27 @@
},
"query": "DELETE FROM extensions_categories\n WHERE extension_id = $1\n RETURNING category_id\n "
},
"9921932de47d84b6a79a6f5fb29b77301d8a3a2e9acefbc97612952da86e6887": {
"describe": {
"columns": [
{
"name": "version_exists",
"ordinal": 0,
"type_info": "Bool"
}
],
"nullable": [
null
],
"parameters": {
"Left": [
"Int4",
"Text"
]
}
},
"query": "SELECT COUNT(*) > 0 AS version_exists\n FROM versions\n WHERE extension_id = $1\n AND num = $2"
},
"a355d51ccf9b49a96bf62078f00fb982ba11a3026d8b9f120d948b6c5e40d317": {
"describe": {
"columns": [],
Expand Down Expand Up @@ -1009,60 +1024,60 @@
"ordinal": 4,
"type_info": "Timestamptz"
},
{
"name": "downloads",
"ordinal": 5,
"type_info": "Int4"
},
{
"name": "features",
"ordinal": 6,
"ordinal": 5,
"type_info": "Jsonb"
},
{
"name": "yanked",
"ordinal": 7,
"ordinal": 6,
"type_info": "Bool"
},
{
"name": "license",
"ordinal": 8,
"ordinal": 7,
"type_info": "Varchar"
},
{
"name": "extension_size",
"ordinal": 9,
"ordinal": 8,
"type_info": "Int4"
},
{
"name": "published_by",
"ordinal": 10,
"ordinal": 9,
"type_info": "Varchar"
},
{
"name": "checksum",
"ordinal": 11,
"ordinal": 10,
"type_info": "Bpchar"
},
{
"name": "links",
"ordinal": 12,
"ordinal": 11,
"type_info": "Varchar"
},
{
"name": "extension_name",
"ordinal": 13,
"ordinal": 12,
"type_info": "Varchar"
},
{
"name": "system_dependencies",
"ordinal": 14,
"ordinal": 13,
"type_info": "Jsonb"
},
{
"name": "libraries",
"ordinal": 15,
"ordinal": 14,
"type_info": "Jsonb"
},
{
"name": "download_count",
"ordinal": 15,
"type_info": "Int4"
}
],
"nullable": [
Expand All @@ -1081,7 +1096,7 @@
true,
true,
true,
true
false
],
"parameters": {
"Left": [
Expand Down Expand Up @@ -1244,5 +1259,17 @@
}
},
"query": "\n UPDATE categories\n SET extension_count = extension_count + 1\n WHERE id = $1\n "
},
"f77d6c607d830e7ea1d564c34ec42b998e0c2657f4cdb8a89dd211a29b4a7d65": {
"describe": {
"columns": [],
"nullable": [],
"parameters": {
"Left": [
"Int4"
]
}
},
"query": "UPDATE versions\n SET download_count = download_count + 1\n WHERE extension_id = $1"
}
}
22 changes: 20 additions & 2 deletions registry/src/download.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
//! Functionality for downloading extensions and maintaining download counts
use crate::errors::ExtensionRegistryError;
use actix_web::web;
use sqlx::{Pool, Postgres};

/// Find an extension's lastest version given it's extension_id
pub async fn latest_version(
extension_id: i32,
conn: web::Data<Pool<Postgres>>,
conn: &Pool<Postgres>,
) -> Result<String, ExtensionRegistryError> {
// Create a transaction on the database
let mut tx = conn.begin().await?;
Expand All @@ -15,3 +14,22 @@ pub async fn latest_version(
.await?;
Ok(latest.num.unwrap())
}

pub async fn check_version(
pool: &Pool<Postgres>,
extension_id: i32,
version: &str,
) -> Result<bool, ExtensionRegistryError> {
let record = sqlx::query!(
"SELECT COUNT(*) > 0 AS version_exists
FROM versions
WHERE extension_id = $1
AND num = $2",
extension_id,
version
)
.fetch_one(pool)
.await?;

Ok(record.version_exists.unwrap_or(false))
}
Loading

0 comments on commit dedfe1c

Please sign in to comment.