Skip to content

delete the authors and author_rels #1341

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

Merged
merged 1 commit into from
Apr 7, 2021
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
76 changes: 13 additions & 63 deletions src/db/add_package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ use crate::{
};
use log::{debug, info};
use postgres::Client;
use regex::Regex;
use serde_json::Value;
use slug::slugify;

Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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,
&registry_data.downloads,
Expand All @@ -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
Expand Down Expand Up @@ -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,
Expand Down
48 changes: 31 additions & 17 deletions src/db/delete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ fn get_id(conn: &mut Client, name: &str) -> Result<i32, Error> {
// 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"),
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -179,13 +179,12 @@ mod tests {
#[test]
fn test_delete_version() {
wrapper(|env| {
fn authors(conn: &mut Client, crate_id: i32) -> Result<Vec<String>, Error> {
fn owners(conn: &mut Client, crate_id: i32) -> Result<Vec<String>, 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()
Expand All @@ -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: "[email protected]".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])?
Expand All @@ -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: "[email protected]".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()]
);

Expand Down
26 changes: 26 additions & 0 deletions src/db/migrate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -612,12 +612,38 @@ pub fn migrate(version: Option<Version>, 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 {
Expand Down
6 changes: 0 additions & 6 deletions src/test/fakes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 <[email protected]>".into()],
features: [
("default".into(), vec!["feature1".into(), "feature3".into()]),
("feature1".into(), Vec::new()),
Expand Down Expand Up @@ -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<String>) -> Self {
self.package.repository = Some(repo.into());
self
Expand Down
1 change: 0 additions & 1 deletion src/utils/cargo_metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ pub(crate) struct Package {
pub(crate) targets: Vec<Target>,
pub(crate) readme: Option<String>,
pub(crate) keywords: Vec<String>,
pub(crate) authors: Vec<String>,
pub(crate) features: HashMap<String, Vec<String>>,
}

Expand Down
21 changes: 0 additions & 21 deletions src/web/crate_details.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ pub struct CrateDetails {
name: String,
version: String,
description: Option<String>,
authors: Vec<(String, String)>,
owners: Vec<(String, String)>,
authors_json: Option<Value>,
dependencies: Option<Value>,
#[serde(serialize_with = "optional_markdown")]
readme: Option<String>,
Expand Down Expand Up @@ -84,7 +82,6 @@ impl CrateDetails {
crates.name,
releases.version,
releases.description,
releases.authors,
releases.dependencies,
releases.readme,
releases.description_long,
Expand Down Expand Up @@ -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"),
Expand All @@ -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(
Expand Down