Skip to content

Commit a018f86

Browse files
committed
Generate crate index files from the database
Uses the newly added columns and index_metadata function to re-generate the entire crate index file from the database rather than a single line when a crate is published or yanked. Test data changes are due to the test framework inserting crates into the database, but not the index.
1 parent 70d9a59 commit a018f86

File tree

6 files changed

+46
-62
lines changed

6 files changed

+46
-62
lines changed

src/tests/builders/version.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ impl<'a> VersionBuilder<'a> {
4343
num,
4444
size: 0,
4545
yanked: false,
46-
checksum: String::new(),
46+
checksum: "0000000000000000000000000000000000000000000000000000000000000000"
47+
.to_string(),
4748
links: None,
4849
}
4950
}

src/tests/http-data/krate_publish_new_krate_too_big_but_whitelisted

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,14 @@
4444
],
4545
[
4646
"content-length",
47-
"167"
47+
"335"
4848
],
4949
[
5050
"content-type",
5151
"text/plain"
5252
]
5353
],
54-
"body": "eyJuYW1lIjoiZm9vX3doaXRlbGlzdCIsInZlcnMiOiIxLjEuMCIsImRlcHMiOltdLCJja3N1bSI6IjRlMzNkYzU5YmJiYzk2NjQ1ZmMwMTk0NWZiNTAyNTA3ZDFiN2JkM2EyZDA2MjI3YmY3YjBmZTg4NDJmMjg0YzIiLCJmZWF0dXJlcyI6e30sInlhbmtlZCI6ZmFsc2UsImxpbmtzIjpudWxsfQo="
54+
"body": "eyJuYW1lIjoiZm9vX3doaXRlbGlzdCIsInZlcnMiOiIwLjk5LjAiLCJkZXBzIjpbXSwiY2tzdW0iOiIwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwIiwiZmVhdHVyZXMiOnt9LCJ5YW5rZWQiOmZhbHNlLCJsaW5rcyI6bnVsbH0KeyJuYW1lIjoiZm9vX3doaXRlbGlzdCIsInZlcnMiOiIxLjEuMCIsImRlcHMiOltdLCJja3N1bSI6IjRlMzNkYzU5YmJiYzk2NjQ1ZmMwMTk0NWZiNTAyNTA3ZDFiN2JkM2EyZDA2MjI3YmY3YjBmZTg4NDJmMjg0YzIiLCJmZWF0dXJlcyI6e30sInlhbmtlZCI6ZmFsc2UsImxpbmtzIjpudWxsfQo="
5555
},
5656
"response": {
5757
"status": 200,

src/tests/http-data/krate_publish_new_krate_twice

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,14 @@
4444
],
4545
[
4646
"content-length",
47-
"163"
47+
"327"
4848
],
4949
[
5050
"content-type",
5151
"text/plain"
5252
]
5353
],
54-
"body": "eyJuYW1lIjoiZm9vX3R3aWNlIiwidmVycyI6IjIuMC4wIiwiZGVwcyI6W10sImNrc3VtIjoiYWNiNTYwNGIxMjZhYzg5NGMxZWIxMWM0NTc1YmYyMDcyZmVhNjEyMzJhODg4ZTQ1Mzc3MGM3OWQ3ZWQ1NjQxOSIsImZlYXR1cmVzIjp7fSwieWFua2VkIjpmYWxzZSwibGlua3MiOm51bGx9Cg=="
54+
"body": "eyJuYW1lIjoiZm9vX3R3aWNlIiwidmVycyI6IjAuOTkuMCIsImRlcHMiOltdLCJja3N1bSI6IjAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAiLCJmZWF0dXJlcyI6e30sInlhbmtlZCI6ZmFsc2UsImxpbmtzIjpudWxsfQp7Im5hbWUiOiJmb29fdHdpY2UiLCJ2ZXJzIjoiMi4wLjAiLCJkZXBzIjpbXSwiY2tzdW0iOiJhY2I1NjA0YjEyNmFjODk0YzFlYjExYzQ1NzViZjIwNzJmZWE2MTIzMmE4ODhlNDUzNzcwYzc5ZDdlZDU2NDE5IiwiZmVhdHVyZXMiOnt9LCJ5YW5rZWQiOmZhbHNlLCJsaW5rcyI6bnVsbH0K"
5555
},
5656
"response": {
5757
"status": 200,

src/tests/http-data/krate_publish_publish_after_removing_documentation

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,14 @@
4444
],
4545
[
4646
"content-length",
47-
"163"
47+
"326"
4848
],
4949
[
5050
"content-type",
5151
"text/plain"
5252
]
5353
],
54-
"body": "eyJuYW1lIjoiZG9jc2NyYXRlIiwidmVycyI6IjAuMi4xIiwiZGVwcyI6W10sImNrc3VtIjoiYWNiNTYwNGIxMjZhYzg5NGMxZWIxMWM0NTc1YmYyMDcyZmVhNjEyMzJhODg4ZTQ1Mzc3MGM3OWQ3ZWQ1NjQxOSIsImZlYXR1cmVzIjp7fSwieWFua2VkIjpmYWxzZSwibGlua3MiOm51bGx9Cg=="
54+
"body": "eyJuYW1lIjoiZG9jc2NyYXRlIiwidmVycyI6IjAuMi4wIiwiZGVwcyI6W10sImNrc3VtIjoiMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMCIsImZlYXR1cmVzIjp7fSwieWFua2VkIjpmYWxzZSwibGlua3MiOm51bGx9CnsibmFtZSI6ImRvY3NjcmF0ZSIsInZlcnMiOiIwLjIuMSIsImRlcHMiOltdLCJja3N1bSI6ImFjYjU2MDRiMTI2YWM4OTRjMWViMTFjNDU3NWJmMjA3MmZlYTYxMjMyYTg4OGU0NTM3NzBjNzlkN2VkNTY0MTkiLCJmZWF0dXJlcyI6e30sInlhbmtlZCI6ZmFsc2UsImxpbmtzIjpudWxsfQo="
5555
},
5656
"response": {
5757
"status": 200,
@@ -104,14 +104,14 @@
104104
],
105105
[
106106
"content-length",
107-
"326"
107+
"489"
108108
],
109109
[
110110
"content-type",
111111
"text/plain"
112112
]
113113
],
114-
"body": "eyJuYW1lIjoiZG9jc2NyYXRlIiwidmVycyI6IjAuMi4xIiwiZGVwcyI6W10sImNrc3VtIjoiYWNiNTYwNGIxMjZhYzg5NGMxZWIxMWM0NTc1YmYyMDcyZmVhNjEyMzJhODg4ZTQ1Mzc3MGM3OWQ3ZWQ1NjQxOSIsImZlYXR1cmVzIjp7fSwieWFua2VkIjpmYWxzZSwibGlua3MiOm51bGx9CnsibmFtZSI6ImRvY3NjcmF0ZSIsInZlcnMiOiIwLjIuMiIsImRlcHMiOltdLCJja3N1bSI6ImFjYjU2MDRiMTI2YWM4OTRjMWViMTFjNDU3NWJmMjA3MmZlYTYxMjMyYTg4OGU0NTM3NzBjNzlkN2VkNTY0MTkiLCJmZWF0dXJlcyI6e30sInlhbmtlZCI6ZmFsc2UsImxpbmtzIjpudWxsfQo="
114+
"body": "eyJuYW1lIjoiZG9jc2NyYXRlIiwidmVycyI6IjAuMi4wIiwiZGVwcyI6W10sImNrc3VtIjoiMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMCIsImZlYXR1cmVzIjp7fSwieWFua2VkIjpmYWxzZSwibGlua3MiOm51bGx9CnsibmFtZSI6ImRvY3NjcmF0ZSIsInZlcnMiOiIwLjIuMSIsImRlcHMiOltdLCJja3N1bSI6ImFjYjU2MDRiMTI2YWM4OTRjMWViMTFjNDU3NWJmMjA3MmZlYTYxMjMyYTg4OGU0NTM3NzBjNzlkN2VkNTY0MTkiLCJmZWF0dXJlcyI6e30sInlhbmtlZCI6ZmFsc2UsImxpbmtzIjpudWxsfQp7Im5hbWUiOiJkb2NzY3JhdGUiLCJ2ZXJzIjoiMC4yLjIiLCJkZXBzIjpbXSwiY2tzdW0iOiJhY2I1NjA0YjEyNmFjODk0YzFlYjExYzQ1NzViZjIwNzJmZWE2MTIzMmE4ODhlNDUzNzcwYzc5ZDdlZDU2NDE5IiwiZmVhdHVyZXMiOnt9LCJ5YW5rZWQiOmZhbHNlLCJsaW5rcyI6bnVsbH0K"
115115
},
116116
"response": {
117117
"status": 200,

src/tests/http-data/team_publish_owned

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,14 @@
4444
],
4545
[
4646
"content-length",
47-
"168"
47+
"337"
4848
],
4949
[
5050
"content-type",
5151
"text/plain"
5252
]
5353
],
54-
"body": "eyJuYW1lIjoiZm9vX3RlYW1fb3duZWQiLCJ2ZXJzIjoiMi4wLjAiLCJkZXBzIjpbXSwiY2tzdW0iOiJhY2I1NjA0YjEyNmFjODk0YzFlYjExYzQ1NzViZjIwNzJmZWE2MTIzMmE4ODhlNDUzNzcwYzc5ZDdlZDU2NDE5IiwiZmVhdHVyZXMiOnt9LCJ5YW5rZWQiOmZhbHNlLCJsaW5rcyI6bnVsbH0K"
54+
"body": "eyJuYW1lIjoiZm9vX3RlYW1fb3duZWQiLCJ2ZXJzIjoiMC45OS4wIiwiZGVwcyI6W10sImNrc3VtIjoiMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMCIsImZlYXR1cmVzIjp7fSwieWFua2VkIjpmYWxzZSwibGlua3MiOm51bGx9CnsibmFtZSI6ImZvb190ZWFtX293bmVkIiwidmVycyI6IjIuMC4wIiwiZGVwcyI6W10sImNrc3VtIjoiYWNiNTYwNGIxMjZhYzg5NGMxZWIxMWM0NTc1YmYyMDcyZmVhNjEyMzJhODg4ZTQ1Mzc3MGM3OWQ3ZWQ1NjQxOSIsImZlYXR1cmVzIjp7fSwieWFua2VkIjpmYWxzZSwibGlua3MiOm51bGx9Cg=="
5555
},
5656
"response": {
5757
"status": 200,

src/worker/git.rs

Lines changed: 34 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,18 @@
11
use crate::background_jobs::Environment;
2-
use crate::schema;
2+
use crate::{models, schema};
33
use anyhow::Context;
44
use cargo_registry_index::Crate;
55
use chrono::Utc;
66
use diesel::prelude::*;
7-
use std::fs::{self, OpenOptions};
7+
use std::fs::{self, File};
88
use std::io::ErrorKind;
99
use std::process::Command;
10-
use swirl::PerformError;
10+
use swirl::{Job, PerformError};
1111

1212
#[swirl::background_job]
1313
pub fn add_crate(env: &Environment, conn: &PgConnection, krate: Crate) -> Result<(), PerformError> {
14-
use std::io::prelude::*;
15-
16-
let repo = env.lock_index()?;
17-
let dst = repo.index_file(&krate.name);
18-
19-
// Add the crate to its relevant file
20-
fs::create_dir_all(dst.parent().unwrap())?;
21-
let mut file = OpenOptions::new().append(true).create(true).open(&dst)?;
22-
serde_json::to_writer(&mut file, &krate)?;
23-
file.write_all(b"\n")?;
24-
2514
let message: String = format!("Updating crate `{}#{}`", krate.name, krate.vers);
26-
repo.commit_and_push(&message, &dst)?;
27-
28-
// Queue another background job to update the http-based index as well.
29-
update_crate_index(krate.name.clone()).enqueue(conn)?;
30-
Ok(())
15+
sync(env, conn, &krate.name, &message)
3116
}
3217

3318
#[swirl::background_job]
@@ -47,10 +32,7 @@ pub fn update_crate_index(env: &Environment, crate_name: String) -> Result<(), P
4732
Ok(())
4833
}
4934

50-
/// Yanks or unyanks a crate version. This requires finding the index
51-
/// file, deserlialise the crate from JSON, change the yank boolean to
52-
/// `true` or `false`, write all the lines back out, and commit and
53-
/// push the changes.
35+
/// Yanks or unyanks a crate version.
5436
#[swirl::background_job]
5537
pub fn sync_yanked(
5638
env: &Environment,
@@ -70,38 +52,39 @@ pub fn sync_yanked(
7052

7153
trace!(?krate, ?version_num, yanked);
7254

55+
let action = if yanked { "Yanking" } else { "Unyanking" };
56+
let message = format!("{action} crate `{krate}#{version_num}`");
57+
sync(env, conn, &krate, &message)
58+
}
59+
60+
/// Re-generates an index file from the database for a single crate.
61+
pub fn sync(
62+
env: &Environment,
63+
conn: &PgConnection,
64+
krate: &str,
65+
commit_message: &str,
66+
) -> Result<(), PerformError> {
67+
use std::io::Write;
68+
trace!(?krate, "Update crate from database");
69+
let db_krate: models::Crate = models::Crate::by_exact_name(krate)
70+
.first(conn)
71+
.context("failed to find crate in database")?;
72+
let new = db_krate
73+
.index_metadata(conn)
74+
.context("failed to generate index metadata")?;
75+
7376
let repo = env.lock_index()?;
74-
let dst = repo.index_file(&krate);
75-
76-
let prev = fs::read_to_string(&dst)?;
77-
let new = prev
78-
.lines()
79-
.map(|line| {
80-
let mut git_crate = serde_json::from_str::<Crate>(line)
81-
.map_err(|_| format!("couldn't decode: `{line}`"))?;
82-
if git_crate.name != krate || git_crate.vers != version_num {
83-
return Ok(line.to_string());
84-
}
85-
git_crate.yanked = Some(yanked);
86-
Ok(serde_json::to_string(&git_crate)?)
87-
})
88-
.collect::<Result<Vec<_>, PerformError>>();
89-
let new = new?.join("\n") + "\n";
90-
91-
if new != prev {
92-
fs::write(&dst, new.as_bytes())?;
93-
94-
let action = if yanked { "Yanking" } else { "Unyanking" };
95-
let message = format!("{action} crate `{krate}#{version_num}`");
96-
97-
repo.commit_and_push(&message, &dst)?;
98-
} else {
99-
debug!("Skipping `yanked` update because index is up-to-date");
77+
let dst = repo.index_file(krate);
78+
if fs::read(&dst).ok().as_ref() == Some(&new) {
79+
debug!("Skipping `{krate}` update because index is up-to-date");
80+
return Ok(());
10081
}
82+
fs::create_dir_all(dst.parent().unwrap())?;
83+
File::create(&dst)?.write_all(&new)?;
84+
repo.commit_and_push(commit_message, &dst)?;
10185

10286
// Queue another background job to update the http-based index as well.
103-
update_crate_index(krate.clone()).enqueue(conn)?;
104-
87+
update_crate_index(krate.to_string()).enqueue(conn)?;
10588
Ok(())
10689
}
10790

0 commit comments

Comments
 (0)