Skip to content

Commit e6a7ce4

Browse files
arlosiTurbo87
authored andcommitted
database: Add checksum field
This can eventually be used to generate the index from the database.
1 parent 246a24c commit e6a7ce4

File tree

9 files changed

+26
-4
lines changed

9 files changed

+26
-4
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
ALTER TABLE versions
2+
DROP COLUMN checksum;
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
ALTER TABLE versions
2+
ADD COLUMN checksum CHAR(64) NULL;

src/controllers/krate/publish.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,11 @@ pub fn publish(req: &mut dyn RequestExt) -> EndpointResult {
154154
// This is only redundant for now. Eventually the duplication will be removed.
155155
let license = new_crate.license.clone();
156156

157+
// Read tarball from request
158+
let mut tarball = Vec::new();
159+
LimitErrorReader::new(req.body(), maximums.max_upload_size).read_to_end(&mut tarball)?;
160+
let hex_cksum: String = Sha256::digest(&tarball).encode_hex();
161+
157162
// Persist the new version of this crate
158163
let version = NewVersion::new(
159164
krate.id,
@@ -165,6 +170,7 @@ pub fn publish(req: &mut dyn RequestExt) -> EndpointResult {
165170
// to get here, and max upload sizes are way less than i32 max
166171
file_length as i32,
167172
user.id,
173+
hex_cksum.clone(),
168174
)?
169175
.save(&conn, &verified_email_address)?;
170176

@@ -191,10 +197,6 @@ pub fn publish(req: &mut dyn RequestExt) -> EndpointResult {
191197
let ignored_invalid_badges = Badge::update_crate(&conn, &krate, new_crate.badges.as_ref())?;
192198
let top_versions = krate.top_versions(&conn)?;
193199

194-
// Read tarball from request
195-
let mut tarball = Vec::new();
196-
LimitErrorReader::new(req.body(), maximums.max_upload_size).read_to_end(&mut tarball)?;
197-
let hex_cksum: String = Sha256::digest(&tarball).encode_hex();
198200
let pkg_name = format!("{}-{}", krate.name, vers);
199201
let cargo_vcs_info = verify_tarball(&pkg_name, &tarball, maximums.max_unpack_size)?;
200202
let pkg_path_in_vcs = cargo_vcs_info.map(|info| info.path_in_vcs);

src/downloads_counter.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,7 @@ mod tests {
457457
None,
458458
0,
459459
self.user.id,
460+
"0000000000000000000000000000000000000000000000000000000000000000".to_string(),
460461
)
461462
.expect("failed to create version")
462463
.save(conn, "[email protected]")

src/models/version.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ pub struct Version {
2323
pub license: Option<String>,
2424
pub crate_size: Option<i32>,
2525
pub published_by: Option<i32>,
26+
pub checksum: Option<String>,
2627
}
2728

2829
#[derive(Insertable, Debug)]
@@ -34,6 +35,7 @@ pub struct NewVersion {
3435
license: Option<String>,
3536
crate_size: Option<i32>,
3637
published_by: i32,
38+
checksum: String,
3739
}
3840

3941
/// The highest version (semver order) and the most recently updated version.
@@ -129,6 +131,7 @@ impl NewVersion {
129131
license_file: Option<&str>,
130132
crate_size: i32,
131133
published_by: i32,
134+
checksum: String,
132135
) -> AppResult<Self> {
133136
let features = serde_json::to_value(features)?;
134137

@@ -139,6 +142,7 @@ impl NewVersion {
139142
license,
140143
crate_size: Some(crate_size),
141144
published_by,
145+
checksum,
142146
};
143147

144148
new_version.validate_license(license_file)?;

src/schema.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -982,6 +982,12 @@ table! {
982982
///
983983
/// (Automatically generated by Diesel.)
984984
published_by -> Nullable<Int4>,
985+
/// The `checksum` column of the `versions` table.
986+
///
987+
/// Its SQL type is `Nullable<Bpchar>`.
988+
///
989+
/// (Automatically generated by Diesel.)
990+
checksum -> Nullable<Bpchar>,
985991
}
986992
}
987993

src/tests/builders/version.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ pub struct VersionBuilder<'a> {
1818
num: semver::Version,
1919
size: i32,
2020
yanked: bool,
21+
checksum: String,
2122
}
2223

2324
impl<'a> VersionBuilder<'a> {
@@ -41,6 +42,7 @@ impl<'a> VersionBuilder<'a> {
4142
num,
4243
size: 0,
4344
yanked: false,
45+
checksum: String::new(),
4446
}
4547
}
4648

@@ -91,6 +93,7 @@ impl<'a> VersionBuilder<'a> {
9193
self.license_file,
9294
self.size,
9395
published_by,
96+
self.checksum,
9497
)?
9598
.save(connection, "[email protected]")?;
9699

src/worker/dump_db/dump-db.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ yanked = "public"
212212
license = "public"
213213
crate_size = "public"
214214
published_by = "public"
215+
checksum = "public"
215216

216217
[versions_published_by.columns]
217218
version_id = "private"

src/worker/update_downloads.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ mod test {
108108
None,
109109
0,
110110
user_id,
111+
"0000000000000000000000000000000000000000000000000000000000000000".to_string(),
111112
)
112113
.unwrap();
113114
let version = version.save(conn, "[email protected]").unwrap();

0 commit comments

Comments
 (0)