Skip to content

Commit d48cd74

Browse files
authored
controllers/token: Order by id instead of created_at (#7181)
In a real world scenario this should not make a huge difference, since `id` is a monotonically increasing counter, so the order should be the same as with `created_at`. We do have the unique `id` index though, which might be used by the database to speed up the query. The main reason for this change is to ensure a stable sort order for our test suite. The `list_tokens()` test case has been a bit flaky lately (see https://github.com/rust-lang/crates.io/actions/runs/6305682314/job/17119535543?pr=7178) because the `created_at` column for all tokens is the same during a test, due to each test running within a Postgres transaction. The `id` however won't be the same, so we can achieve a stable sort order that way.
1 parent f6b1ff3 commit d48cd74

File tree

2 files changed

+10
-10
lines changed

2 files changed

+10
-10
lines changed

src/controllers/token.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ pub async fn list(
5656
.bind::<Interval, _>(params.expired_days_interval())
5757
.sql(")"))),
5858
)
59-
.order(api_tokens::created_at.desc())
59+
.order(api_tokens::id.desc())
6060
.load(conn)?;
6161

6262
Ok(Json(json!({ "api_tokens": tokens })))

src/tests/routes/me/tokens/snapshots/all__routes__me__tokens__list__list_tokens.snap

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,6 @@ expression: response.into_json()
44
---
55
{
66
"api_tokens": [
7-
{
8-
"crate_scopes": null,
9-
"created_at": "[datetime]",
10-
"endpoint_scopes": null,
11-
"expired_at": null,
12-
"id": "[id]",
13-
"last_used_at": "[datetime]",
14-
"name": "bar"
15-
},
167
{
178
"crate_scopes": [
189
"serde",
@@ -26,6 +17,15 @@ expression: response.into_json()
2617
"id": "[id]",
2718
"last_used_at": "[datetime]",
2819
"name": "baz"
20+
},
21+
{
22+
"crate_scopes": null,
23+
"created_at": "[datetime]",
24+
"endpoint_scopes": null,
25+
"expired_at": null,
26+
"id": "[id]",
27+
"last_used_at": "[datetime]",
28+
"name": "bar"
2929
}
3030
]
3131
}

0 commit comments

Comments
 (0)