Skip to content

Commit 6a5d70c

Browse files
committed
controllers/krate/metadata: Sort versions by id
This changes the sorting of `crate.versions`. Previously, we sorted versions by semver, which is more computationally expensive compared to sorting by id/date, which can be achieved more easily directly from the database. While this change alters the sorting order, it still provides consistent output and eliminates the need for double semver sorting.
1 parent 9b89b10 commit 6a5d70c

File tree

2 files changed

+22
-26
lines changed

2 files changed

+22
-26
lines changed

src/controllers/krate/metadata.rs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ use axum_extra::json;
1818
use axum_extra::response::ErasedJson;
1919
use diesel::prelude::*;
2020
use diesel_async::RunQueryDsl;
21-
use std::cmp::Reverse;
2221
use std::str::FromStr;
2322

2423
#[derive(Debug, Deserialize, FromRequestParts, utoipa::IntoParams)]
@@ -91,15 +90,12 @@ pub async fn find_crate(
9190
.ok_or_else(|| crate_not_found(&path.name))?;
9291

9392
let mut versions_publishers_and_audit_actions = if include.versions {
94-
let mut versions_and_publishers: Vec<(Version, Option<User>)> =
95-
Version::belonging_to(&krate)
96-
.left_outer_join(users::table)
97-
.select(<(Version, Option<User>)>::as_select())
98-
.load(&mut conn)
99-
.await?;
100-
101-
versions_and_publishers
102-
.sort_by_cached_key(|(version, _)| Reverse(semver::Version::parse(&version.num).ok()));
93+
let versions_and_publishers: Vec<(Version, Option<User>)> = Version::belonging_to(&krate)
94+
.left_outer_join(users::table)
95+
.select(<(Version, Option<User>)>::as_select())
96+
.order_by(versions::id)
97+
.load(&mut conn)
98+
.await?;
10399

104100
let versions = versions_and_publishers
105101
.iter()

src/tests/routes/crates/snapshots/crates_io__tests__routes__crates__read__show.snap

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ snapshot_kind: text
3636
"updated_at": "[datetime]",
3737
"versions": [
3838
1,
39-
3,
40-
2
39+
2,
40+
3
4141
],
4242
"yanked": false
4343
},
@@ -90,30 +90,30 @@ snapshot_kind: text
9090
"crate_size": 0,
9191
"created_at": "[datetime]",
9292
"description": null,
93-
"dl_path": "/api/v1/crates/foo_show/0.5.1/download",
93+
"dl_path": "/api/v1/crates/foo_show/0.5.0/download",
9494
"documentation": null,
9595
"downloads": 0,
9696
"edition": null,
9797
"features": {},
9898
"has_lib": null,
9999
"homepage": null,
100-
"id": 3,
100+
"id": 2,
101101
"lib_links": null,
102102
"license": null,
103103
"links": {
104-
"authors": "/api/v1/crates/foo_show/0.5.1/authors",
105-
"dependencies": "/api/v1/crates/foo_show/0.5.1/dependencies",
106-
"version_downloads": "/api/v1/crates/foo_show/0.5.1/downloads"
104+
"authors": "/api/v1/crates/foo_show/0.5.0/authors",
105+
"dependencies": "/api/v1/crates/foo_show/0.5.0/dependencies",
106+
"version_downloads": "/api/v1/crates/foo_show/0.5.0/downloads"
107107
},
108-
"num": "0.5.1",
108+
"num": "0.5.0",
109109
"published_by": {
110110
"avatar": null,
111111
"id": 1,
112112
"login": "foo",
113113
"name": null,
114114
"url": "https://github.com/foo"
115115
},
116-
"readme_path": "/api/v1/crates/foo_show/0.5.1/readme",
116+
"readme_path": "/api/v1/crates/foo_show/0.5.0/readme",
117117
"repository": null,
118118
"rust_version": null,
119119
"updated_at": "[datetime]",
@@ -128,30 +128,30 @@ snapshot_kind: text
128128
"crate_size": 0,
129129
"created_at": "[datetime]",
130130
"description": null,
131-
"dl_path": "/api/v1/crates/foo_show/0.5.0/download",
131+
"dl_path": "/api/v1/crates/foo_show/0.5.1/download",
132132
"documentation": null,
133133
"downloads": 0,
134134
"edition": null,
135135
"features": {},
136136
"has_lib": null,
137137
"homepage": null,
138-
"id": 2,
138+
"id": 3,
139139
"lib_links": null,
140140
"license": null,
141141
"links": {
142-
"authors": "/api/v1/crates/foo_show/0.5.0/authors",
143-
"dependencies": "/api/v1/crates/foo_show/0.5.0/dependencies",
144-
"version_downloads": "/api/v1/crates/foo_show/0.5.0/downloads"
142+
"authors": "/api/v1/crates/foo_show/0.5.1/authors",
143+
"dependencies": "/api/v1/crates/foo_show/0.5.1/dependencies",
144+
"version_downloads": "/api/v1/crates/foo_show/0.5.1/downloads"
145145
},
146-
"num": "0.5.0",
146+
"num": "0.5.1",
147147
"published_by": {
148148
"avatar": null,
149149
"id": 1,
150150
"login": "foo",
151151
"name": null,
152152
"url": "https://github.com/foo"
153153
},
154-
"readme_path": "/api/v1/crates/foo_show/0.5.0/readme",
154+
"readme_path": "/api/v1/crates/foo_show/0.5.1/readme",
155155
"repository": null,
156156
"rust_version": null,
157157
"updated_at": "[datetime]",

0 commit comments

Comments
 (0)