## Tool to output all tables with autoinc
## from old server to use on new server
## to properly repair tables.
## in Reference to
## http://bugs.mysql.com/bug.php?id=14515#c59034
$hostname = "localhost";
$username = "root";
$password = "";
$skip = array("mysql", "information_schema");
$output_name = "repair_auto_inc.sql";
### no changes needed past this point
$fp = fopen ($output_name, "w");
fwrite($fp, "## Repair database tool output\n## use this output to fix missing auto_incr\n\n\n");
fclose($fp);
$dbLink = mysql_connect($hostname,$username,$password) or die("Unable to connect to the database.");
$sql = "show databases";
$query = mysql_query($sql) or die("error fetching database names");
while ($rs=mysql_fetch_array($query)) {
if (!in_array($rs['Database'],$skip)) {
echo "Database: " . $rs['Database'] . "
\r\n";
$dbName = $rs['Database'];
mysql_connect($hostname,$username,$password) or die("Can't create connection");
$res1 = mysql_query("SHOW TABLES FROM $dbName");
$i=0;
$fp = fopen ($output_name, "a");
fwrite($fp, "\n\n\nUSE `".$dbName."`;\n\n");
while($row1 = mysql_fetch_array($res1)){
$res2 = mysql_db_query($dbName, "SHOW CREATE TABLE $row1[0]");
while($row2 = mysql_fetch_array($res2)){
preg_match("/\s*(\W\w+\W)(.*auto_increment)/", $row2[1], $matches);
if($matches){
$i++;
$str = "ALTER TABLE ".$row2[0]." CHANGE ".$matches[1]."
".$matches[1]."".$matches[2].";\n";
fwrite($fp, $str);
}
}
mysql_free_result($res2);
}
fclose($fp);
mysql_free_result($res1);
mysql_close();
echo "Entries created for " . $dbName . ": ".$i."
\r\n
\r\n";
}
}
echo "Finished!\n";
?>