Fix lib/Cxn.pm that didn't merge correctly. Fix t/lib/OptionParser.t test failure. Update Cxn, OptionParser, and DSNParser in all tools.

This commit is contained in:
Daniel Nichter
2013-03-13 12:38:45 -06:00
parent 35683d5261
commit 5ddfb95ca7
9 changed files with 137 additions and 59 deletions

View File

@@ -2223,15 +2223,19 @@ sub new {
$dsn = $dp->copy($prev_dsn, $dsn); $dsn = $dp->copy($prev_dsn, $dsn);
} }
my $dsn_name = $dp->as_string($dsn, [qw(h P S)])
|| $dp->as_string($dsn, [qw(F)])
|| '';
my $self = { my $self = {
dsn => $dsn, dsn => $dsn,
dbh => $args{dbh}, dbh => $args{dbh},
dsn_name => $dp->as_string($dsn, [qw(h P S)]), dsn_name => $dsn_name,
hostname => '', hostname => '',
set => $args{set}, set => $args{set},
NAME_lc => defined($args{NAME_lc}) ? $args{NAME_lc} : 1, NAME_lc => defined($args{NAME_lc}) ? $args{NAME_lc} : 1,
dbh_set => 0, dbh_set => 0,
OptionParser => $o, ask_pass => $o->get('ask-pass'),
DSNParser => $dp, DSNParser => $dp,
is_cluster_node => undef, is_cluster_node => undef,
parent => $args{parent}, parent => $args{parent},
@@ -2244,11 +2248,10 @@ sub connect {
my ( $self, %opts ) = @_; my ( $self, %opts ) = @_;
my $dsn = $self->{dsn}; my $dsn = $self->{dsn};
my $dp = $self->{DSNParser}; my $dp = $self->{DSNParser};
my $o = $self->{OptionParser};
my $dbh = $self->{dbh}; my $dbh = $self->{dbh};
if ( !$dbh || !$dbh->ping() ) { if ( !$dbh || !$dbh->ping() ) {
if ( $o->get('ask-pass') && !$self->{asked_for_pass} ) { if ( $self->{ask_pass} && !$self->{asked_for_pass} ) {
$dsn->{p} = OptionParser::prompt_noecho("Enter MySQL password: "); $dsn->{p} = OptionParser::prompt_noecho("Enter MySQL password: ");
$self->{asked_for_pass} = 1; $self->{asked_for_pass} = 1;
} }

View File

@@ -2567,15 +2567,19 @@ sub new {
$dsn = $dp->copy($prev_dsn, $dsn); $dsn = $dp->copy($prev_dsn, $dsn);
} }
my $dsn_name = $dp->as_string($dsn, [qw(h P S)])
|| $dp->as_string($dsn, [qw(F)])
|| '';
my $self = { my $self = {
dsn => $dsn, dsn => $dsn,
dbh => $args{dbh}, dbh => $args{dbh},
dsn_name => $dp->as_string($dsn, [qw(h P S)]), dsn_name => $dsn_name,
hostname => '', hostname => '',
set => $args{set}, set => $args{set},
NAME_lc => defined($args{NAME_lc}) ? $args{NAME_lc} : 1, NAME_lc => defined($args{NAME_lc}) ? $args{NAME_lc} : 1,
dbh_set => 0, dbh_set => 0,
OptionParser => $o, ask_pass => $o->get('ask-pass'),
DSNParser => $dp, DSNParser => $dp,
is_cluster_node => undef, is_cluster_node => undef,
parent => $args{parent}, parent => $args{parent},
@@ -2588,11 +2592,10 @@ sub connect {
my ( $self, %opts ) = @_; my ( $self, %opts ) = @_;
my $dsn = $self->{dsn}; my $dsn = $self->{dsn};
my $dp = $self->{DSNParser}; my $dp = $self->{DSNParser};
my $o = $self->{OptionParser};
my $dbh = $self->{dbh}; my $dbh = $self->{dbh};
if ( !$dbh || !$dbh->ping() ) { if ( !$dbh || !$dbh->ping() ) {
if ( $o->get('ask-pass') && !$self->{asked_for_pass} ) { if ( $self->{ask_pass} && !$self->{asked_for_pass} ) {
$dsn->{p} = OptionParser::prompt_noecho("Enter MySQL password: "); $dsn->{p} = OptionParser::prompt_noecho("Enter MySQL password: ");
$self->{asked_for_pass} = 1; $self->{asked_for_pass} = 1;
} }

View File

@@ -1724,15 +1724,19 @@ sub new {
$dsn = $dp->copy($prev_dsn, $dsn); $dsn = $dp->copy($prev_dsn, $dsn);
} }
my $dsn_name = $dp->as_string($dsn, [qw(h P S)])
|| $dp->as_string($dsn, [qw(F)])
|| '';
my $self = { my $self = {
dsn => $dsn, dsn => $dsn,
dbh => $args{dbh}, dbh => $args{dbh},
dsn_name => $dp->as_string($dsn, [qw(h P S)]), dsn_name => $dsn_name,
hostname => '', hostname => '',
set => $args{set}, set => $args{set},
NAME_lc => defined($args{NAME_lc}) ? $args{NAME_lc} : 1, NAME_lc => defined($args{NAME_lc}) ? $args{NAME_lc} : 1,
dbh_set => 0, dbh_set => 0,
OptionParser => $o, ask_pass => $o->get('ask-pass'),
DSNParser => $dp, DSNParser => $dp,
is_cluster_node => undef, is_cluster_node => undef,
parent => $args{parent}, parent => $args{parent},
@@ -1745,11 +1749,10 @@ sub connect {
my ( $self, %opts ) = @_; my ( $self, %opts ) = @_;
my $dsn = $self->{dsn}; my $dsn = $self->{dsn};
my $dp = $self->{DSNParser}; my $dp = $self->{DSNParser};
my $o = $self->{OptionParser};
my $dbh = $self->{dbh}; my $dbh = $self->{dbh};
if ( !$dbh || !$dbh->ping() ) { if ( !$dbh || !$dbh->ping() ) {
if ( $o->get('ask-pass') && !$self->{asked_for_pass} ) { if ( $self->{ask_pass} && !$self->{asked_for_pass} ) {
$dsn->{p} = OptionParser::prompt_noecho("Enter MySQL password: "); $dsn->{p} = OptionParser::prompt_noecho("Enter MySQL password: ");
$self->{asked_for_pass} = 1; $self->{asked_for_pass} = 1;
} }

View File

@@ -5054,15 +5054,19 @@ sub new {
$dsn = $dp->copy($prev_dsn, $dsn); $dsn = $dp->copy($prev_dsn, $dsn);
} }
my $dsn_name = $dp->as_string($dsn, [qw(h P S)])
|| $dp->as_string($dsn, [qw(F)])
|| '';
my $self = { my $self = {
dsn => $dsn, dsn => $dsn,
dbh => $args{dbh}, dbh => $args{dbh},
dsn_name => $dp->as_string($dsn, [qw(h P S)]), dsn_name => $dsn_name,
hostname => '', hostname => '',
set => $args{set}, set => $args{set},
NAME_lc => defined($args{NAME_lc}) ? $args{NAME_lc} : 1, NAME_lc => defined($args{NAME_lc}) ? $args{NAME_lc} : 1,
dbh_set => 0, dbh_set => 0,
OptionParser => $o, ask_pass => $o->get('ask-pass'),
DSNParser => $dp, DSNParser => $dp,
is_cluster_node => undef, is_cluster_node => undef,
parent => $args{parent}, parent => $args{parent},
@@ -5075,11 +5079,10 @@ sub connect {
my ( $self, %opts ) = @_; my ( $self, %opts ) = @_;
my $dsn = $self->{dsn}; my $dsn = $self->{dsn};
my $dp = $self->{DSNParser}; my $dp = $self->{DSNParser};
my $o = $self->{OptionParser};
my $dbh = $self->{dbh}; my $dbh = $self->{dbh};
if ( !$dbh || !$dbh->ping() ) { if ( !$dbh || !$dbh->ping() ) {
if ( $o->get('ask-pass') && !$self->{asked_for_pass} ) { if ( $self->{ask_pass} && !$self->{asked_for_pass} ) {
$dsn->{p} = OptionParser::prompt_noecho("Enter MySQL password: "); $dsn->{p} = OptionParser::prompt_noecho("Enter MySQL password: ");
$self->{asked_for_pass} = 1; $self->{asked_for_pass} = 1;
} }

View File

@@ -3683,15 +3683,19 @@ sub new {
$dsn = $dp->copy($prev_dsn, $dsn); $dsn = $dp->copy($prev_dsn, $dsn);
} }
my $dsn_name = $dp->as_string($dsn, [qw(h P S)])
|| $dp->as_string($dsn, [qw(F)])
|| '';
my $self = { my $self = {
dsn => $dsn, dsn => $dsn,
dbh => $args{dbh}, dbh => $args{dbh},
dsn_name => $dp->as_string($dsn, [qw(h P S)]), dsn_name => $dsn_name,
hostname => '', hostname => '',
set => $args{set}, set => $args{set},
NAME_lc => defined($args{NAME_lc}) ? $args{NAME_lc} : 1, NAME_lc => defined($args{NAME_lc}) ? $args{NAME_lc} : 1,
dbh_set => 0, dbh_set => 0,
OptionParser => $o, ask_pass => $o->get('ask-pass'),
DSNParser => $dp, DSNParser => $dp,
is_cluster_node => undef, is_cluster_node => undef,
parent => $args{parent}, parent => $args{parent},
@@ -3704,11 +3708,10 @@ sub connect {
my ( $self, %opts ) = @_; my ( $self, %opts ) = @_;
my $dsn = $self->{dsn}; my $dsn = $self->{dsn};
my $dp = $self->{DSNParser}; my $dp = $self->{DSNParser};
my $o = $self->{OptionParser};
my $dbh = $self->{dbh}; my $dbh = $self->{dbh};
if ( !$dbh || !$dbh->ping() ) { if ( !$dbh || !$dbh->ping() ) {
if ( $o->get('ask-pass') && !$self->{asked_for_pass} ) { if ( $self->{ask_pass} && !$self->{asked_for_pass} ) {
$dsn->{p} = OptionParser::prompt_noecho("Enter MySQL password: "); $dsn->{p} = OptionParser::prompt_noecho("Enter MySQL password: ");
$self->{asked_for_pass} = 1; $self->{asked_for_pass} = 1;
} }

View File

@@ -3461,15 +3461,19 @@ sub new {
$dsn = $dp->copy($prev_dsn, $dsn); $dsn = $dp->copy($prev_dsn, $dsn);
} }
my $dsn_name = $dp->as_string($dsn, [qw(h P S)])
|| $dp->as_string($dsn, [qw(F)])
|| '';
my $self = { my $self = {
dsn => $dsn, dsn => $dsn,
dbh => $args{dbh}, dbh => $args{dbh},
dsn_name => $dp->as_string($dsn, [qw(h P S)]), dsn_name => $dsn_name,
hostname => '', hostname => '',
set => $args{set}, set => $args{set},
NAME_lc => defined($args{NAME_lc}) ? $args{NAME_lc} : 1, NAME_lc => defined($args{NAME_lc}) ? $args{NAME_lc} : 1,
dbh_set => 0, dbh_set => 0,
OptionParser => $o, ask_pass => $o->get('ask-pass'),
DSNParser => $dp, DSNParser => $dp,
is_cluster_node => undef, is_cluster_node => undef,
parent => $args{parent}, parent => $args{parent},
@@ -3482,11 +3486,10 @@ sub connect {
my ( $self, %opts ) = @_; my ( $self, %opts ) = @_;
my $dsn = $self->{dsn}; my $dsn = $self->{dsn};
my $dp = $self->{DSNParser}; my $dp = $self->{DSNParser};
my $o = $self->{OptionParser};
my $dbh = $self->{dbh}; my $dbh = $self->{dbh};
if ( !$dbh || !$dbh->ping() ) { if ( !$dbh || !$dbh->ping() ) {
if ( $o->get('ask-pass') && !$self->{asked_for_pass} ) { if ( $self->{ask_pass} && !$self->{asked_for_pass} ) {
$dsn->{p} = OptionParser::prompt_noecho("Enter MySQL password: "); $dsn->{p} = OptionParser::prompt_noecho("Enter MySQL password: ");
$self->{asked_for_pass} = 1; $self->{asked_for_pass} = 1;
} }

View File

@@ -2407,13 +2407,14 @@ sub new {
ask_pass => $o->get('ask-pass'), ask_pass => $o->get('ask-pass'),
DSNParser => $dp, DSNParser => $dp,
is_cluster_node => undef, is_cluster_node => undef,
parent => $args{parent},
}; };
return bless $self, $class; return bless $self, $class;
} }
sub connect { sub connect {
my ( $self ) = @_; my ( $self, %opts ) = @_;
my $dsn = $self->{dsn}; my $dsn = $self->{dsn};
my $dp = $self->{DSNParser}; my $dp = $self->{DSNParser};
@@ -2423,11 +2424,18 @@ sub connect {
$dsn->{p} = OptionParser::prompt_noecho("Enter MySQL password: "); $dsn->{p} = OptionParser::prompt_noecho("Enter MySQL password: ");
$self->{asked_for_pass} = 1; $self->{asked_for_pass} = 1;
} }
$dbh = $dp->get_dbh($dp->get_cxn_params($dsn), { AutoCommit => 1 }); $dbh = $dp->get_dbh(
$dp->get_cxn_params($dsn),
{
AutoCommit => 1,
%opts,
},
);
} }
PTDEBUG && _d($dbh, 'Connected dbh to', $self->{name});
return $self->set_dbh($dbh); $dbh = $self->set_dbh($dbh);
PTDEBUG && _d($dbh, 'Connected dbh to', $self->{hostname},$self->{dsn_name});
return $dbh;
} }
sub set_dbh { sub set_dbh {
@@ -2450,6 +2458,11 @@ sub set_dbh {
$self->{hostname} = $hostname; $self->{hostname} = $hostname;
} }
if ( $self->{parent} ) {
PTDEBUG && _d($dbh, 'Setting InactiveDestroy=1 in parent');
$dbh->{InactiveDestroy} = 1;
}
if ( my $set = $self->{set}) { if ( my $set = $self->{set}) {
$set->($dbh); $set->($dbh);
} }
@@ -2459,6 +2472,13 @@ sub set_dbh {
return $dbh; return $dbh;
} }
sub lost_connection {
my ($self, $e) = @_;
return 0 unless $e;
return $e =~ m/MySQL server has gone away/
|| $e =~ m/Lost connection to MySQL server/;
}
sub dbh { sub dbh {
my ($self) = @_; my ($self) = @_;
return $self->{dbh}; return $self->{dbh};
@@ -2477,12 +2497,21 @@ sub name {
sub DESTROY { sub DESTROY {
my ($self) = @_; my ($self) = @_;
if ( $self->{dbh}
PTDEBUG && _d('Destroying cxn');
if ( $self->{parent} ) {
PTDEBUG && _d($self->{dbh}, 'Not disconnecting dbh in parent');
}
elsif ( $self->{dbh}
&& blessed($self->{dbh}) && blessed($self->{dbh})
&& $self->{dbh}->can("disconnect") ) { && $self->{dbh}->can("disconnect") )
PTDEBUG && _d('Disconnecting dbh', $self->{dbh}, $self->{name}); {
PTDEBUG && _d($self->{dbh}, 'Disconnecting dbh on', $self->{hostname},
$self->{dsn_name});
$self->{dbh}->disconnect(); $self->{dbh}->disconnect();
} }
return; return;
} }

View File

@@ -112,13 +112,14 @@ sub new {
ask_pass => $o->get('ask-pass'), ask_pass => $o->get('ask-pass'),
DSNParser => $dp, DSNParser => $dp,
is_cluster_node => undef, is_cluster_node => undef,
parent => $args{parent},
}; };
return bless $self, $class; return bless $self, $class;
} }
sub connect { sub connect {
my ( $self ) = @_; my ( $self, %opts ) = @_;
my $dsn = $self->{dsn}; my $dsn = $self->{dsn};
my $dp = $self->{DSNParser}; my $dp = $self->{DSNParser};
@@ -129,11 +130,18 @@ sub connect {
$dsn->{p} = OptionParser::prompt_noecho("Enter MySQL password: "); $dsn->{p} = OptionParser::prompt_noecho("Enter MySQL password: ");
$self->{asked_for_pass} = 1; $self->{asked_for_pass} = 1;
} }
$dbh = $dp->get_dbh($dp->get_cxn_params($dsn), { AutoCommit => 1 }); $dbh = $dp->get_dbh(
$dp->get_cxn_params($dsn),
{
AutoCommit => 1,
%opts,
},
);
} }
PTDEBUG && _d($dbh, 'Connected dbh to', $self->{name});
return $self->set_dbh($dbh); $dbh = $self->set_dbh($dbh);
PTDEBUG && _d($dbh, 'Connected dbh to', $self->{hostname},$self->{dsn_name});
return $dbh;
} }
sub set_dbh { sub set_dbh {
@@ -166,6 +174,11 @@ sub set_dbh {
$self->{hostname} = $hostname; $self->{hostname} = $hostname;
} }
if ( $self->{parent} ) {
PTDEBUG && _d($dbh, 'Setting InactiveDestroy=1 in parent');
$dbh->{InactiveDestroy} = 1;
}
# Call the set callback to let the caller SET any MySQL variables. # Call the set callback to let the caller SET any MySQL variables.
if ( my $set = $self->{set}) { if ( my $set = $self->{set}) {
$set->($dbh); $set->($dbh);
@@ -176,6 +189,15 @@ sub set_dbh {
return $dbh; return $dbh;
} }
sub lost_connection {
my ($self, $e) = @_;
return 0 unless $e;
return $e =~ m/MySQL server has gone away/
|| $e =~ m/Lost connection to MySQL server/;
# The 1st pattern means that MySQL itself died or was stopped.
# The 2nd pattern means that our cxn was killed (KILL <id>).
}
# Sub: dbh # Sub: dbh
# Return the cxn's dbh. # Return the cxn's dbh.
sub dbh { sub dbh {
@@ -200,12 +222,21 @@ sub name {
sub DESTROY { sub DESTROY {
my ($self) = @_; my ($self) = @_;
if ( $self->{dbh}
PTDEBUG && _d('Destroying cxn');
if ( $self->{parent} ) {
PTDEBUG && _d($self->{dbh}, 'Not disconnecting dbh in parent');
}
elsif ( $self->{dbh}
&& blessed($self->{dbh}) && blessed($self->{dbh})
&& $self->{dbh}->can("disconnect") ) { && $self->{dbh}->can("disconnect") )
PTDEBUG && _d('Disconnecting dbh', $self->{dbh}, $self->{name}); {
PTDEBUG && _d($self->{dbh}, 'Disconnecting dbh on', $self->{hostname},
$self->{dsn_name});
$self->{dbh}->disconnect(); $self->{dbh}->disconnect();
} }
return; return;
} }

View File

@@ -1980,7 +1980,7 @@ my %synop = $o->_parse_synopsis();
is_deeply( is_deeply(
\%synop, \%synop,
{ {
usage => "pt-archiver [OPTION...] --source DSN --where WHERE", usage => "pt-archiver [OPTIONS] --source DSN --where WHERE",
description => "pt-archiver nibbles records from a MySQL table. The --source and --dest arguments use DSN syntax; if COPY is yes, --dest defaults to the key's value from --source.", description => "pt-archiver nibbles records from a MySQL table. The --source and --dest arguments use DSN syntax; if COPY is yes, --dest defaults to the key's value from --source.",
}, },
"_parse_synopsis() gets usage and description" "_parse_synopsis() gets usage and description"