Skip to content

Commit

Permalink
fix parallel safe by removing raise notice
Browse files Browse the repository at this point in the history
  • Loading branch information
JimFuller-RedHat committed Sep 10, 2024
1 parent c584c01 commit fb472ea
Show file tree
Hide file tree
Showing 3 changed files with 154 additions and 0 deletions.
2 changes: 2 additions & 0 deletions migration/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ mod m0000575_create_weakness;
mod m0000580_mark_fns;
mod m0000590_get_purl_fns;
mod m0000595_analysis_api_index;
mod m0000600_remove_raise_notice_fns;

pub struct Migrator;

Expand Down Expand Up @@ -153,6 +154,7 @@ impl MigratorTrait for Migrator {
Box::new(m0000580_mark_fns::Migration),
Box::new(m0000590_get_purl_fns::Migration),
Box::new(m0000595_analysis_api_index::Migration),
Box::new(m0000600_remove_raise_notice_fns::Migration),
]
}
}
Expand Down
28 changes: 28 additions & 0 deletions migration/src/m0000600_remove_raise_notice_fns.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
use sea_orm_migration::prelude::*;

#[derive(DeriveMigrationName)]
pub struct Migration;

#[async_trait::async_trait]
impl MigrationTrait for Migration {
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.get_connection()
.execute_unprepared(include_str!(
"m0000600_remove_raise_notice_fns/semver_cmp.sql"
))
.await
.map(|_| ())?;
Ok(())
}

async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.get_connection()
.execute_unprepared(include_str!("m0000580_mark_fns/semver_cmp.sql"))
.await
.map(|_| ())?;

Ok(())
}
}
124 changes: 124 additions & 0 deletions migration/src/m0000600_remove_raise_notice_fns/semver_cmp.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
create or replace function semver_cmp(left_p text, right_p text)
returns integer
as
$$
declare
left_parts text[];
right_parts text[];

left_major bigint;
left_minor bigint;
left_patch bigint;
left_pre text;
left_build text;

right_major bigint;
right_minor bigint;
right_patch bigint;
right_pre text;
right_build text;

left_numeric bool;
right_numeric bool;

cur integer;

begin
left_parts = regexp_split_to_array(left_p, E'\\+');
left_build = left_parts[2];

left_parts = regexp_split_to_array(left_parts[1], E'-');
left_pre = left_parts[2];

left_parts = regexp_split_to_array(left_parts[1], E'\\.');
left_major = left_parts[1]::decimal;
left_minor = left_parts[2]::decimal;
left_patch = left_parts[3]::decimal;

right_parts = regexp_split_to_array(right_p, E'\\+');
right_build = right_parts[2];

right_parts = regexp_split_to_array(right_parts[1], E'-');
right_pre = right_parts[2];

right_parts = regexp_split_to_array(right_parts[1], E'\\.');
right_major = right_parts[1]::decimal;
right_minor = right_parts[2]::decimal;
right_patch = right_parts[3]::decimal;

if left_major > right_major then
return +1;
elsif left_major < right_major then
return -1;
end if;

if left_minor > right_minor then
return +1;
elsif left_minor < right_minor then
return -1;
end if;

if left_patch > right_patch then
return +1;
elsif left_patch < right_patch then
return -1;
end if;

if left_pre is null and right_pre is not null then
return +1;
elsif left_pre is not null and right_pre is null then
return -1;
elsif left_pre is not null and right_pre is not null then
left_parts = regexp_split_to_array(left_pre, E'\\.');
right_parts = regexp_split_to_array(right_pre, E'\\.');
-- do the hard work

cur := 0;
loop
cur := cur + 1;

left_pre := left_parts[cur];
right_pre := right_parts[cur];

if left_pre is null and right_pre is null then
return 0;
end if;

if left_pre is null and right_pre is not null then
return -1;
elsif left_pre is not null and right_pre is null then
return +1;
end if;

left_numeric := is_numeric(left_pre);
right_numeric := is_numeric(right_pre);

if left_numeric and right_numeric then
if left_pre::bigint < right_pre::bigint then
return -1;
elsif left_pre::bigint > right_pre::bigint then
return +1;
end if;
else
if left_pre < right_pre then
return -1;
elsif left_pre > right_pre then
return +1;
end if;
end if;

if cur > 10 then
exit;
end if;
end loop;
else
return 0;
end if;

return null;
exception
when others then
return null;
end
$$
language 'plpgsql' immutable parallel safe;

0 comments on commit fb472ea

Please sign in to comment.