3 # See the NOTICE file distributed with this work for additional information
4 # regarding copyright ownership.
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
10 # http://www.apache.org/licenses/LICENSE-2.0
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.
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 ################################################################################################################
25 # $Source: /cvsroot/ensembl/ensembl-personal/genebuilders/scripts/convert_tables_MyISAM_InnoDB.pl,v $
31 use Getopt::Long qw(:config no_ignore_case);
34 my ($host, $user, $pass, $port, $dbpattern, $verbose, $convert_all, $convert_to, $tables);
39 $host =
'ens-staging';
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
54 my $dsn =
"DBI:mysql:host=$host";
56 $dsn .=
";port=$port";
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" ;
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 ) ;
68 my @tables_to_convert ;
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"}} ;
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" ;
88 print
"\n\nWill convert these tables : \n" ;
89 for my $table ( @tables_to_convert ) {
90 printf
"%-10s ===> %-10s\n", $table, $convert_to ;
92 print
"\n\n\tARE YOU SURE YOU WANT TO GO AHEAD ??? ( Y / N )" ;
96 print
"Stopping as you don't want to go ahead. no tables have been converted \n" ;
99 $db_info->disconnect() ;
105 my ( $dbname, $tables_to_convert, $convert_to ) = @_ ;
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 ) ;
110 for my $table ( @$tables_to_convert ) {
111 my $sql=
"alter table $table engine =\'".$convert_to.
"\';" ;
113 print
"table $table converted to $convert_to\n" ;
119 my ($db_info, $dbname, $verbose) = @_;
121 my $sql =
"select table_type, table_name, engine from tables where table_schema =\'".$dbname.
"\' " ;
123 my $lines = $db_info->selectall_arrayref($sql) ;
127 for my $r( @$lines) {
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] ;
135 for ( keys %engine_types ) {
136 print uc($_) .
" " . scalar(@{$engine_types{$_}}) .
" tables $dbname\n" ;
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 ;
147 return \%engine_types ;
151 =head2
get_input_arg ( Bio::EnsEMBL::Analysis::Tools::Utilities)
153 Function : waits
for input from STDIN and returns
'1' if input =~m/y/i
154 and
'0' if input matches /n/i.
161 while (defined (my $line=<STDIN>)){
165 }elsif( $line =~m/n/i){
168 print
"Wrong input - only answer 'y' or 'n'\n" ;