Skip to content

Commit 9ac7ef0

Browse files
authored
Merge pull request #1341 from hi-rustin/rustin-authors
delete the authors and author_rels
2 parents 47ff0a3 + c1cd637 commit 9ac7ef0

File tree

6 files changed

+70
-108
lines changed

6 files changed

+70
-108
lines changed

src/db/add_package.rs

+13-63
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ use crate::{
1414
};
1515
use log::{debug, info};
1616
use postgres::Client;
17-
use regex::Regex;
1817
use serde_json::Value;
1918
use slug::slugify;
2019

@@ -53,14 +52,14 @@ pub(crate) fn add_package_into_database(
5352
dependencies, target_name, yanked, build_status,
5453
rustdoc_status, test_status, license, repository_url,
5554
homepage_url, description, description_long, readme,
56-
authors, keywords, have_examples, downloads, files,
55+
keywords, have_examples, downloads, files,
5756
doc_targets, is_library, doc_rustc_version,
5857
documentation_url, default_target, features, github_repo
5958
)
6059
VALUES (
6160
$1, $2, $3, $4, $5, $6, $7, $8, $9,
6261
$10, $11, $12, $13, $14, $15, $16, $17, $18,
63-
$19, $20, $21, $22, $23, $24, $25, $26, $27
62+
$19, $20, $21, $22, $23, $24, $25, $26
6463
)
6564
ON CONFLICT (crate_id, version) DO UPDATE
6665
SET release_time = $3,
@@ -76,18 +75,17 @@ pub(crate) fn add_package_into_database(
7675
description = $13,
7776
description_long = $14,
7877
readme = $15,
79-
authors = $16,
80-
keywords = $17,
81-
have_examples = $18,
82-
downloads = $19,
83-
files = $20,
84-
doc_targets = $21,
85-
is_library = $22,
86-
doc_rustc_version = $23,
87-
documentation_url = $24,
88-
default_target = $25,
89-
features = $26,
90-
github_repo = $27
78+
keywords = $16,
79+
have_examples = $17,
80+
downloads = $18,
81+
files = $19,
82+
doc_targets = $20,
83+
is_library = $21,
84+
doc_rustc_version = $22,
85+
documentation_url = $23,
86+
default_target = $24,
87+
features = $25,
88+
github_repo = $26
9189
RETURNING id",
9290
&[
9391
&crate_id,
@@ -105,7 +103,6 @@ pub(crate) fn add_package_into_database(
105103
&metadata_pkg.description,
106104
&rustdoc,
107105
&readme,
108-
&serde_json::to_value(&metadata_pkg.authors)?,
109106
&serde_json::to_value(&metadata_pkg.keywords)?,
110107
&has_examples,
111108
&registry_data.downloads,
@@ -123,7 +120,6 @@ pub(crate) fn add_package_into_database(
123120
let release_id: i32 = rows[0].get(0);
124121

125122
add_keywords_into_database(conn, &metadata_pkg, release_id)?;
126-
add_authors_into_database(conn, &metadata_pkg, release_id)?;
127123
add_compression_into_database(conn, compression_algorithms.into_iter(), release_id)?;
128124

129125
// Update the crates table with the new release
@@ -344,52 +340,6 @@ fn add_keywords_into_database(
344340
Ok(())
345341
}
346342

347-
/// Adds authors into database
348-
fn add_authors_into_database(
349-
conn: &mut Client,
350-
pkg: &MetadataPackage,
351-
release_id: i32,
352-
) -> Result<()> {
353-
let author_capture_re = Regex::new("^([^><]+)<*(.*?)>*$").unwrap();
354-
for author in &pkg.authors {
355-
if let Some(author_captures) = author_capture_re.captures(&author[..]) {
356-
let author = author_captures
357-
.get(1)
358-
.map(|m| m.as_str())
359-
.unwrap_or("")
360-
.trim();
361-
let email = author_captures
362-
.get(2)
363-
.map(|m| m.as_str())
364-
.unwrap_or("")
365-
.trim();
366-
let slug = slugify(&author);
367-
368-
let author_id: i32 = {
369-
let rows = conn.query("SELECT id FROM authors WHERE slug = $1", &[&slug])?;
370-
if !rows.is_empty() {
371-
rows[0].get(0)
372-
} else {
373-
conn.query(
374-
"INSERT INTO authors (name, email, slug) VALUES ($1, $2, $3)
375-
RETURNING id",
376-
&[&author, &email, &slug],
377-
)?[0]
378-
.get(0)
379-
}
380-
};
381-
382-
// add relationship
383-
let _ = conn.query(
384-
"INSERT INTO author_rels (rid, aid) VALUES ($1, $2)",
385-
&[&release_id, &author_id],
386-
);
387-
}
388-
}
389-
390-
Ok(())
391-
}
392-
393343
pub fn update_crate_data_in_database(
394344
conn: &mut Client,
395345
name: &str,

src/db/delete.rs

+31-17
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ fn get_id(conn: &mut Client, name: &str) -> Result<i32, Error> {
5050
// metaprogramming!
5151
// WARNING: these must be hard-coded and NEVER user input.
5252
const METADATA: &[(&str, &str)] = &[
53-
("author_rels", "rid"),
5453
("keyword_rels", "rid"),
5554
("builds", "rid"),
5655
("compression_rels", "release"),
@@ -119,6 +118,7 @@ fn delete_crate_from_database(conn: &mut Client, name: &str, crate_id: i32) -> R
119118
#[cfg(test)]
120119
mod tests {
121120
use super::*;
121+
use crate::index::api::CrateOwner;
122122
use crate::test::{assert_success, wrapper};
123123
use failure::Error;
124124
use postgres::Client;
@@ -179,13 +179,12 @@ mod tests {
179179
#[test]
180180
fn test_delete_version() {
181181
wrapper(|env| {
182-
fn authors(conn: &mut Client, crate_id: i32) -> Result<Vec<String>, Error> {
182+
fn owners(conn: &mut Client, crate_id: i32) -> Result<Vec<String>, Error> {
183183
Ok(conn
184184
.query(
185-
"SELECT name FROM authors
186-
INNER JOIN author_rels ON authors.id = author_rels.aid
187-
INNER JOIN releases ON author_rels.rid = releases.id
188-
WHERE releases.crate_id = $1",
185+
"SELECT name FROM owners
186+
INNER JOIN owner_rels ON owners.id = owner_rels.oid
187+
WHERE owner_rels.cid = $1",
189188
&[&crate_id],
190189
)?
191190
.into_iter()
@@ -198,16 +197,14 @@ mod tests {
198197
.fake_release()
199198
.name("a")
200199
.version("1.0.0")
201-
.author("malicious actor")
202-
.create()?;
203-
let v2 = env
204-
.fake_release()
205-
.name("a")
206-
.version("2.0.0")
207-
.author("Peter Rabbit")
200+
.add_owner(CrateOwner {
201+
login: "malicious actor".into(),
202+
avatar: "https://example.org/malicious".into(),
203+
name: "malicious actor".into(),
204+
email: "[email protected]".into(),
205+
})
208206
.create()?;
209207
assert!(release_exists(&mut db.conn(), v1)?);
210-
assert!(release_exists(&mut db.conn(), v2)?);
211208
let crate_id = db
212209
.conn()
213210
.query("SELECT crate_id FROM releases WHERE id = $1", &[&v1])?
@@ -216,15 +213,32 @@ mod tests {
216213
.unwrap()
217214
.get(0);
218215
assert_eq!(
219-
authors(&mut db.conn(), crate_id)?,
220-
vec!["malicious actor".to_string(), "Peter Rabbit".to_string()]
216+
owners(&mut db.conn(), crate_id)?,
217+
vec!["malicious actor".to_string()]
218+
);
219+
220+
let v2 = env
221+
.fake_release()
222+
.name("a")
223+
.version("2.0.0")
224+
.add_owner(CrateOwner {
225+
login: "Peter Rabbit".into(),
226+
avatar: "https://example.org/peter".into(),
227+
name: "Peter Rabbit".into(),
228+
email: "[email protected]".into(),
229+
})
230+
.create()?;
231+
assert!(release_exists(&mut db.conn(), v2)?);
232+
assert_eq!(
233+
owners(&mut db.conn(), crate_id)?,
234+
vec!["Peter Rabbit".to_string()]
221235
);
222236

223237
delete_version(&mut db.conn(), &*env.storage(), "a", "1.0.0")?;
224238
assert!(!release_exists(&mut db.conn(), v1)?);
225239
assert!(release_exists(&mut db.conn(), v2)?);
226240
assert_eq!(
227-
authors(&mut db.conn(), crate_id)?,
241+
owners(&mut db.conn(), crate_id)?,
228242
vec!["Peter Rabbit".to_string()]
229243
);
230244

src/db/migrate.rs

+26
Original file line numberDiff line numberDiff line change
@@ -612,12 +612,38 @@ pub fn migrate(version: Option<Version>, conn: &mut Client) -> CratesfyiResult<(
612612
CREATE INDEX releases_github_repo_idx ON releases (github_repo);
613613
CREATE INDEX github_repos_stars_idx ON github_repos(stars DESC);
614614
",
615+
// downgrade
615616
"
616617
DROP INDEX crates_latest_version_idx;
617618
DROP INDEX releases_github_repo_idx;
618619
DROP INDEX github_repos_stars_idx;
619620
",
620621
),
622+
migration!(
623+
context,
624+
27,
625+
"delete the authors and author_rels",
626+
// upgrade
627+
"
628+
DROP TABLE authors, author_rels;
629+
ALTER TABLE releases DROP COLUMN authors;
630+
",
631+
// downgrade
632+
"
633+
CREATE TABLE authors (
634+
id SERIAL PRIMARY KEY,
635+
name VARCHAR(255),
636+
email VARCHAR(255),
637+
slug VARCHAR(255) UNIQUE NOT NULL
638+
);
639+
CREATE TABLE author_rels (
640+
rid INT REFERENCES releases(id),
641+
aid INT REFERENCES authors(id),
642+
UNIQUE(rid, aid)
643+
);
644+
ALTER TABLE releases ADD COLUMN authors JSON;
645+
",
646+
),
621647
];
622648

623649
for migration in migrations {

src/test/fakes.rs

-6
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ impl<'a> FakeRelease<'a> {
6464
targets: vec![Target::dummy_lib("fake_package".into(), None)],
6565
readme: None,
6666
keywords: vec!["fake".into(), "package".into()],
67-
authors: vec!["Fake Person <[email protected]>".into()],
6867
features: [
6968
("default".into(), vec!["feature1".into(), "feature3".into()]),
7069
("feature1".into(), Vec::new()),
@@ -121,11 +120,6 @@ impl<'a> FakeRelease<'a> {
121120
self
122121
}
123122

124-
pub(crate) fn author(mut self, author: &str) -> Self {
125-
self.package.authors = vec![author.into()];
126-
self
127-
}
128-
129123
pub(crate) fn repo(mut self, repo: impl Into<String>) -> Self {
130124
self.package.repository = Some(repo.into());
131125
self

src/utils/cargo_metadata.rs

-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ pub(crate) struct Package {
5858
pub(crate) targets: Vec<Target>,
5959
pub(crate) readme: Option<String>,
6060
pub(crate) keywords: Vec<String>,
61-
pub(crate) authors: Vec<String>,
6261
pub(crate) features: HashMap<String, Vec<String>>,
6362
}
6463

src/web/crate_details.rs

-21
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@ pub struct CrateDetails {
1515
name: String,
1616
version: String,
1717
description: Option<String>,
18-
authors: Vec<(String, String)>,
1918
owners: Vec<(String, String)>,
20-
authors_json: Option<Value>,
2119
dependencies: Option<Value>,
2220
#[serde(serialize_with = "optional_markdown")]
2321
readme: Option<String>,
@@ -84,7 +82,6 @@ impl CrateDetails {
8482
crates.name,
8583
releases.version,
8684
releases.description,
87-
releases.authors,
8885
releases.dependencies,
8986
releases.readme,
9087
releases.description_long,
@@ -162,9 +159,7 @@ impl CrateDetails {
162159
name: krate.get("name"),
163160
version: krate.get("version"),
164161
description: krate.get("description"),
165-
authors: Vec::new(),
166162
owners: Vec::new(),
167-
authors_json: krate.get("authors"),
168163
dependencies: krate.get("dependencies"),
169164
readme: krate.get("readme"),
170165
rustdoc: krate.get("description_long"),
@@ -191,22 +186,6 @@ impl CrateDetails {
191186
release_id,
192187
};
193188

194-
// get authors
195-
let authors = conn
196-
.query(
197-
"SELECT name, slug
198-
FROM authors
199-
INNER JOIN author_rels ON author_rels.aid = authors.id
200-
WHERE rid = $1",
201-
&[&release_id],
202-
)
203-
.unwrap();
204-
205-
crate_details.authors = authors
206-
.into_iter()
207-
.map(|row| (row.get("name"), row.get("slug")))
208-
.collect();
209-
210189
// get owners
211190
let owners = conn
212191
.query(

0 commit comments

Comments
 (0)