mirror of
https://github.com/percona/percona-toolkit.git
synced 2025-09-08 11:07:58 +00:00
add 5.7 compatibility for pt-show-grants
This commit is contained in:
@@ -1727,6 +1727,7 @@ sub _d {
|
||||
# End Daemon 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 +1827,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 +1858,27 @@ sub main {
|
||||
PTDEBUG && _d('Checking user', $user_host);
|
||||
}
|
||||
|
||||
# If MySQL 5.7+ then we need to use SHOW CREATE USER
|
||||
my @create_user;
|
||||
if ( compare_versions($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 +1925,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 +1955,7 @@ sub main {
|
||||
}
|
||||
|
||||
@result;
|
||||
} @grants;
|
||||
} grep { /\bgrant\b/i } @grants;
|
||||
|
||||
print join(
|
||||
"\n",
|
||||
@@ -2007,6 +2033,37 @@ 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;
|
||||
@_ = map { (my $temp = $_) =~ s/\n/\n# /g; $temp; }
|
||||
|
2421
bin/pt-show-grants.old
Executable file
2421
bin/pt-show-grants.old
Executable file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user