From 1f93caf67cb701a4e171ff65e52052143c00a7c7 Mon Sep 17 00:00:00 2001 From: Daniel Nichter Date: Wed, 4 Dec 2013 16:14:17 -0800 Subject: [PATCH] Add dsn opt to Cxn::connect() to change dsn. Update Cxn in pt-agent. --- bin/pt-agent | 11 ++++++++++- lib/Cxn.pm | 9 ++++++++- t/lib/Cxn.t | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 72 insertions(+), 2 deletions(-) diff --git a/bin/pt-agent b/bin/pt-agent index ced83594..92f19289 100755 --- a/bin/pt-agent +++ b/bin/pt-agent @@ -3691,7 +3691,7 @@ sub new { sub connect { my ( $self, %opts ) = @_; - my $dsn = $self->{dsn}; + my $dsn = $opts{dsn} || $self->{dsn}; my $dp = $self->{DSNParser}; my $dbh = $self->{dbh}; @@ -3710,6 +3710,13 @@ sub connect { } $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}); return $dbh; } @@ -3873,6 +3880,8 @@ sub quote_val { return $val if $val =~ m/^0x[0-9a-fA-F]+$/ # quote hex data && !$args{is_char}; # unless is_char is true + return $val if $args{is_float}; + $val =~ s/(['\\])/\\$1/g; return "'$val'"; } diff --git a/lib/Cxn.pm b/lib/Cxn.pm index b59bdd30..38469319 100644 --- a/lib/Cxn.pm +++ b/lib/Cxn.pm @@ -119,7 +119,7 @@ sub new { sub connect { my ( $self, %opts ) = @_; - my $dsn = $self->{dsn}; + my $dsn = $opts{dsn} || $self->{dsn}; my $dp = $self->{DSNParser}; my $dbh = $self->{dbh}; @@ -139,6 +139,13 @@ sub connect { } $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}); return $dbh; } diff --git a/t/lib/Cxn.t b/t/lib/Cxn.t index 2585d4a9..a5ae4308 100644 --- a/t/lib/Cxn.t +++ b/t/lib/Cxn.t @@ -24,6 +24,8 @@ my $q = new Quoter(); my $dp = new DSNParser(opts=>$dsn_opts); my $sb = new Sandbox(basedir => '/tmp', DSNParser => $dp); 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 ) { plan skip_all => 'Cannot connect to sandbox master'; @@ -319,6 +321,58 @@ is( unlink $sync_file if -f $sync_file; 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. # #############################################################################