--- mysql2pgsql 2000/03/06 07:13:23 1.1.1.1 +++ mysql2pgsql 2000/10/24 11:51:00 1.6 @@ -1,4 +1,4 @@ -#!/usr/local/bin/perl +#!/usr/local/bin/perl -w # MySQL to PostgreSQL dump file converter # @@ -6,11 +6,16 @@ # mysqldump my_db_name | ./mysql2pgsql | psql pg_db_name # # Convert mysqldump file (from MySQL) to something readable by psql ! +# +# homepage: http://www.rot13.org/~dpavlin/projects.html -# 1999-12-15 DbP -- Dobrica Pavlinusic +# 1999-12-15 DbP -- Dobrica Pavlinusic # 1999-12-26 DbP don't make serial from auto_increment, create all manually # (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) + +# see rest of changelog at http://cvs.linux.hr/cvsweb.cgi/sql/mysql2pgsql use DBI; @@ -61,7 +66,7 @@ if (/^\s*(\w+)\s+/) { $seq="${table}_${1}_seq"; push @sequences,"$table.$1"; - s/\w*int.*auto_increment/int4 default nextval('$seq') not null/ig; + s/(\w+) \w*int.*auto_increment/$1 int4 default nextval('$seq') not null/ig; } else { die "can't get name of field!"; } @@ -85,6 +90,8 @@ # blob -> text s/\w*blob/text/gi; + # tinytext -> text + s/tinytext/text/gi; # char -> varchar # PostgreSQL would otherwise pad with spaces as opposed @@ -93,11 +100,30 @@ # nuke date representation (not supported in PostgreSQL) s/datetime default '[^']+'/datetime/i; + s/date default '[^']+'/datetime/i; + s/time default '[^']+'/datetime/i; - # change not null datetime filend to null valid ones - # (to support remapping of "zaro time" to null + # change not null datetime field to null valid ones + # (to support remapping of "zero time" to null s/datetime not null/datetime/i; + # nuke size of timestamp + s/timestamp\([^)]*\)/timestamp/i; + + # double -> float8 + s/double\([^)]*\)/float8/i; + + # add unique to definition of type (MySQL separates this) + if (/unique \w+ \((\w+)\)/i) { + $sql=~s/($1)([^,]+)/$1$2 unique/i; + next; + } + + # quote column names + s/(^\s*)(\S+)(\s*)/$1"$2"$3/gi if (!/key/i); + + } else { # not inside create table + #---- fix data in inserted data: (from MS world) # FIX: disabled for now if (00 && /insert into/i) { @@ -108,10 +134,11 @@ s!\x92!`!g; } - } else { # not inside create table - # fix dates '0000-00-00 00:00:00' (should be null) s/'0000-00-00 00:00:00'/null/gi; + s/'0000-00-00'/null/gi; + s/'00:00:00'/null/gi; + s/([12]\d\d\d)([01]\d)([0-3]\d)([0-2]\d)([0-6]\d)([0-6]\d)/'$1-$2-$3 $4:$5:$6'/; } $sql.="$_"; @@ -146,7 +173,7 @@ print STDERR "$sql\n"; $sth = $dbh->prepare($sql) || die $dbh->errstr(); $sth->execute() || die $sth->errstr(); - ($start) = $sth->fetchrow_array(); + ($start) = $sth->fetchrow_array() || 1; $seq="${table}_${field}_seq";