Skip to content

Instantly share code, notes, and snippets.

@dpavlin
Created July 10, 2012 19:31
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dpavlin/3085693 to your computer and use it in GitHub Desktop.
Save dpavlin/3085693 to your computer and use it in GitHub Desktop.
Migrate MySQL 4.0 dump to 5.5 format
#!/usr/bin/perl
use warnings;
use strict;
my $create_table = 0;
sub key {
my ($unique,$key,$name,$cols) = @_;
$name = "`$name`" if defined $name;
return " $unique$key $name (" . join(',', map {
s{\((\d+)\)}{} ? "`$_`($1)" : "`$_`"
} split(/,/,$cols)) . ")";
}
while(<>) {
chomp;
s{^(--)(.+)}{$1 $2}; # fix commends (WTF?)
s{(CREATE TABLE )(\S+)}{$1`$2`} and $create_table = 1;
s{(CREATE DATABASE) \Q/*!32312 IF NOT EXISTS*/\E (\S+);}{$1 IF NOT EXISTS `$2` ;};
s{TYPE=MyISAM}{ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin} and $create_table = 0;
if ( $create_table ) {
s{^\s+(UNIQUE|PRIMARY|FULLTEXT)?(\s?KEY)\s+(\S+)?\s?\((\S+)\)}{key($1,$2,$3,$4)}e or
s{^ (\S+)}{ `$1`};
s{(timestamp)\(\d+\)}{$1};
}
print "$_\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment