OTWO-1213 Works around lost encoding in Ruby/C binding layer
[ohcount] / test / src_dir / mysql-stale-table-sniper
1 #!/usr/bin/perl
2
3 # This is mysql-table-sniper, a program to help remove tables from a MySQL server.
4
5 # This program is copyright (c) 2007 Baron Schwartz, baron at xaprb dot com.
6 # Feedback and improvements are welcome.
7 #
8 # THIS PROGRAM IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
9 # WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
10 # MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
11 #
12 # This program is free software; you can redistribute it and/or modify it under
13 # the terms of the GNU General Public License as published by the Free Software
14 # Foundation, version 2; OR the Perl Artistic License.  On UNIX and similar
15 # systems, you can issue `man perlgpl' or `man perlartistic' to read these
16 # licenses.
17 #
18 # You should have received a copy of the GNU General Public License along with
19 # this program; if not, write to the Free Software Foundation, Inc., 59 Temple
20 # Place, Suite 330, Boston, MA  02111-1307  USA.
21
22 use strict;
23 use warnings FATAL => 'all';
24
25 use DBI;
26 use English qw(-no_match_vars);
27 use Getopt::Long;
28 use List::Util qw(max);
29
30 our $VERSION = '@VERSION@';
31
32 $OUTPUT_AUTOFLUSH = 1;
33
34 # ############################################################################
35 # Get configuration information.
36 # ############################################################################
37
38    print <<USAGE;
39
40 $PROGRAM_NAME helps you remove tables from a MySQL server.
41
42 If possible, database options are read from your .my.cnf file.
43 For more details, please read the documentation:
44
45    perldoc $PROGRAM_NAME
46
47 USAGE
48    exit(1);
49 }
50
51 # ############################################################################
52 # Get ready to do the main work.
53 # ############################################################################
54
55 my @databases = @{$dbh->selectcol_arrayref('SHOW DATABASES')};
56 my @whole_batch;
57
58 DATABASE:
59 foreach my $database ( @databases ) {
60
61    # Ignore databases as instructed.  Also ignore INFORMATION_SCHEMA and skip
62    # databases caused by lost+found directories created in the root of ext3
63    # filesystems; they are not really databases.
64    next DATABASE if
65       ( $opts{d} && !exists($opts{d}->{$database}) )
66       || $database =~ m/^(information_schema|lost\+found)$/mi
67       || exists $opts{g}->{$database};
68
69    my @tables = @{$dbh->selectcol_arrayref('SHOW TABLES FROM `' . $database .  '`')};
70    next DATABASE unless @tables;
71
72    my %info_for;
73
74    # Get a list of active connections
75    my $processes = $dbh->selectall_hashref("show processlist", 'Id');
76
77    foreach my $db ( @{ $dbh->selectcol_arrayref('show databases') } ) {
78       my @tables = @{ $dbh->selectcol_arrayref("show tables from $db") };
79       foreach my $tbl ( @tables ) {
80
81          # We only want tables whose name ends in digits NOT preceded by other
82          # digits (for example, barontest_2006_12_06 should not be dropped).
83          my ( $process ) = $tbl =~ m/\D_(\d+)$/;
84
85          next unless $process;
86
87          # If the process doesn't exist anymore, the table isn't in use.
88          if ( !exists($processes->{$process} ) ) {
89             print "Dropping table $db.$tbl\n" if $ENV{RKGDEBUG};
90             $dbh->do("drop table if exists $db.$tbl");
91          }
92       }
93    }
94
95    TABLE:
96    foreach my $table ( @tables ) {
97       next TABLE if exists $opts{n}->{$table};
98
99       my $ddl = ($dbh->selectrow_array("SHOW CREATE TABLE `$database`.`$table`"))[1];
100       next TABLE if $ddl =~ m/^CREATE ALGORITHM/;
101
102    }
103
104 }
105
106 # ############################################################################
107 # Subroutines
108 # ############################################################################
109
110 # ############################################################################
111 # Documentation
112 # ############################################################################
113
114 =pod
115
116 =head1 NAME
117
118 mysql-stale-table-sniper - Find and possibly remove stale MySQL tables.
119
120 =head1 DESCRIPTION
121
122 =head1 OUTPUT
123
124 =head1 SYSTEM REQUIREMENTS
125
126 You need the following Perl modules: DBI and DBD::mysql.
127
128 =head1 LICENSE
129
130 This program is copyright (c) 2007 Baron Schwartz, baron at xaprb dot com.
131 Feedback and improvements are welcome.
132
133 THIS PROGRAM IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
134 WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
135 MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
136
137 This program is free software; you can redistribute it and/or modify it under
138 the terms of the GNU General Public License as published by the Free Software
139 Foundation, version 2; OR the Perl Artistic License.  On UNIX and similar
140 systems, you can issue `man perlgpl' or `man perlartistic' to read these
141 licenses.
142
143 You should have received a copy of the GNU General Public License along with
144 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
145 Place, Suite 330, Boston, MA  02111-1307  USA.
146
147 =head1 AUTHOR
148
149 Baron Schwartz, baron at xaprb dot com.
150
151 =cut