Created
July 10, 2012 19:31
-
-
Save dpavlin/3085693 to your computer and use it in GitHub Desktop.
Migrate MySQL 4.0 dump to 5.5 format
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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