Skip to content

Commit 7e2da7d

Browse files
committed
database: Move versions_published_by::insert() call outside of NewVersion::save() fn
This prepares for the "Trusted Publishing" implementation, where there is no associated user account for a release and thus no email address either. Moving the call outside of the transaction is fine since the only production code call is already happening in an outer transaction.
1 parent 6773e19 commit 7e2da7d

File tree

4 files changed

+16
-11
lines changed

4 files changed

+16
-11
lines changed

crates/crates_io_database/src/models/version.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use diesel_async::scoped_futures::ScopedFutureExt;
88
use diesel_async::{AsyncConnection, AsyncPgConnection, RunQueryDsl};
99
use serde::Deserialize;
1010

11-
use crate::models::{Crate, User, versions_published_by};
11+
use crate::models::{Crate, User};
1212
use crate::schema::{readme_renderings, users, versions};
1313

1414
// Queryable has a custom implementation below
@@ -107,11 +107,7 @@ pub struct NewVersion<'a> {
107107
}
108108

109109
impl NewVersion<'_> {
110-
pub async fn save(
111-
&self,
112-
conn: &mut AsyncPgConnection,
113-
published_by_email: &str,
114-
) -> QueryResult<Version> {
110+
pub async fn save(&self, conn: &mut AsyncPgConnection) -> QueryResult<Version> {
115111
use diesel::insert_into;
116112

117113
conn.transaction(|conn| {
@@ -122,8 +118,6 @@ impl NewVersion<'_> {
122118
.get_result(conn)
123119
.await?;
124120

125-
versions_published_by::insert(version.id, published_by_email, conn).await?;
126-
127121
Ok(version)
128122
}
129123
.scope_boxed()

src/controllers/krate/publish.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ use crate::util::errors::{AppResult, BoxedAppError, bad_request, custom, interna
4242
use crate::views::{
4343
EncodableCrate, EncodableCrateDependency, GoodCrate, PublishMetadata, PublishWarnings,
4444
};
45+
use crates_io_database::models::versions_published_by;
4546
use crates_io_diesel_helpers::canon_crate_name;
4647

4748
const MISSING_RIGHTS_ERROR_MESSAGE: &str = "this crate exists but you don't seem to be an owner. \
@@ -432,7 +433,7 @@ pub async fn publish(app: AppState, req: Parts, body: Body) -> AppResult<Json<Go
432433
.keywords(&keywords)
433434
.build();
434435

435-
let version = new_version.save(conn, &verified_email_address).await.map_err(|error| {
436+
let version = new_version.save(conn).await.map_err(|error| {
436437
use diesel::result::{Error, DatabaseErrorKind};
437438
match error {
438439
Error::DatabaseError(DatabaseErrorKind::UniqueViolation, _) =>
@@ -441,6 +442,8 @@ pub async fn publish(app: AppState, req: Parts, body: Body) -> AppResult<Json<Go
441442
}
442443
})?;
443444

445+
versions_published_by::insert(version.id, &verified_email_address, conn).await?;
446+
444447
NewVersionOwnerAction::builder()
445448
.version_id(version.id)
446449
.user_id(user.id)

src/tests/builders/version.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use crate::schema::dependencies;
33
use std::collections::BTreeMap;
44

55
use chrono::{DateTime, Utc};
6+
use crates_io_database::models::versions_published_by;
67
use diesel::prelude::*;
78
use diesel_async::{AsyncPgConnection, RunQueryDsl};
89

@@ -110,7 +111,8 @@ impl VersionBuilder {
110111
.maybe_created_at(self.created_at.as_ref())
111112
.build();
112113

113-
let vers = new_version.save(connection, "[email protected]").await?;
114+
let vers = new_version.save(connection).await?;
115+
versions_published_by::insert(vers.id, "[email protected]", connection).await?;
114116

115117
let new_deps = self
116118
.dependencies

src/worker/jobs/downloads/update_metadata.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ mod tests {
107107
use super::*;
108108
use crate::models::{Crate, NewCrate, NewUser, NewVersion, User, Version};
109109
use crate::schema::{crate_downloads, crates, versions};
110+
use crates_io_database::models::versions_published_by;
110111
use crates_io_test_db::TestDatabase;
111112
use diesel::sql_types::Timestamptz;
112113
use diesel_async::AsyncConnection;
@@ -136,7 +137,12 @@ mod tests {
136137
.checksum("0000000000000000000000000000000000000000000000000000000000000000")
137138
.build();
138139

139-
let version = version.save(conn, "[email protected]").await.unwrap();
140+
let version = version.save(conn).await.unwrap();
141+
142+
versions_published_by::insert(version.id, "[email protected]", conn)
143+
.await
144+
.unwrap();
145+
140146
(krate, version)
141147
}
142148

0 commit comments

Comments
 (0)