Skip to content

Commit a54fba2

Browse files
committed
Used crate ID for SyncCrateFeed
1 parent 0f6c8f9 commit a54fba2

File tree

2 files changed

+24
-9
lines changed

2 files changed

+24
-9
lines changed

src/controllers/krate/publish.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -614,7 +614,7 @@ pub async fn publish(app: AppState, req: Parts, body: Body) -> AppResult<Json<Go
614614
let git_index_job = jobs::SyncToGitIndex::new(&krate.name);
615615
let sparse_index_job = jobs::SyncToSparseIndex::new(&krate.name);
616616
let publish_notifications_job = SendPublishNotificationsJob::new(version.id);
617-
let crate_feed_job = jobs::rss::SyncCrateFeed::new(krate.name.clone());
617+
let crate_feed_job = jobs::rss::SyncCrateFeed::new(krate.id, krate.name.clone());
618618
let updates_feed_job = jobs::rss::SyncUpdatesFeed;
619619

620620
tokio::try_join!(

src/worker/jobs/rss/sync_crate_feed.rs

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,16 @@ const NUM_ITEMS: i64 = 10;
2222

2323
#[derive(Serialize, Deserialize)]
2424
pub struct SyncCrateFeed {
25+
crate_id: Option<i32>,
2526
name: String,
2627
}
2728

2829
impl SyncCrateFeed {
29-
pub fn new(name: String) -> Self {
30-
Self { name }
30+
pub fn new(crate_id: i32, name: String) -> Self {
31+
Self {
32+
crate_id: Some(crate_id),
33+
name,
34+
}
3135
}
3236
}
3337

@@ -43,8 +47,13 @@ impl BackgroundJob for SyncCrateFeed {
4347

4448
info!("Loading latest {NUM_ITEMS} version updates for `{name}` from the database…");
4549
let mut conn = ctx.deadpool.get().await?;
50+
let crate_id = if let Some(crate_id) = self.crate_id {
51+
crate_id
52+
} else {
53+
load_crate_id(name, &mut conn).await?
54+
};
4655

47-
let version_updates = load_version_updates(name, &mut conn).await?;
56+
let version_updates = load_version_updates(crate_id, &mut conn).await?;
4857

4958
let feed_id = FeedId::Crate { name };
5059

@@ -89,21 +98,28 @@ impl BackgroundJob for SyncCrateFeed {
8998
}
9099
}
91100

101+
async fn load_crate_id(name: &str, conn: &mut AsyncPgConnection) -> QueryResult<i32> {
102+
crates::table
103+
.filter(crates::name.eq(name))
104+
.select(crates::id)
105+
.first::<i32>(conn)
106+
.await
107+
}
108+
92109
/// Load the latest versions from the database.
93110
///
94111
/// This function will load all versions from the database that are younger
95112
/// than [`ALWAYS_INCLUDE_AGE`]. If there are less than [`NUM_ITEMS`] versions
96113
/// then the list will be padded with older versions until [`NUM_ITEMS`] are
97114
/// returned.
98115
async fn load_version_updates(
99-
name: &str,
116+
crate_id: i32,
100117
conn: &mut AsyncPgConnection,
101118
) -> QueryResult<Vec<VersionUpdate>> {
102119
let threshold_dt = chrono::Utc::now().naive_utc() - ALWAYS_INCLUDE_AGE;
103120

104121
let updates = versions::table
105-
.inner_join(crates::table)
106-
.filter(crates::name.eq(name))
122+
.filter(versions::crate_id.eq(crate_id))
107123
.filter(versions::created_at.gt(threshold_dt))
108124
.order(versions::created_at.desc())
109125
.select(VersionUpdate::as_select())
@@ -116,8 +132,7 @@ async fn load_version_updates(
116132
}
117133

118134
versions::table
119-
.inner_join(crates::table)
120-
.filter(crates::name.eq(name))
135+
.filter(versions::crate_id.eq(crate_id))
121136
.order(versions::created_at.desc())
122137
.select(VersionUpdate::as_select())
123138
.limit(NUM_ITEMS)

0 commit comments

Comments
 (0)