Skip to content

SQL: Fix metadata handling in to_semver_no_prerelease() function #3886

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,10 @@ tracing-subscriber = "0.2"
url = "2.1"

[dev-dependencies]
bigdecimal = { version = ">= 0.0.10, < 0.2.0" }
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unfortunately diesel does not reexport the version that they depend on, so we have to add this here ourselves

claim = "0.5"
conduit-test = "0.9.0-alpha.4"
diesel = { version = "1.4.0", features = ["numeric"] }
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the numeric feature is necessary because the semver_triple returned by to_semver_no_prerelease() is based on the Numeric SQL type.

hyper-tls = "0.5"
lazy_static = "1.0"
tokio = "1.5.0"
Expand Down
12 changes: 12 additions & 0 deletions migrations/2021-09-03-132838_fix_to_semver_no_prerelease/down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
create or replace function to_semver_no_prerelease(text) returns semver_triple
immutable
language sql
as
$$
SELECT (
split_part($1, '.', 1)::numeric,
split_part($1, '.', 2)::numeric,
split_part(split_part($1, '+', 1), '.', 3)::numeric
)::semver_triple
WHERE strpos($1, '-') = 0
$$;
19 changes: 19 additions & 0 deletions migrations/2021-09-03-132838_fix_to_semver_no_prerelease/up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
create or replace function to_semver_no_prerelease(text) returns semver_triple
immutable
language sql
as
$$
SELECT (
-- 2) then, we extract the major, minor and patch numbers
-- (dropping the prerelease part to avoid number conversion errors)
split_part(version_without_metadata, '.', 1)::numeric,
split_part(version_without_metadata, '.', 2)::numeric,
split_part(split_part(version_without_metadata, '-', 1), '.', 3)::numeric
)::semver_triple
FROM (
-- 1) first, we split off the release metadata, if it exists
SELECT split_part($1, '+', 1) as version_without_metadata
) as vwm
-- 3) finally, we only return the result if there is no prerelease part
WHERE strpos(version_without_metadata, '-') = 0
$$;
1 change: 1 addition & 0 deletions src/tests/all.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ mod record;
mod schema_details;
mod server;
mod server_binary;
mod sql;
mod team;
mod token;
mod unhealthy_database;
Expand Down
39 changes: 39 additions & 0 deletions src/tests/sql/custom_functions.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
use bigdecimal::BigDecimal;
use diesel::prelude::*;
use diesel::sql_types::Text;
use diesel::{select, sql_function};

fn pg_connection() -> PgConnection {
let database_url =
dotenv::var("TEST_DATABASE_URL").expect("TEST_DATABASE_URL must be set to run tests");
let conn = PgConnection::establish(&database_url).unwrap();
conn.begin_test_transaction().unwrap();
conn
}

sql_function!(fn to_semver_no_prerelease(x: Text) -> Nullable<Record<(Numeric, Numeric, Numeric)>>);

#[test]
fn to_semver_no_prerelease_works() {
let conn = pg_connection();

#[track_caller]
fn test(conn: &PgConnection, text: &str, expected: Option<(i32, i32, i32)>) {
let query = select(to_semver_no_prerelease(text));
let result = query
.get_result::<Option<(BigDecimal, BigDecimal, BigDecimal)>>(conn)
.unwrap();

let expected = expected.map(|it| (it.0.into(), it.1.into(), it.2.into()));
assert_eq!(result, expected);
}

test(&conn, "0.0.0", Some((0, 0, 0)));
test(&conn, "1.2.4", Some((1, 2, 4)));
test(&conn, "1.2.4+metadata", Some((1, 2, 4)));
test(&conn, "1.2.4-beta.3", None);

// see https://github.com/rust-lang/crates.io/issues/3882
test(&conn, "0.4.45+curl-7.78.0", Some((0, 4, 45)));
test(&conn, "0.1.4-preview+4.3.2", None);
}
1 change: 1 addition & 0 deletions src/tests/sql/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mod custom_functions;