-
Notifications
You must be signed in to change notification settings - Fork 21
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
Refactor and enhance version comparison #907
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
0bff50c
chore: implement other version schemes using an exact match
ctron e6e8fe3
refactor: Convert version scheme into an enum
ctron d3ce614
chore: work around a clippy warning
ctron 01d5b99
chore: clean up cve project data conversion for version schemes
ctron 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 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 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 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 |
---|---|---|
@@ -1,15 +1,17 @@ | ||
use sea_orm::entity::prelude::*; | ||
|
||
#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel)] | ||
#[sea_orm(table_name = "version_scheme")] | ||
pub struct Model { | ||
#[sea_orm(primary_key)] | ||
pub id: String, | ||
pub name: String, | ||
pub description: Option<String>, | ||
#[derive(Copy, Clone, Eq, Hash, Debug, PartialEq, EnumIter, DeriveActiveEnum, strum::Display)] | ||
#[sea_orm( | ||
rs_type = "String", | ||
db_type = "String(StringLen::None)", | ||
rename_all = "camelCase" | ||
)] | ||
#[strum(serialize_all = "camelCase")] | ||
pub enum VersionScheme { | ||
Generic, | ||
Git, | ||
Semver, | ||
Rpm, | ||
Python, | ||
Maven, | ||
} | ||
|
||
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] | ||
pub enum Relation {} | ||
|
||
impl ActiveModelBehavior for ActiveModel {} |
This file contains 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 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,44 @@ | ||
use sea_orm_migration::prelude::*; | ||
|
||
#[derive(DeriveMigrationName)] | ||
pub struct Migration; | ||
|
||
#[async_trait::async_trait] | ||
#[allow(deprecated)] | ||
impl MigrationTrait for Migration { | ||
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> { | ||
manager | ||
.get_connection() | ||
.execute_unprepared(include_str!("m0000670_version_cmp/version_matches.sql")) | ||
.await | ||
.map(|_| ())?; | ||
|
||
manager | ||
.get_connection() | ||
.execute_unprepared(include_str!( | ||
"m0000670_version_cmp/generic_version_matches.sql" | ||
)) | ||
.await | ||
.map(|_| ())?; | ||
|
||
Ok(()) | ||
} | ||
|
||
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> { | ||
manager | ||
.get_connection() | ||
.execute_unprepared(include_str!( | ||
"m0000620_parallel_unsafe_pg_fns/version_matches.sql" | ||
)) | ||
.await | ||
.map(|_| ())?; | ||
|
||
manager | ||
.get_connection() | ||
.execute_unprepared(r#"DROP FUNCTION generic_version_matches"#) | ||
.await | ||
.map(|_| ())?; | ||
|
||
Ok(()) | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
migration/src/m0000670_version_cmp/generic_version_matches.sql
This file contains 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,28 @@ | ||
-- this is just an exact version match | ||
create or replace function generic_version_matches(version_p text, range_p version_range) | ||
returns bool | ||
as | ||
$$ | ||
begin | ||
if range_p.low_version is not null then | ||
if range_p.low_inclusive then | ||
if version_p = range_p.low_version then | ||
return true; | ||
end if; | ||
end if; | ||
end if; | ||
|
||
if range_p.high_version is not null then | ||
if range_p.high_inclusive then | ||
if version_p = range_p.high_version then | ||
return true; | ||
end if; | ||
end if; | ||
end if; | ||
|
||
return false; | ||
|
||
end | ||
$$ | ||
language plpgsql immutable; | ||
|
This file contains 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,42 @@ | ||
create or replace function version_matches(version_p text, range_p version_range) | ||
returns bool | ||
as | ||
$$ | ||
declare | ||
begin | ||
-- for an authoritative list of support schemes, see the enum | ||
-- `trustify_entity::version_scheme::VersionScheme` | ||
return case | ||
when range_p.version_scheme_id = 'git' | ||
-- Git is git, and hard. | ||
then gitver_version_matches(version_p, range_p) | ||
when range_p.version_scheme_id = 'semver' | ||
-- Semver is semver | ||
then semver_version_matches(version_p, range_p) | ||
when range_p.version_scheme_id = 'gem' | ||
-- RubyGems claims to be semver | ||
then semver_version_matches(version_p, range_p) | ||
when range_p.version_scheme_id = 'npm' | ||
-- NPM claims to be semver | ||
then semver_version_matches(version_p, range_p) | ||
when range_p.version_scheme_id = 'golang' | ||
-- Golang claims to be semver | ||
then semver_version_matches(version_p, range_p) | ||
when range_p.version_scheme_id = 'nuget' | ||
-- NuGet claims to be semver | ||
then semver_version_matches(version_p, range_p) | ||
when range_p.version_scheme_id = 'generic' | ||
-- Just check if it is equal | ||
then generic_version_matches(version_p, range_p) | ||
when range_p.version_scheme_id = 'rpm' | ||
-- Look at me! I'm an RPM! I'm special! | ||
then rpmver_version_matches(version_p, range_p) | ||
when range_p.version_scheme_id = 'maven' | ||
-- Look at me! I'm a Maven! I'm kinda special! | ||
then maven_version_matches(version_p, range_p) | ||
else | ||
false | ||
end; | ||
end | ||
$$ | ||
language plpgsql immutable; |
This file contains 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 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 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
Oops, something went wrong.
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.
you could rewrite to be a bit more compact:
untested, I doubt this would result in such a speed up that it is worth reducing readability