Skip to content

Commit 7438bdf

Browse files
committed
database/semver_ord: Return null if version number can't be parsed
1 parent e3ebad5 commit 7438bdf

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

crates/crates_io_database/tests/semver_ord.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crates_io_test_db::TestDatabase;
22
use diesel::prelude::*;
3-
use diesel::sql_types::Text;
3+
use diesel::sql_types::{Nullable, Text};
44
use diesel_async::RunQueryDsl;
55
use std::fmt::Debug;
66

@@ -14,15 +14,16 @@ async fn test_jsonb_output() {
1414

1515
#[derive(QueryableByName)]
1616
struct Row {
17-
#[diesel(sql_type = Text)]
18-
output: String,
17+
#[diesel(sql_type = Nullable<Text>)]
18+
output: Option<String>,
1919
}
2020

2121
diesel::sql_query(query)
2222
.get_result::<Row>(&mut conn)
2323
.await
2424
.unwrap()
2525
.output
26+
.unwrap_or_default()
2627
};
2728

2829
insta::assert_snapshot!(check("0.0.0").await, @r#"[0, 0, 0, {}]"#);
@@ -32,7 +33,7 @@ async fn test_jsonb_output() {
3233
insta::assert_snapshot!(check("1.0.0-0.HDTV-BluRay.1020p.YTSUB.L33TRip.mkv").await, @r#"[1, 0, 0, [false, 0, true, "HDTV-BluRay", true, "1020p", true, "YTSUB", true, "L33TRip", true, "mkv", null, null, null, null, null, null, null, null, ""]]"#);
3334

3435
// Invalid version string
35-
insta::assert_snapshot!(check("foo").await, @"[null, null, null, {}]");
36+
insta::assert_snapshot!(check("foo").await, @"");
3637

3738
// Version string with a lot of prerelease identifiers
3839
insta::assert_snapshot!(check("1.2.3-1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.end").await, @r#"[1, 2, 3, [false, 1, false, 2, false, 3, false, 4, false, 5, false, 6, false, 7, false, 8, false, 9, false, 10, "11.12.13.14.15.16.17.end"]]"#);

migrations/2025-03-06-060640_semver_ord/up.sql

+5-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ declare
1818
prerelease_part text;
1919
i int := 0;
2020
begin
21+
if match_result is null then
22+
return null;
23+
end if;
24+
2125
if match_result[4] is null then
2226
-- A JSONB object has higher precedence than an array, and versions with
2327
-- prerelease specifiers should have lower precedence than those without.
@@ -67,7 +71,7 @@ comment on function semver_ord is 'Converts a semver string into a JSONB array f
6771
-- Add corresponding column to the `versions` table.
6872

6973
alter table versions
70-
add semver_ord jsonb default 'null'::jsonb not null;
74+
add semver_ord jsonb;
7175

7276
comment on column versions.semver_ord is 'JSONB representation of the version number for sorting purposes.';
7377

0 commit comments

Comments
 (0)