File: delete_more.pm

package info (click to toggle)
percona-toolkit 3.2.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 68,916 kB
  • sloc: perl: 241,287; sql: 22,868; sh: 19,746; javascript: 6,799; makefile: 353; awk: 38; python: 30; sed: 1
file content (187 lines) | stat: -rw-r--r-- 5,598 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# This program is copyright 2010 Percona Inc.
# Feedback and improvements are welcome.
#
# THIS PROGRAM IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
# WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
# MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation, version 2; OR the Perl Artistic License.  On UNIX and similar
# systems, you can issue `man perlgpl' or `man perlartistic' to read these
# licenses.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Software Foundation, Inc., 59 Temple
# Place, Suite 330, Boston, MA  02111-1307  USA.

package delete_more;

# This mk-archiver plugin demonstrates how to archive/DELETE rows on one
# table--the main table--and also DELETE related rows on other tables.
# The picture is:
#
#   main_table-123:  other_table-123:
#     col-m 1           col-o 1
#     col-m 2           col-o 1
#                       col-o 2
#
# When rows on main table are deleted, corresponding rows on the other
# tables are deleted where main table col-m = other table col-o.  This
# works for both single and --bulk-delete.  The tables are *not* 1-to-1
# so a single delete for main col-m = 1 will result in two deletes fro
# other col-o = 1.  This means --limit does *not* apply on other table.
# 
# The other table's name is derived from the main table's name according
# to the settings below.
#
# Limitations:
#   * all tables must be on the same server
#   * other table column (e.g. opk) must be the same on all other tables
#   * main table column and other table columns must be numeric
#   * no NULL values

use strict;
use warnings FATAL => 'all';
use English qw(-no_match_vars);

use constant PTDEBUG  => $ENV{PTDEBUG};

use Data::Dumper;
$Data::Dumper::Indent    = 1;
$Data::Dumper::Sortkeys  = 1;
$Data::Dumper::Quotekeys = 0;

# ###########################################################################
# Customize these values for your tables.
# ###########################################################################
my $main_table_col   = 'id';   # main table pk col
my $other_table_col  = 'id';   # other table pk col
my $main_table_id    = qr/(\d+)$/;
my $other_table_base = 'other_table-';
my $other_db         = undef;  # undef = same as main db
my $other_table      = undef;  # undef = auto-determine

# ###########################################################################
# Don't modify anything below here.
# ###########################################################################
sub new {
   my ( $class, %args ) = @_;
   my $o = $args{OptionParser};
   my $q = $args{Quoter};

   $other_db ||= $args{db};

   if ( !$other_table ) {
      my ($id) = $args{tbl} =~ m/$main_table_id/;
      die "Cannot determine other table; $args{tbl} does not match "
         . $main_table_id unless $id;
      $other_table = $other_table_base . $id;
   }
   $other_table = $q->quote($other_db, $other_table);
   PTDEBUG && _d('Other table:', $other_table);

   my $self = {
      dbh          => $args{dbh},
      bulk_delete  => $o->get('bulk-delete'),
      limit        => $o->get('limit'),
      delete_rows  => [],  # saved main table col vals for --bulk-delete
      main_col_pos => -1,
      other_tbl    => $other_table,
   };

   if ( $o->get('dry-run') ) {
      print "# delete_more other table $other_table\n";
   }

   return bless $self, $class;
}

sub before_begin {
   my ( $self, %args ) = @_;
   my $allcols = $args{allcols};
   PTDEBUG && _d('allcols:', Dumper($allcols));
   my $colpos = -1;
   foreach my $col ( @$allcols ) {
      $colpos++;
      last if $col eq $main_table_col;
   }
   if ( $colpos < 0 ) {
      die "Main table column $main_table_col not selected by mk-archiver: "
         . join(', ', @$allcols);
   }
   PTDEBUG && _d('main col pos:', $colpos);
   $self->{main_col_pos} = $colpos;
   return;
}

sub is_archivable {
   my ( $self, %args ) = @_;
   my $row = $args{row};
   push @{$self->{delete_rows}}, $row->[$self->{main_col_pos}]
      if $self->{bulk_delete};
   return 1;
}

sub before_delete {
   my ( $self, %args ) = @_;
   my $row = $args{row};
   my $val = $row->[ $self->{main_col_pos} ];
   my $dbh = $self->{dbh};

   my $sql = "DELETE FROM $self->{other_tbl} "
           . "WHERE $other_table_col=$val";
   PTDEBUG && _d($sql);
   eval {
      $dbh->do($sql);
   };
   if ( $EVAL_ERROR ) {
      PTDEBUG && _d($EVAL_ERROR);
      warn $EVAL_ERROR;
   }

   return;
}

sub before_bulk_delete {
   my ( $self, %args ) = @_;

   if ( !scalar @{$self->{delete_rows}} ) {
      warn "before_bulk_delete() called without any rows to delete";
      return;
   }

   my $dbh              = $self->{dbh};
   my $delete_rows      = join(',', @{$self->{delete_rows}});
   $self->{delete_rows} = [];  # clear for next call


   my $sql = "DELETE FROM $self->{other_tbl} "
           . "WHERE $other_table_col IN ($delete_rows) ";
#           . "LIMIT $self->{limit}";
   PTDEBUG && _d($sql);
   eval {
      $dbh->do($sql);
   };
   if ( $EVAL_ERROR ) {
      PTDEBUG && _d($EVAL_ERROR);
      warn $EVAL_ERROR;
   }

   return;
}

sub after_finish {
   my ( $self ) = @_;
   return;
}

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;