diff --git a/bin/pt-table-checksum b/bin/pt-table-checksum index e671aa4a..932ab8c4 100755 --- a/bin/pt-table-checksum +++ b/bin/pt-table-checksum @@ -1580,19 +1580,56 @@ sub new { return bless $self, $class; } +sub get_create_table { + my ( $self, $dbh, $db, $tbl ) = @_; + die "I need a dbh parameter" unless $dbh; + die "I need a db parameter" unless $db; + die "I need a tbl parameter" unless $tbl; + my $q = $self->{Quoter}; + + my $sql = '/*!40101 SET @OLD_SQL_MODE := @@SQL_MODE, ' + . q{@@SQL_MODE := REPLACE(REPLACE(@@SQL_MODE, 'ANSI_QUOTES', ''), ',,', ','), } + . '@OLD_QUOTE := @@SQL_QUOTE_SHOW_CREATE, ' + . '@@SQL_QUOTE_SHOW_CREATE := 1 */'; + MKDEBUG && _d($sql); + eval { $dbh->do($sql); }; + MKDEBUG && $EVAL_ERROR && _d($EVAL_ERROR); + + $sql = 'USE ' . $q->quote($db); + MKDEBUG && _d($dbh, $sql); + $dbh->do($sql); + + $sql = "SHOW CREATE TABLE " . $q->quote($db, $tbl); + MKDEBUG && _d($sql); + my $href; + eval { $href = $dbh->selectrow_hashref($sql); }; + if ( $EVAL_ERROR ) { + MKDEBUG && _d($EVAL_ERROR); + return; + } + + $sql = '/*!40101 SET @@SQL_MODE := @OLD_SQL_MODE, ' + . '@@SQL_QUOTE_SHOW_CREATE := @OLD_QUOTE */'; + MKDEBUG && _d($sql); + $dbh->do($sql); + + my ($key) = grep { m/create table/i } keys %$href; + if ( $key ) { + MKDEBUG && _d('This table is a base table'); + $href->{$key} =~ s/\b[ ]{2,}/ /g; + $href->{$key} .= "\n"; + } + else { + MKDEBUG && _d('This table is a view'); + ($key) = grep { m/create view/i } keys %$href; + } + + return $href->{$key}; +} + sub parse { my ( $self, $ddl, $opts ) = @_; return unless $ddl; - if ( ref $ddl eq 'ARRAY' ) { - if ( lc $ddl->[0] eq 'table' ) { - $ddl = $ddl->[1]; - } - else { - return { - engine => 'VIEW', - }; - } - } if ( $ddl !~ m/CREATE (?:TEMPORARY )?TABLE `/ ) { die "Cannot parse table definition; is ANSI quoting " @@ -1899,41 +1936,27 @@ sub remove_auto_increment { return $ddl; } -sub remove_secondary_indexes { - my ( $self, $ddl ) = @_; - my $sec_indexes_ddl; - my $tbl_struct = $self->parse($ddl); - - if ( ($tbl_struct->{engine} || '') =~ m/InnoDB/i ) { - my $clustered_key = $tbl_struct->{clustered_key}; - $clustered_key ||= ''; - - my @sec_indexes = map { - my $key_def = $_->{ddl}; - $key_def =~ s/([\(\)])/\\$1/g; - $ddl =~ s/\s+$key_def//i; - - my $key_ddl = "ADD $_->{ddl}"; - $key_ddl .= ',' unless $key_ddl =~ m/,$/; - $key_ddl; - } - grep { $_->{name} ne $clustered_key } - values %{$tbl_struct->{keys}}; - MKDEBUG && _d('Secondary indexes:', Dumper(\@sec_indexes)); - - if ( @sec_indexes ) { - $sec_indexes_ddl = join(' ', @sec_indexes); - $sec_indexes_ddl =~ s/,$//; - } - - $ddl =~ s/,(\n\) )/$1/s; +sub get_table_status { + my ( $self, $dbh, $db, $like ) = @_; + my $q = $self->{Quoter}; + my $sql = "SHOW TABLE STATUS FROM " . $q->quote($db); + my @params; + if ( $like ) { + $sql .= ' LIKE ?'; + push @params, $like; } - else { - MKDEBUG && _d('Not removing secondary indexes from', - $tbl_struct->{engine}, 'table'); - } - - return $ddl, $sec_indexes_ddl, $tbl_struct; + MKDEBUG && _d($sql, @params); + my $sth = $dbh->prepare($sql); + $sth->execute(@params); + my @tables = @{$sth->fetchall_arrayref({})}; + @tables = map { + my %tbl; # Make a copy with lowercased keys + @tbl{ map { lc $_ } keys %$_ } = values %$_; + $tbl{engine} ||= $tbl{type} || $tbl{comment}; + delete $tbl{type}; + \%tbl; + } @tables; + return @tables; } sub _d { @@ -2207,311 +2230,6 @@ sub _d { # End TableNibbler package # ########################################################################### -# ########################################################################### -# MySQLDump package -# This package is a copy without comments from the original. The original -# with comments and its test file can be found in the Bazaar repository at, -# lib/MySQLDump.pm -# t/lib/MySQLDump.t -# See https://launchpad.net/percona-toolkit for more information. -# ########################################################################### -{ -package MySQLDump; - -use strict; -use warnings FATAL => 'all'; -use English qw(-no_match_vars); -use constant MKDEBUG => $ENV{MKDEBUG} || 0; - -( our $before = <<'EOF') =~ s/^ //gm; - /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; - /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; - /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; - /*!40101 SET NAMES utf8 */; - /*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */; - /*!40103 SET TIME_ZONE='+00:00' */; - /*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */; - /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; - /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; - /*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */; -EOF - -( our $after = <<'EOF') =~ s/^ //gm; - /*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */; - /*!40101 SET SQL_MODE=@OLD_SQL_MODE */; - /*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */; - /*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */; - /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; - /*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */; - /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */; - /*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */; -EOF - -sub new { - my ( $class, %args ) = @_; - my $self = { - cache => 0, # Afaik no script uses this cache any longer because - }; - return bless $self, $class; -} - -sub dump { - my ( $self, $dbh, $quoter, $db, $tbl, $what ) = @_; - - if ( $what eq 'table' ) { - my $ddl = $self->get_create_table($dbh, $quoter, $db, $tbl); - return unless $ddl; - if ( $ddl->[0] eq 'table' ) { - return $before - . 'DROP TABLE IF EXISTS ' . $quoter->quote($tbl) . ";\n" - . $ddl->[1] . ";\n"; - } - else { - return 'DROP TABLE IF EXISTS ' . $quoter->quote($tbl) . ";\n" - . '/*!50001 DROP VIEW IF EXISTS ' - . $quoter->quote($tbl) . "*/;\n/*!50001 " - . $self->get_tmp_table($dbh, $quoter, $db, $tbl) . "*/;\n"; - } - } - elsif ( $what eq 'triggers' ) { - my $trgs = $self->get_triggers($dbh, $quoter, $db, $tbl); - if ( $trgs && @$trgs ) { - my $result = $before . "\nDELIMITER ;;\n"; - foreach my $trg ( @$trgs ) { - if ( $trg->{sql_mode} ) { - $result .= qq{/*!50003 SET SESSION SQL_MODE='$trg->{sql_mode}' */;;\n}; - } - $result .= "/*!50003 CREATE */ "; - if ( $trg->{definer} ) { - my ( $user, $host ) - = map { s/'/''/g; "'$_'"; } - split('@', $trg->{definer}, 2); - $result .= "/*!50017 DEFINER=$user\@$host */ "; - } - $result .= sprintf("/*!50003 TRIGGER %s %s %s ON %s\nFOR EACH ROW %s */;;\n\n", - $quoter->quote($trg->{trigger}), - @{$trg}{qw(timing event)}, - $quoter->quote($trg->{table}), - $trg->{statement}); - } - $result .= "DELIMITER ;\n\n/*!50003 SET SESSION SQL_MODE=\@OLD_SQL_MODE */;\n\n"; - return $result; - } - else { - return undef; - } - } - elsif ( $what eq 'view' ) { - my $ddl = $self->get_create_table($dbh, $quoter, $db, $tbl); - return '/*!50001 DROP TABLE IF EXISTS ' . $quoter->quote($tbl) . "*/;\n" - . '/*!50001 DROP VIEW IF EXISTS ' . $quoter->quote($tbl) . "*/;\n" - . '/*!50001 ' . $ddl->[1] . "*/;\n"; - } - else { - die "You didn't say what to dump."; - } -} - -sub _use_db { - my ( $self, $dbh, $quoter, $new ) = @_; - if ( !$new ) { - MKDEBUG && _d('No new DB to use'); - return; - } - my $sql = 'USE ' . $quoter->quote($new); - MKDEBUG && _d($dbh, $sql); - $dbh->do($sql); - return; -} - -sub get_create_table { - my ( $self, $dbh, $quoter, $db, $tbl ) = @_; - if ( !$self->{cache} || !$self->{tables}->{$db}->{$tbl} ) { - my $sql = '/*!40101 SET @OLD_SQL_MODE := @@SQL_MODE, ' - . q{@@SQL_MODE := REPLACE(REPLACE(@@SQL_MODE, 'ANSI_QUOTES', ''), ',,', ','), } - . '@OLD_QUOTE := @@SQL_QUOTE_SHOW_CREATE, ' - . '@@SQL_QUOTE_SHOW_CREATE := 1 */'; - MKDEBUG && _d($sql); - eval { $dbh->do($sql); }; - MKDEBUG && $EVAL_ERROR && _d($EVAL_ERROR); - $self->_use_db($dbh, $quoter, $db); - $sql = "SHOW CREATE TABLE " . $quoter->quote($db, $tbl); - MKDEBUG && _d($sql); - my $href; - eval { $href = $dbh->selectrow_hashref($sql); }; - if ( $EVAL_ERROR ) { - warn "Failed to $sql. The table may be damaged.\nError: $EVAL_ERROR"; - return; - } - - $sql = '/*!40101 SET @@SQL_MODE := @OLD_SQL_MODE, ' - . '@@SQL_QUOTE_SHOW_CREATE := @OLD_QUOTE */'; - MKDEBUG && _d($sql); - $dbh->do($sql); - my ($key) = grep { m/create table/i } keys %$href; - if ( $key ) { - MKDEBUG && _d('This table is a base table'); - $self->{tables}->{$db}->{$tbl} = [ 'table', $href->{$key} ]; - } - else { - MKDEBUG && _d('This table is a view'); - ($key) = grep { m/create view/i } keys %$href; - $self->{tables}->{$db}->{$tbl} = [ 'view', $href->{$key} ]; - } - } - return $self->{tables}->{$db}->{$tbl}; -} - -sub get_columns { - my ( $self, $dbh, $quoter, $db, $tbl ) = @_; - MKDEBUG && _d('Get columns for', $db, $tbl); - if ( !$self->{cache} || !$self->{columns}->{$db}->{$tbl} ) { - $self->_use_db($dbh, $quoter, $db); - my $sql = "SHOW COLUMNS FROM " . $quoter->quote($db, $tbl); - MKDEBUG && _d($sql); - my $cols = $dbh->selectall_arrayref($sql, { Slice => {} }); - - $self->{columns}->{$db}->{$tbl} = [ - map { - my %row; - @row{ map { lc $_ } keys %$_ } = values %$_; - \%row; - } @$cols - ]; - } - return $self->{columns}->{$db}->{$tbl}; -} - -sub get_tmp_table { - my ( $self, $dbh, $quoter, $db, $tbl ) = @_; - my $result = 'CREATE TABLE ' . $quoter->quote($tbl) . " (\n"; - $result .= join(",\n", - map { ' ' . $quoter->quote($_->{field}) . ' ' . $_->{type} } - @{$self->get_columns($dbh, $quoter, $db, $tbl)}); - $result .= "\n)"; - MKDEBUG && _d($result); - return $result; -} - -sub get_triggers { - my ( $self, $dbh, $quoter, $db, $tbl ) = @_; - if ( !$self->{cache} || !$self->{triggers}->{$db} ) { - $self->{triggers}->{$db} = {}; - my $sql = '/*!40101 SET @OLD_SQL_MODE := @@SQL_MODE, ' - . q{@@SQL_MODE := REPLACE(REPLACE(@@SQL_MODE, 'ANSI_QUOTES', ''), ',,', ','), } - . '@OLD_QUOTE := @@SQL_QUOTE_SHOW_CREATE, ' - . '@@SQL_QUOTE_SHOW_CREATE := 1 */'; - MKDEBUG && _d($sql); - eval { $dbh->do($sql); }; - MKDEBUG && $EVAL_ERROR && _d($EVAL_ERROR); - $sql = "SHOW TRIGGERS FROM " . $quoter->quote($db); - MKDEBUG && _d($sql); - my $sth = $dbh->prepare($sql); - $sth->execute(); - if ( $sth->rows ) { - my $trgs = $sth->fetchall_arrayref({}); - foreach my $trg (@$trgs) { - my %trg; - @trg{ map { lc $_ } keys %$trg } = values %$trg; - push @{ $self->{triggers}->{$db}->{ $trg{table} } }, \%trg; - } - } - $sql = '/*!40101 SET @@SQL_MODE := @OLD_SQL_MODE, ' - . '@@SQL_QUOTE_SHOW_CREATE := @OLD_QUOTE */'; - MKDEBUG && _d($sql); - $dbh->do($sql); - } - if ( $tbl ) { - return $self->{triggers}->{$db}->{$tbl}; - } - return values %{$self->{triggers}->{$db}}; -} - -sub get_databases { - my ( $self, $dbh, $quoter, $like ) = @_; - if ( !$self->{cache} || !$self->{databases} || $like ) { - my $sql = 'SHOW DATABASES'; - my @params; - if ( $like ) { - $sql .= ' LIKE ?'; - push @params, $like; - } - my $sth = $dbh->prepare($sql); - MKDEBUG && _d($sql, @params); - $sth->execute( @params ); - my @dbs = map { $_->[0] } @{$sth->fetchall_arrayref()}; - $self->{databases} = \@dbs unless $like; - return @dbs; - } - return @{$self->{databases}}; -} - -sub get_table_status { - my ( $self, $dbh, $quoter, $db, $like ) = @_; - if ( !$self->{cache} || !$self->{table_status}->{$db} || $like ) { - my $sql = "SHOW TABLE STATUS FROM " . $quoter->quote($db); - my @params; - if ( $like ) { - $sql .= ' LIKE ?'; - push @params, $like; - } - MKDEBUG && _d($sql, @params); - my $sth = $dbh->prepare($sql); - $sth->execute(@params); - my @tables = @{$sth->fetchall_arrayref({})}; - @tables = map { - my %tbl; # Make a copy with lowercased keys - @tbl{ map { lc $_ } keys %$_ } = values %$_; - $tbl{engine} ||= $tbl{type} || $tbl{comment}; - delete $tbl{type}; - \%tbl; - } @tables; - $self->{table_status}->{$db} = \@tables unless $like; - return @tables; - } - return @{$self->{table_status}->{$db}}; -} - -sub get_table_list { - my ( $self, $dbh, $quoter, $db, $like ) = @_; - if ( !$self->{cache} || !$self->{table_list}->{$db} || $like ) { - my $sql = "SHOW /*!50002 FULL*/ TABLES FROM " . $quoter->quote($db); - my @params; - if ( $like ) { - $sql .= ' LIKE ?'; - push @params, $like; - } - MKDEBUG && _d($sql, @params); - my $sth = $dbh->prepare($sql); - $sth->execute(@params); - my @tables = @{$sth->fetchall_arrayref()}; - @tables = map { - my %tbl = ( - name => $_->[0], - engine => ($_->[1] || '') eq 'VIEW' ? 'VIEW' : '', - ); - \%tbl; - } @tables; - $self->{table_list}->{$db} = \@tables unless $like; - return @tables; - } - return @{$self->{table_list}->{$db}}; -} - -sub _d { - my ($package, undef, $line) = caller 0; - @_ = map { (my $temp = $_) =~ s/\n/\n# /g; $temp; } - map { defined $_ ? $_ : 'undef' } - @_; - print STDERR "# $package:$line $PID ", join(' ', @_), "\n"; -} - -1; -} -# ########################################################################### -# End MySQLDump package -# ########################################################################### - # ########################################################################### # MasterSlave package # This package is a copy without comments from the original. The original @@ -4410,8 +4128,8 @@ sub _iterate_dbh { if ( !$engine || $self->engine_is_allowed($engine) ) { my $ddl; - if ( my $du = $self->{MySQLDump} ) { - $ddl = $du->get_create_table($dbh, $q, $self->{db}, $tbl)->[1]; + if ( my $du = $self->{TableParser} ) { + $ddl = $du->get_create_table($dbh, $self->{db}, $tbl); } return { @@ -4873,11 +4591,9 @@ sub main { my $tn = new TableNibbler(TableParser => $tp, Quoter => $q); my $rc = new RowChecksum(Quoter=> $q, OptionParser => $o); my $ms = new MasterSlave(VersionParser => $vp); - my $du = new MySQLDump(); my $rr = new Retry(); my %common_modules = ( DSNParser => $dp, - MySQLDump => $du, OptionParser => $o, MasterSlave => $ms, Quoter => $q, diff --git a/lib/SchemaIterator.pm b/lib/SchemaIterator.pm index 02e6e110..765d01ce 100644 --- a/lib/SchemaIterator.pm +++ b/lib/SchemaIterator.pm @@ -343,8 +343,8 @@ sub _iterate_dbh { if ( !$engine || $self->engine_is_allowed($engine) ) { my $ddl; - if ( my $du = $self->{TableParser} ) { - $ddl = $du->get_create_table($dbh, $self->{db}, $tbl); + if ( my $tp = $self->{TableParser} ) { + $ddl = $tp->get_create_table($dbh, $self->{db}, $tbl); } return {