mirror of
https://github.com/percona/percona-toolkit.git
synced 2025-09-23 21:05:00 +00:00
Add dsn opt to Cxn::connect() to change dsn. Update Cxn in pt-agent.
This commit is contained in:
11
bin/pt-agent
11
bin/pt-agent
@@ -3691,7 +3691,7 @@ sub new {
|
|||||||
|
|
||||||
sub connect {
|
sub connect {
|
||||||
my ( $self, %opts ) = @_;
|
my ( $self, %opts ) = @_;
|
||||||
my $dsn = $self->{dsn};
|
my $dsn = $opts{dsn} || $self->{dsn};
|
||||||
my $dp = $self->{DSNParser};
|
my $dp = $self->{DSNParser};
|
||||||
|
|
||||||
my $dbh = $self->{dbh};
|
my $dbh = $self->{dbh};
|
||||||
@@ -3710,6 +3710,13 @@ sub connect {
|
|||||||
}
|
}
|
||||||
|
|
||||||
$dbh = $self->set_dbh($dbh);
|
$dbh = $self->set_dbh($dbh);
|
||||||
|
if ( $opts{dsn} ) {
|
||||||
|
$self->{dsn} = $dsn;
|
||||||
|
$self->{dsn_name} = $dp->as_string($dsn, [qw(h P S)])
|
||||||
|
|| $dp->as_string($dsn, [qw(F)])
|
||||||
|
|| '';
|
||||||
|
|
||||||
|
}
|
||||||
PTDEBUG && _d($dbh, 'Connected dbh to', $self->{hostname},$self->{dsn_name});
|
PTDEBUG && _d($dbh, 'Connected dbh to', $self->{hostname},$self->{dsn_name});
|
||||||
return $dbh;
|
return $dbh;
|
||||||
}
|
}
|
||||||
@@ -3873,6 +3880,8 @@ sub quote_val {
|
|||||||
return $val if $val =~ m/^0x[0-9a-fA-F]+$/ # quote hex data
|
return $val if $val =~ m/^0x[0-9a-fA-F]+$/ # quote hex data
|
||||||
&& !$args{is_char}; # unless is_char is true
|
&& !$args{is_char}; # unless is_char is true
|
||||||
|
|
||||||
|
return $val if $args{is_float};
|
||||||
|
|
||||||
$val =~ s/(['\\])/\\$1/g;
|
$val =~ s/(['\\])/\\$1/g;
|
||||||
return "'$val'";
|
return "'$val'";
|
||||||
}
|
}
|
||||||
|
@@ -119,7 +119,7 @@ sub new {
|
|||||||
|
|
||||||
sub connect {
|
sub connect {
|
||||||
my ( $self, %opts ) = @_;
|
my ( $self, %opts ) = @_;
|
||||||
my $dsn = $self->{dsn};
|
my $dsn = $opts{dsn} || $self->{dsn};
|
||||||
my $dp = $self->{DSNParser};
|
my $dp = $self->{DSNParser};
|
||||||
|
|
||||||
my $dbh = $self->{dbh};
|
my $dbh = $self->{dbh};
|
||||||
@@ -139,6 +139,13 @@ sub connect {
|
|||||||
}
|
}
|
||||||
|
|
||||||
$dbh = $self->set_dbh($dbh);
|
$dbh = $self->set_dbh($dbh);
|
||||||
|
if ( $opts{dsn} ) {
|
||||||
|
$self->{dsn} = $dsn;
|
||||||
|
$self->{dsn_name} = $dp->as_string($dsn, [qw(h P S)])
|
||||||
|
|| $dp->as_string($dsn, [qw(F)])
|
||||||
|
|| '';
|
||||||
|
|
||||||
|
}
|
||||||
PTDEBUG && _d($dbh, 'Connected dbh to', $self->{hostname},$self->{dsn_name});
|
PTDEBUG && _d($dbh, 'Connected dbh to', $self->{hostname},$self->{dsn_name});
|
||||||
return $dbh;
|
return $dbh;
|
||||||
}
|
}
|
||||||
|
54
t/lib/Cxn.t
54
t/lib/Cxn.t
@@ -24,6 +24,8 @@ my $q = new Quoter();
|
|||||||
my $dp = new DSNParser(opts=>$dsn_opts);
|
my $dp = new DSNParser(opts=>$dsn_opts);
|
||||||
my $sb = new Sandbox(basedir => '/tmp', DSNParser => $dp);
|
my $sb = new Sandbox(basedir => '/tmp', DSNParser => $dp);
|
||||||
my $master_dbh = $sb->get_dbh_for('master');
|
my $master_dbh = $sb->get_dbh_for('master');
|
||||||
|
my $slave1_dbh = $sb->get_dbh_for('slave1');
|
||||||
|
my $slave1_dsn = $sb->dsn_for('slave1');
|
||||||
|
|
||||||
if ( !$master_dbh ) {
|
if ( !$master_dbh ) {
|
||||||
plan skip_all => 'Cannot connect to sandbox master';
|
plan skip_all => 'Cannot connect to sandbox master';
|
||||||
@@ -319,6 +321,58 @@ is(
|
|||||||
unlink $sync_file if -f $sync_file;
|
unlink $sync_file if -f $sync_file;
|
||||||
unlink $outfile if -f $outfile;
|
unlink $outfile if -f $outfile;
|
||||||
|
|
||||||
|
# #############################################################################
|
||||||
|
# Re-connect with new DSN.
|
||||||
|
# #############################################################################
|
||||||
|
|
||||||
|
SKIP: {
|
||||||
|
skip "Cannot connect to slave1", 4 unless $slave1_dbh;
|
||||||
|
|
||||||
|
$cxn = make_cxn(
|
||||||
|
dsn_string => 'h=127.1,P=12345,u=msandbox,p=msandbox',
|
||||||
|
);
|
||||||
|
|
||||||
|
$cxn->connect();
|
||||||
|
ok(
|
||||||
|
$cxn->dbh()->ping(),
|
||||||
|
"First connect()"
|
||||||
|
);
|
||||||
|
|
||||||
|
($row) = $cxn->dbh()->selectrow_hashref('SHOW SLAVE STATUS');
|
||||||
|
ok(
|
||||||
|
!defined $row,
|
||||||
|
"First connect() to master"
|
||||||
|
) or diag(Dumper($row));
|
||||||
|
|
||||||
|
$cxn->dbh->disconnect();
|
||||||
|
$cxn->connect(dsn => $dp->parse($slave1_dsn));
|
||||||
|
|
||||||
|
ok(
|
||||||
|
$cxn->dbh()->ping(),
|
||||||
|
"Re-connect connect()"
|
||||||
|
);
|
||||||
|
|
||||||
|
($row) = $cxn->dbh()->selectrow_hashref('SHOW SLAVE STATUS');
|
||||||
|
ok(
|
||||||
|
$row,
|
||||||
|
"Re-connect connect(slave_dsn) to slave"
|
||||||
|
) or diag(Dumper($row));
|
||||||
|
|
||||||
|
$cxn->dbh->disconnect();
|
||||||
|
$cxn->connect();
|
||||||
|
|
||||||
|
ok(
|
||||||
|
$cxn->dbh()->ping(),
|
||||||
|
"Re-re-connect connect()"
|
||||||
|
);
|
||||||
|
|
||||||
|
($row) = $cxn->dbh()->selectrow_hashref('SHOW SLAVE STATUS');
|
||||||
|
ok(
|
||||||
|
$row,
|
||||||
|
"Re-re-connect connect() to slave"
|
||||||
|
) or diag(Dumper($row));
|
||||||
|
}
|
||||||
|
|
||||||
# #############################################################################
|
# #############################################################################
|
||||||
# Done.
|
# Done.
|
||||||
# #############################################################################
|
# #############################################################################
|
||||||
|
Reference in New Issue
Block a user