mirror of
https://github.com/percona/percona-toolkit.git
synced 2025-09-01 18:25:59 +00:00
made compare_versions func into its own module. made tests
This commit is contained in:
@@ -16,6 +16,7 @@ BEGIN {
|
||||
OptionParser
|
||||
DSNParser
|
||||
Daemon
|
||||
VersionCompare
|
||||
));
|
||||
}
|
||||
|
||||
@@ -1727,6 +1728,52 @@ sub _d {
|
||||
# End Daemon package
|
||||
# ###########################################################################
|
||||
|
||||
# ###########################################################################
|
||||
# VersionCompare package
|
||||
# This package is a copy without comments from the original. The original
|
||||
# with comments and its test file can be found in the Bazaar repository at,
|
||||
# lib/VersionCompare.pm
|
||||
# t/lib/VersionCompare.t
|
||||
# See https://launchpad.net/percona-toolkit for more information.
|
||||
# ###########################################################################
|
||||
{
|
||||
package VersionCompare;
|
||||
|
||||
use strict;
|
||||
use English qw(-no_match_vars);
|
||||
use constant PTDEBUG => $ENV{PTDEBUG} || 0;
|
||||
|
||||
sub cmp {
|
||||
my ($v1, $v2) = @_;
|
||||
|
||||
$v1 =~ s/[^\d\.]//;
|
||||
$v2 =~ s/[^\d\.]//;
|
||||
|
||||
my @a = ( $v1 =~ /(\d+)\.?/g );
|
||||
my @b = ( $v2 =~ /(\d+)\.?/g );
|
||||
foreach my $n1 (@a) {
|
||||
$n1 += 0;
|
||||
if (!@b) {
|
||||
return 1;
|
||||
}
|
||||
my $n2 = shift @b;
|
||||
$n2 += 0;
|
||||
if ($n1 == $n2) {
|
||||
next;
|
||||
}
|
||||
else {
|
||||
return $n1 <=> $n2;
|
||||
}
|
||||
}
|
||||
return @b ? -1 : 0;
|
||||
}
|
||||
|
||||
|
||||
1;
|
||||
}
|
||||
# ###########################################################################
|
||||
# End VersionCompare package
|
||||
# ###########################################################################
|
||||
|
||||
# ###########################################################################
|
||||
# This is a combination of modules and programs in one -- a runnable module.
|
||||
@@ -1858,9 +1905,9 @@ sub main {
|
||||
PTDEBUG && _d('Checking user', $user_host);
|
||||
}
|
||||
|
||||
# If MySQL 5.7+ then we need to use SHOW CREATE USER
|
||||
# If MySQL 5.7.6+ then we need to use SHOW CREATE USER
|
||||
my @create_user;
|
||||
if ( compare_versions($version, '5.7.6') >= 0 ) {
|
||||
if ( VersionCompare::cmp($version, '5.7.6') >= 0 ) {
|
||||
eval {
|
||||
@create_user = @{ $dbh->selectcol_arrayref("SHOW CREATE USER $user_host") };
|
||||
};
|
||||
@@ -2033,36 +2080,6 @@ sub split_grants {
|
||||
return @grants;
|
||||
}
|
||||
|
||||
sub compare_versions {
|
||||
my ($v1, $v2) = @_;
|
||||
|
||||
# Remove all but numbers and dots.
|
||||
# Assume simple 1.2.3 style
|
||||
$v1 =~ s/[^\d\.]//;
|
||||
$v2 =~ s/[^\d\.]//;
|
||||
|
||||
my @a = ( $v1 =~ /(\d+)\.?/g );
|
||||
my @b = ( $v2 =~ /(\d+)\.?/g );
|
||||
foreach my $n1 (@a) {
|
||||
$n1 += 0; #convert to number
|
||||
if (!@b) {
|
||||
# b ran out of digits, a is larger
|
||||
return 1;
|
||||
}
|
||||
my $n2 = shift @b;
|
||||
$n2 += 0; # convert to number
|
||||
if ($n1 == $n2) {
|
||||
# still tied?, fetch next
|
||||
next;
|
||||
}
|
||||
else {
|
||||
# difference! return result
|
||||
return $n1 <=> $n2;
|
||||
}
|
||||
}
|
||||
# b still has digits? it's larger, else it's a tie
|
||||
return @b ? -1 : 0;
|
||||
}
|
||||
|
||||
sub _d {
|
||||
my ($package, undef, $line) = caller 0;
|
||||
|
File diff suppressed because it is too large
Load Diff
68
lib/VersionCompare.pm
Normal file
68
lib/VersionCompare.pm
Normal file
@@ -0,0 +1,68 @@
|
||||
# This program is copyright 2016 Percona LLC.
|
||||
# Feedback and improvements are welcome.
|
||||
#
|
||||
# THIS PROGRAM IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
|
||||
# WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
|
||||
# MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|
||||
#
|
||||
# This program is free software; you can redistribute it and/or modify it under
|
||||
# the terms of the GNU General Public License as published by the Free Software
|
||||
# Foundation, version 2; OR the Perl Artistic License. On UNIX and similar
|
||||
# systems, you can issue `man perlgpl' or `man perlartistic' to read these
|
||||
# licenses.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License along with
|
||||
# this program; if not, write to the Free Software Foundation, Inc., 59 Temple
|
||||
# Place, Suite 330, Boston, MA 02111-1307 USA.
|
||||
# ###########################################################################
|
||||
# VersionCompare package
|
||||
# ###########################################################################
|
||||
|
||||
# The purpose of this very simple module is to compare MySQL version strings
|
||||
# There's VersionParser and the perl core "version" module, but I wanted
|
||||
# something simpler and that could grow incrementally
|
||||
|
||||
{
|
||||
package VersionCompare;
|
||||
|
||||
use strict;
|
||||
use English qw(-no_match_vars);
|
||||
use constant PTDEBUG => $ENV{PTDEBUG} || 0;
|
||||
|
||||
sub cmp {
|
||||
my ($v1, $v2) = @_;
|
||||
|
||||
# Remove all but numbers and dots.
|
||||
# Assume simple 1.2.3 style
|
||||
$v1 =~ s/[^\d\.]//;
|
||||
$v2 =~ s/[^\d\.]//;
|
||||
|
||||
my @a = ( $v1 =~ /(\d+)\.?/g );
|
||||
my @b = ( $v2 =~ /(\d+)\.?/g );
|
||||
foreach my $n1 (@a) {
|
||||
$n1 += 0; #convert to number
|
||||
if (!@b) {
|
||||
# b ran out of digits, a is larger
|
||||
return 1;
|
||||
}
|
||||
my $n2 = shift @b;
|
||||
$n2 += 0; # convert to number
|
||||
if ($n1 == $n2) {
|
||||
# still tied?, fetch next
|
||||
next;
|
||||
}
|
||||
else {
|
||||
# difference! return result
|
||||
return $n1 <=> $n2;
|
||||
}
|
||||
}
|
||||
# b still has digits? it's larger, else it's a tie
|
||||
return @b ? -1 : 0;
|
||||
}
|
||||
|
||||
|
||||
1;
|
||||
}
|
||||
# ###########################################################################
|
||||
# End VersionCompare package
|
||||
# ###########################################################################
|
48
t/lib/VersionCompare.t
Normal file
48
t/lib/VersionCompare.t
Normal file
@@ -0,0 +1,48 @@
|
||||
#!/usr/bin/perl
|
||||
|
||||
BEGIN {
|
||||
die "The PERCONA_TOOLKIT_BRANCH environment variable is not set.\n"
|
||||
unless $ENV{PERCONA_TOOLKIT_BRANCH} && -d $ENV{PERCONA_TOOLKIT_BRANCH};
|
||||
unshift @INC, "$ENV{PERCONA_TOOLKIT_BRANCH}/lib";
|
||||
};
|
||||
|
||||
use strict;
|
||||
use warnings FATAL => 'all';
|
||||
use English qw(-no_match_vars);
|
||||
use Test::More tests=>14;
|
||||
|
||||
use VersionCompare;
|
||||
use PerconaTest;
|
||||
|
||||
my @versions = qw( 5.7 5.6 1
|
||||
5.6 5.7 -1
|
||||
5.6 5.6 0
|
||||
5.17 5.6 1
|
||||
5.9 5.17 -1
|
||||
5.10 5.10 0
|
||||
5.1.2 5.5 -1
|
||||
5 3 1
|
||||
5.6 5.5.5 1
|
||||
5.7.7 5.7.7 0
|
||||
5.6 5.6.0 -1
|
||||
v5.4.3-0 5.7 -1
|
||||
5.7 v5.4.3-0 1
|
||||
v5.7.3-0 v5.4.3-0 1
|
||||
);
|
||||
|
||||
while ( @versions ) {
|
||||
my $v1 = shift @versions;
|
||||
my $v2 = shift @versions;
|
||||
my $res = shift @versions;
|
||||
|
||||
ok ( VersionCompare::cmp($v1, $v2) == $res,
|
||||
"$v1 vs $v2"
|
||||
) or diag("result was [",VersionCompare::cmp($v1, $v2),"]");
|
||||
}
|
||||
|
||||
|
||||
# #############################################################################
|
||||
# Done.
|
||||
# #############################################################################
|
||||
#ok($sb->ok(), "Sandbox servers") or BAIL_OUT(__FILE__ . " broke the sandbox");
|
||||
exit;
|
Reference in New Issue
Block a user