#!/usr/bin/perl -w # Matija Nalis, GPLv3+, 20080116 # find and FIX mysql duplicate primary keys which case errors like # "/etc/mysql/debian-start[24344]: error : Table upgrade required. Please do "REPAIR TABLE `some_table`" to fix it!" # in mysql5 after upgrade to etch, on debian maintenance scripts # which do "CHECK TABLE". "REPAIR TABLE" however says "Table is already up to date" and won't fix it. # # see http://blog.tigertech.net/posts/mysql-table-upgrade-required/ # # it reads sql dump ("mysqldump -A" for example) from stdin # and outputs fixed sql dump to stdout use strict; while () { my $fix = $_; if ($fix =~ s/^(\s*PRIMARY\s+KEY\s+\()(`?[^` ]+`?)\s*,\s*(`?[^` ]+`?)(\)\s*,?\s*)$/$1$2$4/) { my $id1=$2; my $id2=$3; # print STDERR "diff $id1 to $id2\n"; if ($id1 =~ /^${id2}$/i) { # really duplicate id? it could be done in regexp above, but it's horrid as it is :) $_ = $fix; } } print; }