Last active
August 29, 2015 14:02
-
-
Save fwip/c24c2389fd358ad94946 to your computer and use it in GitHub Desktop.
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 | |
# Original version at: http://stackoverflow.com/a/9857906 | |
#=============================================================================== | |
# USAGE: ./mysql2sqlite.pl <MySQL_dumpfile> | |
# DESCRIPTION: Converts MySQL dumpfile to SQLite database | |
# Triggers are not converted | |
# The dump must be done with | |
# > mysqldump --skip-triggers -u [user] --p [database] > dumpfile | |
# REQUIREMENTS: Perl and module SQL::Translator, SQLite | |
#=============================================================================== | |
use strict; | |
use warnings; | |
use Carp; | |
use English qw( -no_match_vars ); | |
use SQL::Translator; | |
my $file = $ARGV[0]; | |
my $filedb = $file; | |
$filedb =~ s/\.*[^.]*$/.db/; | |
if ( -s $filedb ) { | |
print "*** Ja existe o arquivo < $filedb >. Abandonando...\n"; | |
exit; | |
} | |
my @stru; | |
my @data; | |
open( my $SQLFILE, "<", $file ) | |
or croak "Can't open $file: $OS_ERROR"; | |
while (<$SQLFILE>) { | |
# nao considera linhas com comentarios e lock/unlock/drop | |
next if ( /^--/ || /^\/\*/ || /^lock/i || /^unlock/i || /^drop/i ); | |
# processa os inserts | |
if (/^(INSERT.+?)[(]/) { | |
my $ins = $1; # captura o nome da tabela | |
s/\\[']/''/g; # substitue aspas simples - \' | |
s/[)],[(]/);\n$ins(/g; # divide multiplos inserts | |
s/\\r\\n/\n/g; # Replace '\r\n' with literal newlines. | |
push( @data, $_ ); | |
} | |
# processa a estrutura | |
else { push( @stru, $_ ); } | |
} | |
close($SQLFILE); | |
my $strusql = join( '', @stru ); | |
my $datasql = join( '', @data ); | |
#open( my $STRU, ">", "stru.sql" ); # to verify the results | |
#open( my $DATA, ">", "data.sql" ); | |
#print $STRU $strusql; | |
#print $DATA $datasql; | |
# here the conversion | |
my $translator = SQL::Translator->new( | |
no_comments => 0, | |
show_warnings => 0, | |
quote_table_names => 1, | |
quote_field_names => 1, | |
validate => 1, | |
); | |
my $struout = $translator->translate( | |
from => 'MySQL', | |
to => 'SQLite', | |
data => \$strusql, | |
# filename => $file, | |
) or croak "Error: " . $translator->error; | |
# define inicio e final da transacao de inserts | |
my $prgini = "PRAGMA foreign_keys=OFF;\n"; | |
my $traini = "BEGIN TRANSACTION;\n"; | |
my $trafin = "COMMIT;\n"; | |
my $prgfin = "PRAGMA foreign_keys=ON;\n"; | |
#gera o arquivo final sqlite | |
my $sqlout = join( "\n", $struout, $prgini, $traini, $datasql, $trafin, $prgfin); | |
open( my $FINAL, ">", "/tmp/final.sql" ); | |
print $FINAL $sqlout; | |
# Monta o SQLite database | |
my $log = "/tmp/sqlite.errlog"; | |
my $command = "sqlite3 $filedb < /tmp/final.sql 2> $log"; | |
system($command) == 0 or die "system $command failed: $?"; | |
if ( -s $log ) { | |
print "*** Houve algum problema. Verifique o arquivo < /tmp/sqlite.errlog > \n"; | |
} | |
else { | |
print "*** Conversao completa. Verifique o arquivo < $filedb > \n"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment