ensembl-hive  2.8.1
convert_tables_MyISAM_InnoDB.pl
Go to the documentation of this file.
1 #!/usr/bin/env perl
2 
3 # See the NOTICE file distributed with this work for additional information
4 # regarding copyright ownership.
5 #
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
9 #
10 # http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17 
18 ###############################################################################################################
19 # convert_tables_MyISAM_InnoDB.pl script
20 # given a pattern of databases to query, lists all tables and whether they are InnoDB or MyISAM
21 # can convert a list of tables, or all tables, to MyISAM, or to InnoDB, as specified
22 # USAGE : perl convert_tables_MyISAM_InnoDB.pl -dbpattern _core_ -convert_to MyISAM -convert_all
23 ################################################################################################################
24 
25 # $Source: /cvsroot/ensembl/ensembl-personal/genebuilders/scripts/convert_tables_MyISAM_InnoDB.pl,v $
26 # $Revision: 1.4 $
27 
28 
29 use strict;
30 use warnings;
31 use Getopt::Long qw(:config no_ignore_case);
32 use DBI;
33 
34 my ($host, $user, $pass, $port, $dbpattern, $verbose, $convert_all, $convert_to, $tables);
35 
36 $user = 'ensro';
37 $pass = '';
38 $port = 3306;
39 $host = 'ens-staging';
40 $verbose = 0;
41 $dbpattern = '';
42 
43 GetOptions( "host|h|dbhost=s" => \$host,
44  "user|u|dbuser=s" => \$user,
45  "pass|p|dbpass=s" => \$pass,
46  "port|P|dbport=i" => \$port,
47  "dbpattern|pattern=s" => \$dbpattern,
48  'verbose!' => \$verbose,
49  'convert_all!' => \$convert_all, # flag to convert all tables
50  'convert_to=s' => \$convert_to, # engine to covert the tables to ( MyISAM, InnoDB )
51  'tables=s' => \$tables, # comma-separated list of tables to convert ie exon,exon_transcript
52  );
53 
54 my $dsn = "DBI:mysql:host=$host";
55 if( $port ) {
56  $dsn .= ";port=$port";
57 }
58 my $db = DBI->connect( $dsn, $user, $pass) ;
59 my @dbnames = map {$_->[0] } @{ $db->selectall_arrayref( "show databases" ) };
60 for my $dbname ( @dbnames ) {
61  if( $dbname =~ /$dbpattern/ ) {
62  print "connecting to $dbname\n" ;
63 
64  my $dsn_info = sprintf( "DBI:mysql:database=%s;host=%s;port=%s", 'information_schema', $host, $port) ;
65  my $db_info = DBI->connect( $dsn_info, $user, $pass ) ;
66 
67  my %engine_tables = %{ check_for_tables($db_info, $dbname, $verbose) } ;
68  my @tables_to_convert ;
69 
70  if ( $tables ) {
71  @tables_to_convert = split /\,/, $tables ;
72  } elsif ( $convert_all ) {
73  if ( $convert_to =~m/MyISAM/) {
74  die('There is no table to convert! Exiting...'."\n") unless (exists $engine_tables{"InnoDB"});
75  @tables_to_convert = @{$engine_tables{"InnoDB"}};
76  }elsif ( $convert_to =~ m/InnoDB/ ) {
77  die('There is no table to convert! Exiting...'."\n") unless (exists $engine_tables{"MyISAM"});
78  @tables_to_convert = @{$engine_tables{"MyISAM"}} ;
79  }
80  }
81 
82  unless ( $convert_to ) {
83  print "\n\n\nTo convert selected tables to use a different storage engines, use : \n\n" ;
84  print "\t\t-convert_to [MyISAM|InnoDB]' -tables exon_transcript,job,exon\n\n\n"
85  ." OR convert all tables with :\n\n\t\t-convert_to MyISAM -convert_all \n" ;
86  } else {
87 
88  print "\n\nWill convert these tables : \n" ;
89  for my $table ( @tables_to_convert ) {
90  printf "%-10s ===> %-10s\n", $table, $convert_to ;
91  }
92  print "\n\n\tARE YOU SURE YOU WANT TO GO AHEAD ??? ( Y / N )" ;
93  if ( get_input_arg() ) {
94  convert_all_tables ( $dbname, \@tables_to_convert, $convert_to ) ;
95  } else {
96  print "Stopping as you don't want to go ahead. no tables have been converted \n" ;
97  }
98  }
99  $db_info->disconnect() ;
100  }
101 }
102 
103 
104 sub convert_all_tables {
105  my ( $dbname, $tables_to_convert, $convert_to ) = @_ ;
106 
107  my $dsn_info = sprintf( "DBI:mysql:database=%s;host=%s;port=%s", $dbname, $host, $port) ;
108  my $dbh = DBI->connect( $dsn_info, $user, $pass ) ;
109 
110  for my $table ( @$tables_to_convert ) {
111  my $sql= "alter table $table engine =\'".$convert_to."\';" ;
112  $dbh->do($sql) ;
113  print "table $table converted to $convert_to\n" ;
114  }
115  $dbh->disconnect();
116 }
117 
118 sub check_for_tables {
119  my ($db_info, $dbname, $verbose) = @_;
120 
121  my $sql ="select table_type, table_name, engine from tables where table_schema =\'".$dbname. "\' " ;
122 
123  my $lines = $db_info->selectall_arrayref($sql) ;
124 
125  my %engine_types;
126 
127  for my $r( @$lines) {
128  my @rows = @$r ;
129  if ($rows[0] eq 'BASE TABLE') {
130  push @{ $engine_types{$rows[2]}}, $rows[1] ;
131  } elsif ($rows[0] eq 'VIEW') {
132  push @{ $engine_types{$rows[0]}}, $rows[1] ;
133  }
134  }
135  for ( keys %engine_types ) {
136  print uc($_) . " " . scalar(@{$engine_types{$_}}) . " tables $dbname\n" ;
137  }
138 
139  if ( $verbose ) {
140  print "\n\nTable types found :\n---------------------------------------\n\n" ;
141  for my $engine( keys %engine_types ) {
142  for my $table ( @{ $engine_types{$engine}} ) {
143  printf "%-10s%-10s\n", $engine, $table ;
144  }
145  }
146  }
147  return \%engine_types ;
148 }
149 
150 
151 =head2 get_input_arg ( Bio::EnsEMBL::Analysis::Tools::Utilities)
152 
153  Function : waits for input from STDIN and returns '1' if input =~m/y/i
154  and '0' if input matches /n/i.
155  Returntype: 1 or 0
156  Exceptions: none
157 
158 =cut
159 
160 sub get_input_arg {
161  while (defined (my $line=<STDIN>)){
162  chomp($line) ;
163  if ( $line=~m/y/i){
164  return 1 ;
165  }elsif( $line =~m/n/i){
166  return 0 ;
167  }
168  print "Wrong input - only answer 'y' or 'n'\n" ;
169  }
170 }
171 
172 
map
public map()
check_for_tables
public check_for_tables()
convert_all_tables
public convert_all_tables()
exon
public exon()
get_input_arg
public Boolean get_input_arg()