Skip to content

Commit 642253c

Browse files
PT-2156 Fix tests for lib (#606)
* PT-2156 - Fix tests for lib - Fixed tests broken for lib/TableParser.pm after fix for PT-1059 - Updated tests for lib/TableParser.pm that are broken due to SHOW CREATE TABLE output format change in 8.0 - Updated modules for all tools that use lib/TableParser.pm * Revert "Fixed pt-archiver tests" This reverts commit a3ab87b. This commit wa needed, because removed code in sandbox/slave_channels.sql broked the test. Proper fix would be to do not remove channel names rather than chaging the test. So revertig it. * PT-2156 - Fix tests for lib - Updated test t/lib/TableChecksum.t so it reflects changes, introduced in the fix for PT-2016 - Updated test t/lib/RowChecksum.t so it reflects changes added to in the fix for PT-2138: UTF8 support - Uncommented SQL in sandbox/slave_channels.sql that made t/lib/MasterSlave.t to fail - Added check for undef into t/pt-archiver/archive_using_channels.t - Updated lib/Cxn.pm so it uses $dbh->{Active} after issue with the ping() call, reported at perl5-dbi/DBD-mysql#306 * PT-2156 - Fix tests for lib - Impoved the fix for PT-2016, so it does not files with keys with USING keyword - Added brackets to expression in lib/TableNibbler.pm, so it does not crap query wit many indexes with OR keyword - Adjusted test t/lib/TableNibbler.t, so it reflects above chages - Modified lib/Cxn.pm, so it has workaround for perl5-dbi/DBD-mysql#306 , introduced in DBD::mysql 4.0.50 - Updated tests: added debugging code and cleanups - Updated modules for tools
1 parent 46a466e commit 642253c

30 files changed

+468
-234
lines changed

bin/pt-archiver

+24-13
Original file line numberDiff line numberDiff line change
@@ -2139,8 +2139,7 @@ sub get_keys {
21392139
my $clustered_key = undef;
21402140

21412141
KEY:
2142-
foreach my $key ( $ddl =~ m/^ ((?:[A-Z]+ )?KEY \(?`[\s\S]*?`\)[\s\S]*?,?)$/gm ) {
2143-
2142+
foreach my $key ( $ddl =~ m/^ ((?:[A-Z]+ )?KEY [\s\S]*?\),?.*)$/gm ) {
21442143
next KEY if $key =~ m/FOREIGN/;
21452144

21462145
my $key_ddl = $key;
@@ -2150,7 +2149,7 @@ sub get_keys {
21502149
$key =~ s/USING HASH/USING BTREE/;
21512150
}
21522151

2153-
my ( $type, $cols ) = $key =~ m/(?:USING (\w+))? \(([\s\S]+?)\)/;
2152+
my ( $type, $cols ) = $key =~ m/(?:USING (\w+))? \(([\s\S]+)\)/;
21542153
my ( $special ) = $key =~ m/(FULLTEXT|SPATIAL)/;
21552154
$type = $type || $special || 'BTREE';
21562155
my ($name) = $key =~ m/(PRIMARY|`[^`]*`)/;
@@ -3221,7 +3220,7 @@ sub generate_cmp_where {
32213220
push @clause, "($val IS NULL OR $quo $type $val)";
32223221
}
32233222
elsif ( $type =~ m/>/ ) {
3224-
push @clause, "($val IS NULL AND $quo IS NOT NULL) OR ($quo $cmp $val)";
3223+
push @clause, "(($val IS NULL AND $quo IS NOT NULL) OR ($quo $cmp $val))";
32253224
}
32263225
else { # If $type =~ m/</ ) {
32273226
push @clauses, "(($val IS NOT NULL AND $quo IS NULL) OR ($quo $cmp $val))";
@@ -4601,7 +4600,7 @@ sub connect {
46014600
my $dp = $self->{DSNParser};
46024601

46034602
my $dbh = $self->{dbh};
4604-
if ( !$dbh || !$dbh->ping() ) {
4603+
if ( !$dbh || ( $dbh && $self->{dbh_set} && !$self->_ping($dbh) ) ) {
46054604
if ( $self->{ask_pass} && !$self->{asked_for_pass} && !defined $dsn->{p} ) {
46064605
$dsn->{p} = OptionParser::prompt_noecho("Enter MySQL password: ");
46074606
$self->{asked_for_pass} = 1;
@@ -4630,19 +4629,20 @@ sub connect {
46304629
sub set_dbh {
46314630
my ($self, $dbh) = @_;
46324631

4633-
if ( $self->{dbh} && $self->{dbh} == $dbh && $self->{dbh_set} ) {
4634-
PTDEBUG && _d($dbh, 'Already set dbh');
4635-
return $dbh;
4636-
}
4637-
46384632
PTDEBUG && _d($dbh, 'Setting dbh');
46394633

46404634
$dbh->{FetchHashKeyName} = 'NAME_lc' if $self->{NAME_lc};
46414635

4642-
my $sql = 'SELECT @@server_id /*!50038 , @@hostname*/';
4636+
my $sql = 'SELECT @@server_id /*!50038 , @@hostname*/, CONNECTION_ID() as connection_id';
46434637
PTDEBUG && _d($dbh, $sql);
4644-
my ($server_id, $hostname) = $dbh->selectrow_array($sql);
4638+
my ($server_id, $hostname, $connection_id) = $dbh->selectrow_array($sql);
46454639
PTDEBUG && _d($dbh, 'hostname:', $hostname, $server_id);
4640+
4641+
if ( $self->{dbh} && $self->{dbh} == $dbh && $self->{dbh_set} && $self->{dbh_set} == $connection_id) {
4642+
PTDEBUG && _d($dbh, 'Already set dbh');
4643+
return $dbh;
4644+
}
4645+
46464646
if ( $hostname ) {
46474647
$self->{hostname} = $hostname;
46484648
}
@@ -4657,7 +4657,7 @@ sub set_dbh {
46574657
}
46584658

46594659
$self->{dbh} = $dbh;
4660-
$self->{dbh_set} = 1;
4660+
$self->{dbh_set} = $connection_id;
46614661
return $dbh;
46624662
}
46634663

@@ -4783,6 +4783,17 @@ sub DESTROY {
47834783
return;
47844784
}
47854785

4786+
sub _ping() {
4787+
my ( $self, $dbh ) = @_;
4788+
if (!$dbh->ping()) {
4789+
return 0;
4790+
}
4791+
my $sql = 'SELECT CONNECTION_ID() as connection_id';
4792+
PTDEBUG && _d($dbh, $sql);
4793+
my ($connection_id) = $dbh->selectrow_array($sql);
4794+
return $self->{dbh_set} == $connection_id;
4795+
}
4796+
47864797
sub _d {
47874798
my ($package, undef, $line) = caller 0;
47884799
@_ = map { (my $temp = $_) =~ s/\n/\n# /g; $temp; }

bin/pt-config-diff

+21-9
Original file line numberDiff line numberDiff line change
@@ -2368,7 +2368,7 @@ sub connect {
23682368
my $dp = $self->{DSNParser};
23692369

23702370
my $dbh = $self->{dbh};
2371-
if ( !$dbh || !$dbh->ping() ) {
2371+
if ( !$dbh || ( $dbh && $self->{dbh_set} && !$self->_ping($dbh) ) ) {
23722372
if ( $self->{ask_pass} && !$self->{asked_for_pass} && !defined $dsn->{p} ) {
23732373
$dsn->{p} = OptionParser::prompt_noecho("Enter MySQL password: ");
23742374
$self->{asked_for_pass} = 1;
@@ -2397,19 +2397,20 @@ sub connect {
23972397
sub set_dbh {
23982398
my ($self, $dbh) = @_;
23992399

2400-
if ( $self->{dbh} && $self->{dbh} == $dbh && $self->{dbh_set} ) {
2401-
PTDEBUG && _d($dbh, 'Already set dbh');
2402-
return $dbh;
2403-
}
2404-
24052400
PTDEBUG && _d($dbh, 'Setting dbh');
24062401

24072402
$dbh->{FetchHashKeyName} = 'NAME_lc' if $self->{NAME_lc};
24082403

2409-
my $sql = 'SELECT @@server_id /*!50038 , @@hostname*/';
2404+
my $sql = 'SELECT @@server_id /*!50038 , @@hostname*/, CONNECTION_ID() as connection_id';
24102405
PTDEBUG && _d($dbh, $sql);
2411-
my ($server_id, $hostname) = $dbh->selectrow_array($sql);
2406+
my ($server_id, $hostname, $connection_id) = $dbh->selectrow_array($sql);
24122407
PTDEBUG && _d($dbh, 'hostname:', $hostname, $server_id);
2408+
2409+
if ( $self->{dbh} && $self->{dbh} == $dbh && $self->{dbh_set} && $self->{dbh_set} == $connection_id) {
2410+
PTDEBUG && _d($dbh, 'Already set dbh');
2411+
return $dbh;
2412+
}
2413+
24132414
if ( $hostname ) {
24142415
$self->{hostname} = $hostname;
24152416
}
@@ -2424,7 +2425,7 @@ sub set_dbh {
24242425
}
24252426

24262427
$self->{dbh} = $dbh;
2427-
$self->{dbh_set} = 1;
2428+
$self->{dbh_set} = $connection_id;
24282429
return $dbh;
24292430
}
24302431

@@ -2550,6 +2551,17 @@ sub DESTROY {
25502551
return;
25512552
}
25522553

2554+
sub _ping() {
2555+
my ( $self, $dbh ) = @_;
2556+
if (!$dbh->ping()) {
2557+
return 0;
2558+
}
2559+
my $sql = 'SELECT CONNECTION_ID() as connection_id';
2560+
PTDEBUG && _d($dbh, $sql);
2561+
my ($connection_id) = $dbh->selectrow_array($sql);
2562+
return $self->{dbh_set} == $connection_id;
2563+
}
2564+
25532565
sub _d {
25542566
my ($package, undef, $line) = caller 0;
25552567
@_ = map { (my $temp = $_) =~ s/\n/\n# /g; $temp; }

bin/pt-deadlock-logger

+21-9
Original file line numberDiff line numberDiff line change
@@ -2712,7 +2712,7 @@ sub connect {
27122712
my $dp = $self->{DSNParser};
27132713

27142714
my $dbh = $self->{dbh};
2715-
if ( !$dbh || !$dbh->ping() ) {
2715+
if ( !$dbh || ( $dbh && $self->{dbh_set} && !$self->_ping($dbh) ) ) {
27162716
if ( $self->{ask_pass} && !$self->{asked_for_pass} && !defined $dsn->{p} ) {
27172717
$dsn->{p} = OptionParser::prompt_noecho("Enter MySQL password: ");
27182718
$self->{asked_for_pass} = 1;
@@ -2741,19 +2741,20 @@ sub connect {
27412741
sub set_dbh {
27422742
my ($self, $dbh) = @_;
27432743

2744-
if ( $self->{dbh} && $self->{dbh} == $dbh && $self->{dbh_set} ) {
2745-
PTDEBUG && _d($dbh, 'Already set dbh');
2746-
return $dbh;
2747-
}
2748-
27492744
PTDEBUG && _d($dbh, 'Setting dbh');
27502745

27512746
$dbh->{FetchHashKeyName} = 'NAME_lc' if $self->{NAME_lc};
27522747

2753-
my $sql = 'SELECT @@server_id /*!50038 , @@hostname*/';
2748+
my $sql = 'SELECT @@server_id /*!50038 , @@hostname*/, CONNECTION_ID() as connection_id';
27542749
PTDEBUG && _d($dbh, $sql);
2755-
my ($server_id, $hostname) = $dbh->selectrow_array($sql);
2750+
my ($server_id, $hostname, $connection_id) = $dbh->selectrow_array($sql);
27562751
PTDEBUG && _d($dbh, 'hostname:', $hostname, $server_id);
2752+
2753+
if ( $self->{dbh} && $self->{dbh} == $dbh && $self->{dbh_set} && $self->{dbh_set} == $connection_id) {
2754+
PTDEBUG && _d($dbh, 'Already set dbh');
2755+
return $dbh;
2756+
}
2757+
27572758
if ( $hostname ) {
27582759
$self->{hostname} = $hostname;
27592760
}
@@ -2768,7 +2769,7 @@ sub set_dbh {
27682769
}
27692770

27702771
$self->{dbh} = $dbh;
2771-
$self->{dbh_set} = 1;
2772+
$self->{dbh_set} = $connection_id;
27722773
return $dbh;
27732774
}
27742775

@@ -2894,6 +2895,17 @@ sub DESTROY {
28942895
return;
28952896
}
28962897

2898+
sub _ping() {
2899+
my ( $self, $dbh ) = @_;
2900+
if (!$dbh->ping()) {
2901+
return 0;
2902+
}
2903+
my $sql = 'SELECT CONNECTION_ID() as connection_id';
2904+
PTDEBUG && _d($dbh, $sql);
2905+
my ($connection_id) = $dbh->selectrow_array($sql);
2906+
return $self->{dbh_set} == $connection_id;
2907+
}
2908+
28972909
sub _d {
28982910
my ($package, undef, $line) = caller 0;
28992911
@_ = map { (my $temp = $_) =~ s/\n/\n# /g; $temp; }

bin/pt-duplicate-key-checker

+3-4
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ sub parse {
345345

346346
my $engine = $self->get_engine($ddl);
347347

348-
my @defs = $ddl =~ m/^(\s+`.*?),?$/gm;
348+
my @defs = $ddl =~ m/(?:(?<=,\n)|(?<=\(\n))(\s+`(?:.|\n)+?`.+?),?\n/g;
349349
my @cols = map { $_ =~ m/`([^`]+)`/ } @defs;
350350
PTDEBUG && _d('Table cols:', join(', ', map { "`$_`" } @cols));
351351

@@ -526,8 +526,7 @@ sub get_keys {
526526
my $clustered_key = undef;
527527

528528
KEY:
529-
foreach my $key ( $ddl =~ m/^ ((?:[A-Z]+ )?KEY .*)$/gm ) {
530-
529+
foreach my $key ( $ddl =~ m/^ ((?:[A-Z]+ )?KEY [\s\S]*?\),?.*)$/gm ) {
531530
next KEY if $key =~ m/FOREIGN/;
532531

533532
my $key_ddl = $key;
@@ -537,7 +536,7 @@ sub get_keys {
537536
$key =~ s/USING HASH/USING BTREE/;
538537
}
539538

540-
my ( $type, $cols ) = $key =~ m/(?:USING (\w+))? \((.+)\)/;
539+
my ( $type, $cols ) = $key =~ m/(?:USING (\w+))? \(([\s\S]+)\)/;
541540
my ( $special ) = $key =~ m/(FULLTEXT|SPATIAL)/;
542541
$type = $type || $special || 'BTREE';
543542
my ($name) = $key =~ m/(PRIMARY|`[^`]*`)/;

bin/pt-find

+3-4
Original file line numberDiff line numberDiff line change
@@ -1897,7 +1897,7 @@ sub parse {
18971897

18981898
my $engine = $self->get_engine($ddl);
18991899

1900-
my @defs = $ddl =~ m/^(\s+`.*?),?$/gm;
1900+
my @defs = $ddl =~ m/(?:(?<=,\n)|(?<=\(\n))(\s+`(?:.|\n)+?`.+?),?\n/g;
19011901
my @cols = map { $_ =~ m/`([^`]+)`/ } @defs;
19021902
PTDEBUG && _d('Table cols:', join(', ', map { "`$_`" } @cols));
19031903

@@ -2078,8 +2078,7 @@ sub get_keys {
20782078
my $clustered_key = undef;
20792079

20802080
KEY:
2081-
foreach my $key ( $ddl =~ m/^ ((?:[A-Z]+ )?KEY .*)$/gm ) {
2082-
2081+
foreach my $key ( $ddl =~ m/^ ((?:[A-Z]+ )?KEY [\s\S]*?\),?.*)$/gm ) {
20832082
next KEY if $key =~ m/FOREIGN/;
20842083

20852084
my $key_ddl = $key;
@@ -2089,7 +2088,7 @@ sub get_keys {
20892088
$key =~ s/USING HASH/USING BTREE/;
20902089
}
20912090

2092-
my ( $type, $cols ) = $key =~ m/(?:USING (\w+))? \((.+)\)/;
2091+
my ( $type, $cols ) = $key =~ m/(?:USING (\w+))? \(([\s\S]+)\)/;
20932092
my ( $special ) = $key =~ m/(FULLTEXT|SPATIAL)/;
20942093
$type = $type || $special || 'BTREE';
20952094
my ($name) = $key =~ m/(PRIMARY|`[^`]*`)/;

bin/pt-fk-error-logger

+21-9
Original file line numberDiff line numberDiff line change
@@ -1866,7 +1866,7 @@ sub connect {
18661866
my $dp = $self->{DSNParser};
18671867

18681868
my $dbh = $self->{dbh};
1869-
if ( !$dbh || !$dbh->ping() ) {
1869+
if ( !$dbh || ( $dbh && $self->{dbh_set} && !$self->_ping($dbh) ) ) {
18701870
if ( $self->{ask_pass} && !$self->{asked_for_pass} && !defined $dsn->{p} ) {
18711871
$dsn->{p} = OptionParser::prompt_noecho("Enter MySQL password: ");
18721872
$self->{asked_for_pass} = 1;
@@ -1895,19 +1895,20 @@ sub connect {
18951895
sub set_dbh {
18961896
my ($self, $dbh) = @_;
18971897

1898-
if ( $self->{dbh} && $self->{dbh} == $dbh && $self->{dbh_set} ) {
1899-
PTDEBUG && _d($dbh, 'Already set dbh');
1900-
return $dbh;
1901-
}
1902-
19031898
PTDEBUG && _d($dbh, 'Setting dbh');
19041899

19051900
$dbh->{FetchHashKeyName} = 'NAME_lc' if $self->{NAME_lc};
19061901

1907-
my $sql = 'SELECT @@server_id /*!50038 , @@hostname*/';
1902+
my $sql = 'SELECT @@server_id /*!50038 , @@hostname*/, CONNECTION_ID() as connection_id';
19081903
PTDEBUG && _d($dbh, $sql);
1909-
my ($server_id, $hostname) = $dbh->selectrow_array($sql);
1904+
my ($server_id, $hostname, $connection_id) = $dbh->selectrow_array($sql);
19101905
PTDEBUG && _d($dbh, 'hostname:', $hostname, $server_id);
1906+
1907+
if ( $self->{dbh} && $self->{dbh} == $dbh && $self->{dbh_set} && $self->{dbh_set} == $connection_id) {
1908+
PTDEBUG && _d($dbh, 'Already set dbh');
1909+
return $dbh;
1910+
}
1911+
19111912
if ( $hostname ) {
19121913
$self->{hostname} = $hostname;
19131914
}
@@ -1922,7 +1923,7 @@ sub set_dbh {
19221923
}
19231924

19241925
$self->{dbh} = $dbh;
1925-
$self->{dbh_set} = 1;
1926+
$self->{dbh_set} = $connection_id;
19261927
return $dbh;
19271928
}
19281929

@@ -2048,6 +2049,17 @@ sub DESTROY {
20482049
return;
20492050
}
20502051

2052+
sub _ping() {
2053+
my ( $self, $dbh ) = @_;
2054+
if (!$dbh->ping()) {
2055+
return 0;
2056+
}
2057+
my $sql = 'SELECT CONNECTION_ID() as connection_id';
2058+
PTDEBUG && _d($dbh, $sql);
2059+
my ($connection_id) = $dbh->selectrow_array($sql);
2060+
return $self->{dbh_set} == $connection_id;
2061+
}
2062+
20512063
sub _d {
20522064
my ($package, undef, $line) = caller 0;
20532065
@_ = map { (my $temp = $_) =~ s/\n/\n# /g; $temp; }

0 commit comments

Comments
 (0)