codemonth.dk

One project every month - making stuff better ...

SEMVER utility for plsql

So anyone doing software releases knows the pain of doing versions correctly for the different releases. One solution to that is using SEMVER (http://semver.org/) which tries to provide a solution for that. Go to the website and read up when you change the different parts of the version (x.y.z = major.minor.patch), and what the rules are around comparing different versions.

The current version has implemented:

  • valid - Is it a valid semver version.
  • gt - Greater than check for 2 semver versions
  • lt - Less than check for 2 semver versions
  • clean - Clean a semver version for unnecessary spaces
  • inc - Increment a part of a semver string

So just download the package from my github repository and install it in any schema in the database


SQL> @semver_ninja.package.sql

Package created.

SQL> @"semver_ninja.package body.sql"

Package body created.

SQL> 

Below is a test of all the implemented functionality as of now.


set serverout on

declare
	l_semver	varchar2(150) := '1.2.3';
	l_semver_2	varchar2(150) := '1.2.2';
	procedure s(s_in varchar2)
	as
	begin
		dbms_output.put_line(s_in);
	end;
begin
	s('Testing if ' || l_semver || ' is valid. Expect valid.');
	if semver_ninja.valid(l_semver) then
		s('Is valid');
	else
		s('Not valid');
	end if;

	l_semver := '1.02.3';
	s('Testing if ' || l_semver || ' is valid. Expect not valid.');
	if semver_ninja.valid(l_semver) then
		s('Is valid');
	else
		s('Not valid');
	end if;

	l_semver := 'A.B.C';
	s('Testing if ' || l_semver || ' is valid. Expect not valid.');
	if semver_ninja.valid(l_semver) then
		s('Is valid');
	else
		s('Not valid');
	end if;

	l_semver := '1.2.3';
	s('Testing if ' || l_semver || ' is greater than '|| l_semver_2 ||'. Expect greater than.');
	if semver_ninja.gt(l_semver, l_semver_2) then
		s('Greater than');
	else
		s('Not greater than');
	end if;

	l_semver := '1.1.5';
	s('Testing if ' || l_semver || ' is greater than '|| l_semver_2 ||'. Expect not greater than.');
	if semver_ninja.gt(l_semver, l_semver_2) then
		s('Greater than');
	else
		s('Not greater than');
	end if;

	l_semver := '1.1.5';
	s('Testing if ' || l_semver || ' is greater than '|| l_semver_2 ||'. Expect not greater than.');
	if semver_ninja.gt(l_semver, l_semver_2) then
		s('Greater than');
	else
		s('Not greater than');
	end if;

	l_semver := '1.0.3';
	s('Testing if ' || l_semver || ' is less than '|| l_semver_2 ||'. Expect less than.');
	if semver_ninja.lt(l_semver, l_semver_2) then
		s('Less than');
	else
		s('Not less than');
	end if;

	l_semver := '2.0.0';
	s('Testing if ' || l_semver || ' is less than '|| l_semver_2 ||'. Expect not less than.');
	if semver_ninja.lt(l_semver, l_semver_2) then
		s('Less than');
	else
		s('Not less than');
	end if;
	
	l_semver := ' v1.2.3     ';
	s('Testing clean of "' || l_semver ||'". Expect output to be "1.2.3"');
	s('Output: "' || semver_ninja.clean(l_semver) || '"');

	l_semver := '1.2.3';
	s('Testing increment of patch version of 1.2.3. Expect 1.2.4.');
	s(semver_ninja.inc(l_semver, 'patch'));

	l_semver := '1.2.3';
	s('Testing increment of minor version of 1.2.3. Expect 1.3.0.');
	s(semver_ninja.inc(l_semver, 'minor'));

	l_semver := '1.2.3';
	s('Testing increment of major version of 1.2.3. Expect 2.0.0.');
	s(semver_ninja.inc(l_semver, 'major'));
end;
/

Testing if 1.2.3 is valid. Expect valid.
Is valid
Testing if 1.02.3 is valid. Expect not valid.
Not valid
Testing if A.B.C is valid. Expect not valid.
Not valid
Testing if 1.2.3 is greater than 1.2.2. Expect greater than.
Greater than
Testing if 1.1.5 is greater than 1.2.2. Expect not greater than.
Not greater than
Testing if 1.1.5 is greater than 1.2.2. Expect not greater than.
Not greater than
Testing if 1.0.3 is less than 1.2.2. Expect less than.
Less than
Testing if 2.0.0 is less than 1.2.2. Expect not less than.
Not less than
Testing clean of " v1.2.3     ". Expect output to be "1.2.3"
Output: "1.2.3"
Testing increment of patch version of 1.2.3. Expect 1.2.4.
1.2.4
Testing increment of minor version of 1.2.3. Expect 1.3.0.
1.3.0
Testing increment of major version of 1.2.3. Expect 2.0.0.
2.0.0

PL/SQL procedure successfully completed.

SQL>

What needs to be added is the satisfies functionality, which can calculate version requirements based on different ranges or predicates, like:
satisfies('1.2.3', '1.x || >=2.5.0 || 5.0.0 - 7.2.3')
which would equate to true

Tagged in : Pipelined function