Skip to content

Commit 59b80af

Browse files
committed
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 51bc8c2 commit 59b80af

25 files changed

+342
-165
lines changed

bin/pt-archiver

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

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

21452145
my $key_ddl = $key;
@@ -2149,7 +2149,7 @@ sub get_keys {
21492149
$key =~ s/USING HASH/USING BTREE/;
21502150
}
21512151

2152-
my ( $type, $cols ) = $key =~ m/(?:USING (\w+))? \(([\s\S]+)\)/;
2152+
my ( $type, $cols ) = $key =~ m/(?:USING (\w+))? \(([\s\S]+)\)/;
21532153
my ( $special ) = $key =~ m/(FULLTEXT|SPATIAL)/;
21542154
$type = $type || $special || 'BTREE';
21552155
my ($name) = $key =~ m/(PRIMARY|`[^`]*`)/;
@@ -3220,7 +3220,7 @@ sub generate_cmp_where {
32203220
push @clause, "($val IS NULL OR $quo $type $val)";
32213221
}
32223222
elsif ( $type =~ m/>/ ) {
3223-
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))";
32243224
}
32253225
else { # If $type =~ m/</ ) {
32263226
push @clauses, "(($val IS NOT NULL AND $quo IS NULL) OR ($quo $cmp $val))";
@@ -4600,7 +4600,7 @@ sub connect {
46004600
my $dp = $self->{DSNParser};
46014601

46024602
my $dbh = $self->{dbh};
4603-
if ( !$dbh || !$dbh->ping() ) {
4603+
if ( !$dbh || ( $dbh && $self->{dbh_set} && !$self->_ping($dbh) ) ) {
46044604
if ( $self->{ask_pass} && !$self->{asked_for_pass} && !defined $dsn->{p} ) {
46054605
$dsn->{p} = OptionParser::prompt_noecho("Enter MySQL password: ");
46064606
$self->{asked_for_pass} = 1;
@@ -4629,19 +4629,20 @@ sub connect {
46294629
sub set_dbh {
46304630
my ($self, $dbh) = @_;
46314631

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

46394634
$dbh->{FetchHashKeyName} = 'NAME_lc' if $self->{NAME_lc};
46404635

4641-
my $sql = 'SELECT @@server_id /*!50038 , @@hostname*/';
4636+
my $sql = 'SELECT @@server_id /*!50038 , @@hostname*/, CONNECTION_ID() as connection_id';
46424637
PTDEBUG && _d($dbh, $sql);
4643-
my ($server_id, $hostname) = $dbh->selectrow_array($sql);
4638+
my ($server_id, $hostname, $connection_id) = $dbh->selectrow_array($sql);
46444639
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+
46454646
if ( $hostname ) {
46464647
$self->{hostname} = $hostname;
46474648
}
@@ -4656,7 +4657,7 @@ sub set_dbh {
46564657
}
46574658

46584659
$self->{dbh} = $dbh;
4659-
$self->{dbh_set} = 1;
4660+
$self->{dbh_set} = $connection_id;
46604661
return $dbh;
46614662
}
46624663

@@ -4782,6 +4783,17 @@ sub DESTROY {
47824783
return;
47834784
}
47844785

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+
47854797
sub _d {
47864798
my ($package, undef, $line) = caller 0;
47874799
@_ = 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

+1-1
Original file line numberDiff line numberDiff line change
@@ -526,7 +526,7 @@ sub get_keys {
526526
my $clustered_key = undef;
527527

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

532532
my $key_ddl = $key;

bin/pt-find

+1-1
Original file line numberDiff line numberDiff line change
@@ -2078,7 +2078,7 @@ sub get_keys {
20782078
my $clustered_key = undef;
20792079

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

20842084
my $key_ddl = $key;

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; }

bin/pt-heartbeat

+1-1
Original file line numberDiff line numberDiff line change
@@ -3819,7 +3819,7 @@ sub get_keys {
38193819
my $clustered_key = undef;
38203820

38213821
KEY:
3822-
foreach my $key ( $ddl =~ m/^ ((?:[A-Z]+ )?KEY [\s\S]*?)$/gm ) {
3822+
foreach my $key ( $ddl =~ m/^ ((?:[A-Z]+ )?KEY [\s\S]*?\),?.*)$/gm ) {
38233823
next KEY if $key =~ m/FOREIGN/;
38243824

38253825
my $key_ddl = $key;

bin/pt-index-usage

+1-1
Original file line numberDiff line numberDiff line change
@@ -3330,7 +3330,7 @@ sub get_keys {
33303330
my $clustered_key = undef;
33313331

33323332
KEY:
3333-
foreach my $key ( $ddl =~ m/^ ((?:[A-Z]+ )?KEY [\s\S]*?)$/gm ) {
3333+
foreach my $key ( $ddl =~ m/^ ((?:[A-Z]+ )?KEY [\s\S]*?\),?.*)$/gm ) {
33343334
next KEY if $key =~ m/FOREIGN/;
33353335

33363336
my $key_ddl = $key;

bin/pt-kill

+22-10
Original file line numberDiff line numberDiff line change
@@ -3193,7 +3193,7 @@ sub get_keys {
31933193
my $clustered_key = undef;
31943194

31953195
KEY:
3196-
foreach my $key ( $ddl =~ m/^ ((?:[A-Z]+ )?KEY [\s\S]*?)$/gm ) {
3196+
foreach my $key ( $ddl =~ m/^ ((?:[A-Z]+ )?KEY [\s\S]*?\),?.*)$/gm ) {
31973197
next KEY if $key =~ m/FOREIGN/;
31983198

31993199
my $key_ddl = $key;
@@ -5415,7 +5415,7 @@ sub connect {
54155415
my $dp = $self->{DSNParser};
54165416

54175417
my $dbh = $self->{dbh};
5418-
if ( !$dbh || !$dbh->ping() ) {
5418+
if ( !$dbh || ( $dbh && $self->{dbh_set} && !$self->_ping($dbh) ) ) {
54195419
if ( $self->{ask_pass} && !$self->{asked_for_pass} && !defined $dsn->{p} ) {
54205420
$dsn->{p} = OptionParser::prompt_noecho("Enter MySQL password: ");
54215421
$self->{asked_for_pass} = 1;
@@ -5444,19 +5444,20 @@ sub connect {
54445444
sub set_dbh {
54455445
my ($self, $dbh) = @_;
54465446

5447-
if ( $self->{dbh} && $self->{dbh} == $dbh && $self->{dbh_set} ) {
5448-
PTDEBUG && _d($dbh, 'Already set dbh');
5449-
return $dbh;
5450-
}
5451-
54525447
PTDEBUG && _d($dbh, 'Setting dbh');
54535448

54545449
$dbh->{FetchHashKeyName} = 'NAME_lc' if $self->{NAME_lc};
54555450

5456-
my $sql = 'SELECT @@server_id /*!50038 , @@hostname*/';
5451+
my $sql = 'SELECT @@server_id /*!50038 , @@hostname*/, CONNECTION_ID() as connection_id';
54575452
PTDEBUG && _d($dbh, $sql);
5458-
my ($server_id, $hostname) = $dbh->selectrow_array($sql);
5453+
my ($server_id, $hostname, $connection_id) = $dbh->selectrow_array($sql);
54595454
PTDEBUG && _d($dbh, 'hostname:', $hostname, $server_id);
5455+
5456+
if ( $self->{dbh} && $self->{dbh} == $dbh && $self->{dbh_set} && $self->{dbh_set} == $connection_id) {
5457+
PTDEBUG && _d($dbh, 'Already set dbh');
5458+
return $dbh;
5459+
}
5460+
54605461
if ( $hostname ) {
54615462
$self->{hostname} = $hostname;
54625463
}
@@ -5471,7 +5472,7 @@ sub set_dbh {
54715472
}
54725473

54735474
$self->{dbh} = $dbh;
5474-
$self->{dbh_set} = 1;
5475+
$self->{dbh_set} = $connection_id;
54755476
return $dbh;
54765477
}
54775478

@@ -5597,6 +5598,17 @@ sub DESTROY {
55975598
return;
55985599
}
55995600

5601+
sub _ping() {
5602+
my ( $self, $dbh ) = @_;
5603+
if (!$dbh->ping()) {
5604+
return 0;
5605+
}
5606+
my $sql = 'SELECT CONNECTION_ID() as connection_id';
5607+
PTDEBUG && _d($dbh, $sql);
5608+
my ($connection_id) = $dbh->selectrow_array($sql);
5609+
return $self->{dbh_set} == $connection_id;
5610+
}
5611+
56005612
sub _d {
56015613
my ($package, undef, $line) = caller 0;
56025614
@_ = map { (my $temp = $_) =~ s/\n/\n# /g; $temp; }

0 commit comments

Comments
 (0)