1#!@PERL_PATH@
2# Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
3#
4# This program is free software; you can redistribute it and/or modify
5# it under the terms of the GNU General Public License as published by
6# the Free Software Foundation; version 2 of the License.
7#
8# This program is distributed in the hope that it will be useful,
9# but WITHOUT ANY WARRANTY; without even the implied warranty of
10# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11# GNU General Public License for more details.
12#
13# You should have received a copy of the GNU General Public License
14# along with this program; if not, write to the Free Software
15# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1335  USA
16
17# Convert given tables in a database to MYISAM
18
19use DBI;
20use Getopt::Long;
21
22$opt_help=$opt_version=$opt_verbose=$opt_force=0;
23$opt_user=$opt_database=$opt_password=undef;
24$opt_host="localhost";
25$opt_socket="";
26$opt_engine="MYISAM";
27$opt_port=0;
28$exit_status=0;
29
30GetOptions(
31  "e|engine|type=s"       => \$opt_engine,
32  "f|force"               => \$opt_force,
33  "help|?"               => \$opt_help,
34  "h|host=s"              => \$opt_host,
35  "p|password=s"          => \$opt_password,
36  "u|user=s"              => \$opt_user,
37  "v|verbose"             => \$opt_verbose,
38  "V|version"             => \$opt_version,
39  "S|socket=s"            => \$opt_socket,
40  "P|port=i"              => \$opt_port
41) || usage(0);
42
43usage($opt_version) if ($#ARGV < 0 || $opt_help || $opt_version);
44
45$opt_database=shift(@ARGV);
46
47if (grep { /^$opt_engine$/i } qw(HEAP MEMORY BLACKHOLE))
48{
49  print "Converting to '$opt_engine' would delete your data; aborting\n";
50  exit(1);
51}
52
53$connect_opt="";
54if ($opt_port)
55{
56  $connect_opt.= ";port=$opt_port";
57}
58if (length($opt_socket))
59{
60  $connect_opt.=";mysql_socket=$opt_socket";
61}
62
63$dbh = DBI->connect("DBI:mysql:$opt_database:${opt_host}$connect_opt",
64		    $opt_user,
65		    $opt_password,
66		    { PrintError => 0})
67  || die "Can't connect to database $opt_database: $DBI::errstr\n";
68
69my @tables;
70
71push(@ARGV, "%") if(!@ARGV);
72
73foreach $pattern (@ARGV)
74{
75  my ($sth,$row);
76  $sth=$dbh->prepare("SHOW TABLES LIKE ?");
77  $rv= $sth->execute($pattern);
78  if(!int($rv))
79  {
80    warn "Can't get tables matching '$pattern' from $opt_database; $DBI::errstr\n";
81    exit(1) unless $opt_force;
82  }
83  while (($row = $sth->fetchrow_arrayref))
84  {
85    push(@tables, $row->[0]);
86  }
87  $sth->finish;
88}
89
90print "Converting tables:\n" if ($opt_verbose);
91foreach $table (@tables)
92{
93  my ($sth,$row);
94
95  # Check if table is already converted
96  $sth=$dbh->prepare("show table status like '$table'");
97  if ($sth->execute && ($row = $sth->fetchrow_arrayref))
98  {
99    if (uc($row->[1]) eq uc($opt_engine))
100    {
101      print "$table already uses the '$opt_engine' engine;  Ignored\n";
102      next;
103    }
104  }
105  print "converting $table\n" if ($opt_verbose);
106  $table=~ s/`/``/g;
107  if (!$dbh->do("ALTER TABLE `$table` ENGINE=$opt_engine"))
108  {
109    print STDERR "Can't convert $table: Error $DBI::errstr\n";
110    exit(1) if (!$opt_force);
111    $exit_status=1;
112  }
113}
114
115$dbh->disconnect;
116exit($exit_status);
117
118
119sub usage
120{
121  my($version)=shift;
122  print "$0  version 1.1\n";
123  exit(0) if ($version);
124
125  print <<EOF;
126
127Conversion of a MariaDB tables to other storage engines
128
129 Usage: $0 database [table[ table ...]]
130 If no tables has been specifed, all tables in the database will be converted.
131 You can also use wildcards, ie "my%"
132
133 The following options are available:
134
135-f, --force
136  Continue even if there is some error.
137
138-?, --help
139  Shows this help
140
141-e, --engine=ENGINE
142  Converts tables to the given storage engine (Default: $opt_engine)
143
144-h, --host=HOST
145  Host name where the database server is located. (Default: $opt_host)
146
147-p, --password=PASSWORD
148  Password for the current user.
149
150-P, --port=PORT
151  TCP/IP port to connect to if host is not "localhost".
152
153-S, --socket=SOCKET
154  Socket to connect with.
155
156-u, --user=USER
157  User name to log into the SQL server.
158
159-v, --verbose
160  This is a test specific option that is only used when debugging a test.
161  Print more information about what is going on.
162
163-V, --version
164  Shows the version of this program.
165EOF
166  exit(1);
167}
168