diff --git a/src/db/add_package.rs b/src/db/add_package.rs index 27e90fa45..6db81c1f7 100644 --- a/src/db/add_package.rs +++ b/src/db/add_package.rs @@ -14,7 +14,6 @@ use crate::{ }; use log::{debug, info}; use postgres::Client; -use regex::Regex; use serde_json::Value; use slug::slugify; @@ -53,14 +52,14 @@ pub(crate) fn add_package_into_database( dependencies, target_name, yanked, build_status, rustdoc_status, test_status, license, repository_url, homepage_url, description, description_long, readme, - authors, keywords, have_examples, downloads, files, + keywords, have_examples, downloads, files, doc_targets, is_library, doc_rustc_version, documentation_url, default_target, features, github_repo ) VALUES ( $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 + $19, $20, $21, $22, $23, $24, $25, $26 ) ON CONFLICT (crate_id, version) DO UPDATE SET release_time = $3, @@ -76,18 +75,17 @@ pub(crate) fn add_package_into_database( description = $13, description_long = $14, readme = $15, - authors = $16, - keywords = $17, - have_examples = $18, - downloads = $19, - files = $20, - doc_targets = $21, - is_library = $22, - doc_rustc_version = $23, - documentation_url = $24, - default_target = $25, - features = $26, - github_repo = $27 + keywords = $16, + have_examples = $17, + downloads = $18, + files = $19, + doc_targets = $20, + is_library = $21, + doc_rustc_version = $22, + documentation_url = $23, + default_target = $24, + features = $25, + github_repo = $26 RETURNING id", &[ &crate_id, @@ -105,7 +103,6 @@ pub(crate) fn add_package_into_database( &metadata_pkg.description, &rustdoc, &readme, - &serde_json::to_value(&metadata_pkg.authors)?, &serde_json::to_value(&metadata_pkg.keywords)?, &has_examples, ®istry_data.downloads, @@ -123,7 +120,6 @@ pub(crate) fn add_package_into_database( let release_id: i32 = rows[0].get(0); add_keywords_into_database(conn, &metadata_pkg, release_id)?; - add_authors_into_database(conn, &metadata_pkg, release_id)?; add_compression_into_database(conn, compression_algorithms.into_iter(), release_id)?; // Update the crates table with the new release @@ -344,52 +340,6 @@ fn add_keywords_into_database( Ok(()) } -/// Adds authors into database -fn add_authors_into_database( - conn: &mut Client, - pkg: &MetadataPackage, - release_id: i32, -) -> Result<()> { - let author_capture_re = Regex::new("^([^><]+)<*(.*?)>*$").unwrap(); - for author in &pkg.authors { - if let Some(author_captures) = author_capture_re.captures(&author[..]) { - let author = author_captures - .get(1) - .map(|m| m.as_str()) - .unwrap_or("") - .trim(); - let email = author_captures - .get(2) - .map(|m| m.as_str()) - .unwrap_or("") - .trim(); - let slug = slugify(&author); - - let author_id: i32 = { - let rows = conn.query("SELECT id FROM authors WHERE slug = $1", &[&slug])?; - if !rows.is_empty() { - rows[0].get(0) - } else { - conn.query( - "INSERT INTO authors (name, email, slug) VALUES ($1, $2, $3) - RETURNING id", - &[&author, &email, &slug], - )?[0] - .get(0) - } - }; - - // add relationship - let _ = conn.query( - "INSERT INTO author_rels (rid, aid) VALUES ($1, $2)", - &[&release_id, &author_id], - ); - } - } - - Ok(()) -} - pub fn update_crate_data_in_database( conn: &mut Client, name: &str, diff --git a/src/db/delete.rs b/src/db/delete.rs index be73ecfd4..6f7251975 100644 --- a/src/db/delete.rs +++ b/src/db/delete.rs @@ -50,7 +50,6 @@ fn get_id(conn: &mut Client, name: &str) -> Result { // metaprogramming! // WARNING: these must be hard-coded and NEVER user input. const METADATA: &[(&str, &str)] = &[ - ("author_rels", "rid"), ("keyword_rels", "rid"), ("builds", "rid"), ("compression_rels", "release"), @@ -119,6 +118,7 @@ fn delete_crate_from_database(conn: &mut Client, name: &str, crate_id: i32) -> R #[cfg(test)] mod tests { use super::*; + use crate::index::api::CrateOwner; use crate::test::{assert_success, wrapper}; use failure::Error; use postgres::Client; @@ -179,13 +179,12 @@ mod tests { #[test] fn test_delete_version() { wrapper(|env| { - fn authors(conn: &mut Client, crate_id: i32) -> Result, Error> { + fn owners(conn: &mut Client, crate_id: i32) -> Result, Error> { Ok(conn .query( - "SELECT name FROM authors - INNER JOIN author_rels ON authors.id = author_rels.aid - INNER JOIN releases ON author_rels.rid = releases.id - WHERE releases.crate_id = $1", + "SELECT name FROM owners + INNER JOIN owner_rels ON owners.id = owner_rels.oid + WHERE owner_rels.cid = $1", &[&crate_id], )? .into_iter() @@ -198,16 +197,14 @@ mod tests { .fake_release() .name("a") .version("1.0.0") - .author("malicious actor") - .create()?; - let v2 = env - .fake_release() - .name("a") - .version("2.0.0") - .author("Peter Rabbit") + .add_owner(CrateOwner { + login: "malicious actor".into(), + avatar: "https://example.org/malicious".into(), + name: "malicious actor".into(), + email: "malicious@example.org".into(), + }) .create()?; assert!(release_exists(&mut db.conn(), v1)?); - assert!(release_exists(&mut db.conn(), v2)?); let crate_id = db .conn() .query("SELECT crate_id FROM releases WHERE id = $1", &[&v1])? @@ -216,15 +213,32 @@ mod tests { .unwrap() .get(0); assert_eq!( - authors(&mut db.conn(), crate_id)?, - vec!["malicious actor".to_string(), "Peter Rabbit".to_string()] + owners(&mut db.conn(), crate_id)?, + vec!["malicious actor".to_string()] + ); + + let v2 = env + .fake_release() + .name("a") + .version("2.0.0") + .add_owner(CrateOwner { + login: "Peter Rabbit".into(), + avatar: "https://example.org/peter".into(), + name: "Peter Rabbit".into(), + email: "peter@example.org".into(), + }) + .create()?; + assert!(release_exists(&mut db.conn(), v2)?); + assert_eq!( + owners(&mut db.conn(), crate_id)?, + vec!["Peter Rabbit".to_string()] ); delete_version(&mut db.conn(), &*env.storage(), "a", "1.0.0")?; assert!(!release_exists(&mut db.conn(), v1)?); assert!(release_exists(&mut db.conn(), v2)?); assert_eq!( - authors(&mut db.conn(), crate_id)?, + owners(&mut db.conn(), crate_id)?, vec!["Peter Rabbit".to_string()] ); diff --git a/src/db/migrate.rs b/src/db/migrate.rs index 3ec965ab3..068ad51c5 100644 --- a/src/db/migrate.rs +++ b/src/db/migrate.rs @@ -612,12 +612,38 @@ pub fn migrate(version: Option, conn: &mut Client) -> CratesfyiResult<( CREATE INDEX releases_github_repo_idx ON releases (github_repo); CREATE INDEX github_repos_stars_idx ON github_repos(stars DESC); ", + // downgrade " DROP INDEX crates_latest_version_idx; DROP INDEX releases_github_repo_idx; DROP INDEX github_repos_stars_idx; ", ), + migration!( + context, + 27, + "delete the authors and author_rels", + // upgrade + " + DROP TABLE authors, author_rels; + ALTER TABLE releases DROP COLUMN authors; + ", + // downgrade + " + CREATE TABLE authors ( + id SERIAL PRIMARY KEY, + name VARCHAR(255), + email VARCHAR(255), + slug VARCHAR(255) UNIQUE NOT NULL + ); + CREATE TABLE author_rels ( + rid INT REFERENCES releases(id), + aid INT REFERENCES authors(id), + UNIQUE(rid, aid) + ); + ALTER TABLE releases ADD COLUMN authors JSON; + ", + ), ]; for migration in migrations { diff --git a/src/test/fakes.rs b/src/test/fakes.rs index addce5133..c6a7bbbdd 100644 --- a/src/test/fakes.rs +++ b/src/test/fakes.rs @@ -64,7 +64,6 @@ impl<'a> FakeRelease<'a> { targets: vec![Target::dummy_lib("fake_package".into(), None)], readme: None, keywords: vec!["fake".into(), "package".into()], - authors: vec!["Fake Person ".into()], features: [ ("default".into(), vec!["feature1".into(), "feature3".into()]), ("feature1".into(), Vec::new()), @@ -121,11 +120,6 @@ impl<'a> FakeRelease<'a> { self } - pub(crate) fn author(mut self, author: &str) -> Self { - self.package.authors = vec![author.into()]; - self - } - pub(crate) fn repo(mut self, repo: impl Into) -> Self { self.package.repository = Some(repo.into()); self diff --git a/src/utils/cargo_metadata.rs b/src/utils/cargo_metadata.rs index c3573666b..23f507d0e 100644 --- a/src/utils/cargo_metadata.rs +++ b/src/utils/cargo_metadata.rs @@ -58,7 +58,6 @@ pub(crate) struct Package { pub(crate) targets: Vec, pub(crate) readme: Option, pub(crate) keywords: Vec, - pub(crate) authors: Vec, pub(crate) features: HashMap>, } diff --git a/src/web/crate_details.rs b/src/web/crate_details.rs index 573219a54..fbb941236 100644 --- a/src/web/crate_details.rs +++ b/src/web/crate_details.rs @@ -15,9 +15,7 @@ pub struct CrateDetails { name: String, version: String, description: Option, - authors: Vec<(String, String)>, owners: Vec<(String, String)>, - authors_json: Option, dependencies: Option, #[serde(serialize_with = "optional_markdown")] readme: Option, @@ -84,7 +82,6 @@ impl CrateDetails { crates.name, releases.version, releases.description, - releases.authors, releases.dependencies, releases.readme, releases.description_long, @@ -162,9 +159,7 @@ impl CrateDetails { name: krate.get("name"), version: krate.get("version"), description: krate.get("description"), - authors: Vec::new(), owners: Vec::new(), - authors_json: krate.get("authors"), dependencies: krate.get("dependencies"), readme: krate.get("readme"), rustdoc: krate.get("description_long"), @@ -191,22 +186,6 @@ impl CrateDetails { release_id, }; - // get authors - let authors = conn - .query( - "SELECT name, slug - FROM authors - INNER JOIN author_rels ON author_rels.aid = authors.id - WHERE rid = $1", - &[&release_id], - ) - .unwrap(); - - crate_details.authors = authors - .into_iter() - .map(|row| (row.get("name"), row.get("slug"))) - .collect(); - // get owners let owners = conn .query(