~percona-toolkit-dev/percona-toolkit/pt-key-walker

« back to all changes in this revision

Viewing changes to t/pt-fk-walker/samples/pm/sakila-staff.pm

  • Committer: Daniel Nichter
  • Date: 2012-03-13 22:28:40 UTC
  • Revision ID: daniel@percona.com-20120313222840-cq2lm21l9c848d67
Document code and plugin.

Show diffs side-by-side

added added

removed removed

Lines of Context:
6
6
use constant PTDEBUG => $ENV{PTDEBUG} || 0;
7
7
use Data::Dumper;
8
8
 
9
 
# staff_id     1,   2
10
 
# address_id   3,   4
11
 
# city_id    300, 576
12
 
# country_id  20,   8
13
 
 
14
9
# Not called by the tool; called in this file.  The tool calls our init().
15
10
sub new {
16
11
   my ($class, %args) = @_;
59
54
   my $del_col = $tbl->{tbl} . "_id";  # col to delete by in this table
60
55
 
61
56
   if ( !$parent_tbl ) {
 
57
      # This is the ultimate parent table (i.e. the first table).
62
58
      # Your custom SQL to get the parent rows to delete, being sure to
63
59
      # select the child table's fk columns.
64
60
      $sql = "SELECT country_id FROM $table "
65
61
            . "WHERE country_id IN (20, 8)";
66
62
   }
67
63
   else {
 
64
      # This is a child table (i.e. 2nd and subsequent tables that
 
65
      # reference the ultimate parent table).
68
66
      # Child table rows fetched automatically based on table names,
69
67
      # or do special parent-child column mappings here.
70
68
      my $col     = $tbl->{tbl} . "_id";
73
71
           . "IN (" . (join ',', @{$parent_tbl->{row_ids}}) . ")";
74
72
   }
75
73
 
 
74
   # Get rows in this table.
76
75
   $rows = $dbh->selectall_arrayref($sql);
77
76
   print $sql, ";\n" if $self->{print};
78
77
   if ( $rows && @$rows ) {
 
78
      # Save the row's key column values which will be used to find
 
79
      # rows in the next child table (i.e. $parent_tbl->{row_ids} from
 
80
      # the child table's perspective) and to delete rows in this table.
79
81
      my @row_ids     =  map { $_->[0] } @$rows;
80
82
      $tbl->{row_ids} = \@row_ids;
81
83
      $self->{del}->[$tableno]
84
86
         . "IN (" . (join ',', @row_ids) . ")";
85
87
   }
86
88
   elsif ( !$parent_tbl ) {
 
89
      # No rows in the parent table.  We probably deleted them and
 
90
      # now the tool is iterating again, so we're done.
87
91
      $self->{done} = 1;
88
92
      return 0;  # stop iterating fk tables
89
93
   }
90
94
 
91
 
   return 1;
 
95
   return 1;  # continue; next fk table
92
96
}
93
97
 
94
 
# Done iterating through tables for these rows.
 
98
# Done iterating through fk tables.
95
99
sub iter_done {
96
100
   my ($self, %args) = @_;
97
101
   my $cxn = $self->{cxn};
 
102
 
 
103
   # DELETE statements were saved in top-to-bottom order, since that's
 
104
   # the order next_table() was called.  So reverse the list so we DELETE
 
105
   # from the bottom (last child) table up.
98
106
   foreach my $sql ( reverse @{$self->{del}} ) {
99
107
      next unless $sql;
100
 
      print $sql, ";\n"     if $self->{print};
 
108
      print $sql, ";\n" if $self->{print};
101
109
      if ( $self->{execute} ) {
102
110
         PTDEBUG && _d($sql);
103
111
         $cxn->dbh()->do($sql);
104
112
      }
105
113
   }
106
 
   $self->{done} = 1 if $self->{print}; # prevent infinite loop
 
114
 
 
115
   # Prevent infinite loop: if we don't delete the rows,
 
116
   # we'll see them again in next_table() and repeat.
 
117
   $self->{done} = 1 if $self->{print};
 
118
 
107
119
   return;
108
120
}
109
121