~drizzle-trunk/drizzle/development

1 by brian
clean slate
1
#!/usr/bin/perl
2
# Copyright (C) 2000-2002 MySQL AB
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-1301  USA
16
17
# Convert given tables in a database to MYISAM
18
19
use DBI;
20
use 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_type="MYISAM";
27
$opt_port=0;
28
$exit_status=0;
29
30
GetOptions("force","help","host=s","password=s","user=s","type=s","verbose","version","socket=s", "port=i") || 
31
  usage(0);
32
usage($opt_version) if ($#ARGV < 0 || $opt_help || $opt_version);
33
$opt_database=shift(@ARGV);
34
35
if (uc($opt_type) eq "HEAP")
36
{
37
  print "Converting to type HEAP would delete your tables; aborting\n";
38
  exit(1);
39
}
40
41
$connect_opt="";
42
if ($opt_port)
43
{
44
  $connect_opt.= ";port=$opt_port";
45
}
46
if (length($opt_socket))
47
{
48
  $connect_opt.=";mysql_socket=$opt_socket";
49
}
50
51
$dbh = DBI->connect("DBI:mysql:$opt_database:${opt_host}$connect_opt",
52
		    $opt_user,
53
		    $opt_password,
54
		    { PrintError => 0})
55
  || die "Can't connect to database $opt_database: $DBI::errstr\n";
56
57
if ($#ARGV < 0)
58
{
59
  # Fetch all table names from the database
60
  my ($sth,$row);
61
  $sth=$dbh->prepare("show tables");
62
  $sth->execute || die "Can't get tables from $opt_database; $DBI::errstr\n";
63
  while (($row = $sth->fetchrow_arrayref))
64
  {
65
    push(@ARGV,$row->[0]);
66
  }
67
  $sth->finish;
68
}
69
70
print "Converting tables:\n" if ($opt_verbose);
71
foreach $table (@ARGV)
72
{
73
  my ($sth,$row);
74
75
  # Check if table is already converted
76
  $sth=$dbh->prepare("show table status like '$table'");  
77
  if ($sth->execute && ($row = $sth->fetchrow_arrayref))
78
  {
79
    if (uc($row->[1]) eq uc($opt_type))
80
    {
81
      print "$table is already of type $opt_type;  Ignored\n";
82
      next;
83
    }
84
  }
85
  print "converting $table\n" if ($opt_verbose);
86
  if (!$dbh->do("ALTER TABLE $table ENGINE=$opt_type"))
87
  {
88
    print STDERR "Can't convert $table: Error $DBI::errstr\n";
89
    exit(1) if (!$opt_force);
90
    $exit_status=1;
91
  }
92
}
93
94
$dbh->disconnect;
95
exit($exit_status);
96
97
98
sub usage
99
{
100
  my($version)=shift;
101
  print "$0  version 1.1\n";
102
  exit(0) if ($version);
103
104
  print <<EOF;
105
106
Conversion of a MySQL tables to other table types.
107
108
 Usage: $0 database [tables]
109
 If no tables has been specifed, all tables in the database will be converted.
110
111
 The following options are available:
112
113
--force
114
  Continue even if there is some error.
115
116
--help or --Information
117
  Shows this help
118
119
--host='host name' (Default $opt_host)
120
  Host name where the database server is located.
121
122
--password='password'
123
  Password for the current user.
124
125
--port=port
126
  TCP/IP port to connect to if host is not "localhost".
127
128
--socket='/path/to/socket'
129
  Socket to connect with.
130
131
--ENGINE='table-type'
132
  Converts tables to the given table type (Default: $opt_type)
133
  MySQL 3.23 supports at least the BDB, ISAM and MYISAM types.
134
135
--user='user_name'
136
  User name to log into the SQL server.
137
138
--verbose
139
  This is a test specific option that is only used when debugging a test.
140
  Print more information about what is going on.
141
142
--version
143
  Shows the version of this program.
144
EOF
145
  exit(1);
146
}