-
Notifications
You must be signed in to change notification settings - Fork 647
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
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -90,8 +90,10 @@ tracing-subscriber = "0.2" | |
url = "2.1" | ||
|
||
[dev-dependencies] | ||
bigdecimal = { version = ">= 0.0.10, < 0.2.0" } | ||
claim = "0.5" | ||
conduit-test = "0.9.0-alpha.4" | ||
diesel = { version = "1.4.0", features = ["numeric"] } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the |
||
hyper-tls = "0.5" | ||
lazy_static = "1.0" | ||
tokio = "1.5.0" | ||
|
12 changes: 12 additions & 0 deletions
12
migrations/2021-09-03-132838_fix_to_semver_no_prerelease/down.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
19
migrations/2021-09-03-132838_fix_to_semver_no_prerelease/up.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
Turbo87 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
)::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 | ||
$$; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
Turbo87 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// 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); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
mod custom_functions; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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