--- mysql2pgsql 2000/10/24 13:55:04 1.7 +++ mysql2pgsql 2001/02/01 15:15:48 1.8 @@ -1,9 +1,8 @@ -#!/usr/local/bin/perl -w +#!/usr/bin/perl -w # MySQL to PostgreSQL dump file converter # -# usage: -# mysqldump my_db_name | ./mysql2pgsql | psql pg_db_name +# For usage: mysqldump --help # # Convert mysqldump file (from MySQL) to something readable by psql ! # @@ -14,25 +13,64 @@ # (to set start value right) # 2000-01-11 DbP now creates sequences with correct value # 2000-04-25 DbP import into CVS (at cvs.linux.hr) +# 2001-01-29 tpo -- Tomas Pospisek : +# 1) make script comply to usage: +# 2) make script output to STDOUT instead of STERR +# 3) change verbosity behaveour +# 4) add debug option # see rest of changelog at http://cvs.linux.hr/cvsweb.cgi/sql/mysql2pgsql use DBI; -if (!defined($ARGV[0]) || !defined($ARGV[1])) { - print "Usage: $0 name_of_dump pg_database_name\n"; +$DEBUG =0; +$VERBOSE =0; + +sub usage { + print "Usage: $0 [-v|--h] mysql_dump_file [pg_database_name]\n\n"; + print "\t* mysql_dump_file is the filename of the mysql dump.\n"; + print "\t* pg_database_name is the name of the postgres database.\n"; + print "\t When ommitted standard output is used.\n"; + print "\t Combined with -v, it will tell you what SQL\n"; + print "\t commands are being executed\n"; +} + +# if mysql_dump_file is not suplied or --h is supplied then we dump usage info +if (! defined($ARGV[0]) || $ARGV[0] eq "--h") { + usage(); exit 1; } -$dump="$ARGV[0]"; -$database="$ARGV[1]"; +if ($ARGV[0] eq "-v") { + $VERBOSE = 1; + # if verbose is set then user has to supply both arguments + if(! defined($ARGV[1]) || ! defined($ARGV[2])) { + usage(); + exit 1; + } else { + $dump="$ARGV[1]"; + $database="$ARGV[1]"; + } +} else { + $dump="$ARGV[0]"; -my $dbh = DBI->connect("DBI:Pg:dbname=template1","","") || die $DBI::errstr; -$dbh->do("create database $database") || die $dbh->errstr(); -$dbh->disconnect; + # if database name is supplied then use it + if (defined($ARGV[1])) { + $database="$ARGV[1]"; + # else be verbose + } else { + $database=""; + $VERBOSE = 1; + } +} -$dbh = DBI->connect("DBI:Pg:dbname=$database","","") || die $DBI::errstr; +if ($database) { + my $dbh = DBI->connect("DBI:Pg:dbname=template1","","") || die $DBI::errstr; + $dbh->do("create database $database") || die $dbh->errstr(); + $dbh->disconnect; + $dbh = DBI->connect("DBI:Pg:dbname=$database","","") || die $DBI::errstr; +} $create=0; # inside create table? $table=""; @@ -41,6 +79,7 @@ while() { chomp; + print "Processing line: $_\n" if $DEBUG; # nuke comments or empty lines next if (/^#/ || /^$/); @@ -128,7 +167,7 @@ # remap colums with names of existing system attribute if (/"oid"/i) { s/"oid"/"_oid"/g; - print "WARNING: table $table uses column \"oid\" which is renamed to \"_oid\"\nYou should fix application manually! Press return to continue."; + print STDERR "WARNING: table $table uses column \"oid\" which is renamed to \"_oid\"\nYou should fix application manually! Press return to continue."; my $wait=; } s/oid/_oid/i if (/key/i && /oid/i); # fix oid in key @@ -172,8 +211,10 @@ $dosql.=");"; # nuked by split, put it back! if ("$dosql" ne "") { $dosql=~s/ _dotcol_ /;/g; - print STDERR "$dosql\n"; - $dbh->do("$dosql") || die "do: '$dosql' ",$dbh->errstr(); + print "$dosql\n" if $VERBOSE; + if($database) { + $dbh->do("$dosql") || die "do: '$dosql' ",$dbh->errstr(); + } } else { print STDERR "empty sql!\n"; } @@ -187,16 +228,28 @@ ($table,$field) = split(/\./,$seq); $sql="select max($field)+1 from $table"; - print STDERR "$sql\n"; - $sth = $dbh->prepare($sql) || die $dbh->errstr(); - $sth->execute() || die $sth->errstr(); - ($start) = $sth->fetchrow_array() || 1; - + print "$sql\n" if $VERBOSE; + if($database){ + $sth = $dbh->prepare($sql) || die $dbh->errstr(); + $sth->execute() || die $sth->errstr(); + ($start) = $sth->fetchrow_array() || 1; + } else { + print STDERR<do("create sequence $seq start $start increment 1") || die $dbh->errstr(); + print "$sql\n" if $VERBOSE; + if($database){ + $dbh->do("create sequence $seq start $start increment 1") || die $dbh->errstr(); + } } print "\n";