Merge pull request #78 from percona/pt-show-grants-compat-lp1435370

add 5.7 compatibility for pt-show-grants
This commit is contained in:
Frank Cizmich
2016-02-24 17:22:40 -03:00
10 changed files with 281 additions and 18 deletions

View File

@@ -16,6 +16,7 @@ BEGIN {
OptionParser
DSNParser
Daemon
VersionCompare
));
}
@@ -1727,6 +1728,53 @@ 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.
# http://www.perl.com/pub/a/2006/07/13/lightning-articles.html?page=last
@@ -1826,6 +1874,7 @@ sub main {
{ AutoCommit => 1, });
my ( $version, $ts ) = $dbh->selectrow_array("SELECT VERSION(), NOW()");
print join("\n",
"-- Grants dumped by pt-show-grants",
"-- Dumped from server " . ($dbh->{mysql_hostinfo} || '')
@@ -1856,6 +1905,27 @@ sub main {
PTDEBUG && _d('Checking user', $user_host);
}
# If MySQL 5.7.6+ then we need to use SHOW CREATE USER
my @create_user;
if ( VersionCompare::cmp($version, '5.7.6') >= 0 ) {
eval {
@create_user = @{ $dbh->selectcol_arrayref("SHOW CREATE USER $user_host") };
};
if ( $EVAL_ERROR ) {
PTDEBUG && _d($EVAL_ERROR);
$exit_status = 1;
}
PTDEBUG && _d('CreateUser:', Dumper(\@create_user));
# make this replication safe converting the CREATE USER into
# CREATE USER IF NOT EXISTS and then doing an ALTER USER
my $create = $create_user[0];
my $alter = $create;
$create =~ s{CREATE USER}{CREATE USER IF NOT EXISTS};
$create =~ s{ IDENTIFIED .*}{};
$alter =~ s{CREATE USER}{ALTER USER};
@create_user = ( $create, $alter );
PTDEBUG && _d('AdjustedCreateUser:', Dumper(\@create_user));
}
my @grants;
eval {
@grants = @{ $dbh->selectcol_arrayref("SHOW GRANTS FOR $user_host") };
@@ -1902,6 +1972,9 @@ sub main {
@grants = sort {
$b =~ m/IDENTIFIED BY/ <=> $a =~ m/IDENTIFIED BY/ || $a cmp $b
} @grants;
# Add @create_user if there
@grants = ( @create_user, @grants ) if scalar @create_user > 0;
PTDEBUG && _d('Grants sorted:', Dumper(\@grants));
# Print REVOKE statements.
@@ -1929,7 +2002,7 @@ sub main {
}
@result;
} @grants;
} grep { /\bgrant\b/i } @grants;
print join(
"\n",
@@ -2007,6 +2080,7 @@ sub split_grants {
return @grants;
}
sub _d {
my ($package, undef, $line) = caller 0;
@_ = map { (my $temp = $_) =~ s/\n/\n# /g; $temp; }