Update to my2pg 1.28, from:
http://www.omnistarinc.com/~fonin/downloads.php#my2pg
This commit is contained in:
parent
52e4f27af9
commit
085fa8ac48
@ -35,17 +35,26 @@
|
||||
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
# SUCH DAMAGE.
|
||||
#
|
||||
# $My2pg: my2pg.pl,v 1.27 2001/12/06 19:32:20 fonin Exp $
|
||||
# $PostgreSQL: pgsql/contrib/mysql/my2pg.pl,v 1.11 2003/11/29 22:39:26 pgsql Exp $
|
||||
# $My2pg: my2pg.pl,v 1.28 2001/12/06 19:32:20 fonin Exp $
|
||||
# $Id: my2pg.pl,v 1.12 2004/04/19 23:11:49 momjian Exp $
|
||||
|
||||
#
|
||||
# $Log: my2pg.pl,v $
|
||||
# Revision 1.11 2003/11/29 22:39:26 pgsql
|
||||
# Revision 1.12 2004/04/19 23:11:49 momjian
|
||||
# Update to my2pg 1.28, from:
|
||||
#
|
||||
# make sure the $Id tags are converted to $PostgreSQL as well ...
|
||||
# http://www.omnistarinc.com/~fonin/downloads.php#my2pg
|
||||
#
|
||||
# Revision 1.10 2003/01/07 22:18:43 momjian
|
||||
# Upgrade to my2pg 1.9
|
||||
# Revision 1.28 2002/11/30 12:03:48 fonin
|
||||
# PostgreSQL does not support indexes on the partial length of column,
|
||||
# e.g.
|
||||
# CREATE INDEX i_index ON table (column(16));
|
||||
# will not work. Fixed.
|
||||
#
|
||||
# Added command-line option -s that prevents my2pg from attempting convert
|
||||
# the data (currently only timestamps).
|
||||
#
|
||||
# Better timestamps conversion.
|
||||
#
|
||||
# Revision 1.27 2002/07/16 14:54:07 fonin
|
||||
# Bugfix - didn't quote the fields inside PRIMARY KEY with -d option.
|
||||
@ -162,7 +171,7 @@ my $chareg=''; # CHAR conversion regexps
|
||||
my $dq=''; # double quote
|
||||
|
||||
# parse command line
|
||||
getopts('nhd',\%opts);
|
||||
getopts('nhds',\%opts);
|
||||
|
||||
# output syntax
|
||||
if($opts{h} ne '') {
|
||||
@ -179,10 +188,17 @@ if($opts{d} ne '') {
|
||||
$dq='"';
|
||||
}
|
||||
|
||||
if($opts{s} ne '') {
|
||||
$safe_data_conv=1;
|
||||
}
|
||||
else {
|
||||
$safe_data_conv=0;
|
||||
}
|
||||
|
||||
$|=1;
|
||||
|
||||
print("------------------------------------------------------------------");
|
||||
print("\n-- My2Pg 1.27 translated dump");
|
||||
print("\n-- My2Pg 1.28 translated dump");
|
||||
print("\n--");
|
||||
print("\n------------------------------------------------------------------");
|
||||
|
||||
@ -213,7 +229,6 @@ print LIBTYPES "\n#define ADD_COMMA if(strcmp(result,\"\")!=0) strcat(result,\",
|
||||
# reading STDIN...
|
||||
my $tabledef=0; # we are outside a table definition
|
||||
while (<>) {
|
||||
|
||||
if(!$tabledef && /^CREATE TABLE \S+/i){
|
||||
$tabledef=1;
|
||||
} elsif($tabledef && /^\) type=\w*;/i){ # /^\w/i
|
||||
@ -225,7 +240,7 @@ while (<>) {
|
||||
s/#/--/;
|
||||
}
|
||||
|
||||
if($tabledef){##################################
|
||||
if($tabledef) {
|
||||
# Convert numeric types
|
||||
s/tinyint\(\d+\)/INT2/i;
|
||||
s/smallint\(\d+\)/INT2/i;
|
||||
@ -254,7 +269,10 @@ while (<>) {
|
||||
s/datetime/TIMESTAMP/;
|
||||
s/timestamp\(\d+\)/TIMESTAMP/i;
|
||||
s/ date / DATE /i;
|
||||
if((/date/ig || /time/ig) && /[,(]\d{4}(\d{2})(\d{2})[,)]/ &&
|
||||
$1>=0 && $1<=12 && $2>=0 && $2<=31) {
|
||||
s/,(\d{4})(\d{2})(\d{2}),/,'$1-$2-$3 00:00:00',/g;
|
||||
}
|
||||
|
||||
# small hack - convert "default" to uppercase, because below we
|
||||
# enclose all lowercase words in double quotes
|
||||
@ -268,7 +286,6 @@ while (<>) {
|
||||
$seq{$table_name}=$tmpseq;
|
||||
$primary{$table_name}=$+;
|
||||
s/(int.*?) .*AUTO_INCREMENT/$1 DEFAULT nextval\('$tmpseq'\)/i;
|
||||
#s/(int.*?)DEFAULT\s*?'.*?'(.*?)AUTO_INCREMENT/$1$2DEFAULT nextval\('$tmpseq'\)/i;
|
||||
}
|
||||
|
||||
# convert UNSIGNED to CHECK constraints
|
||||
@ -290,7 +307,6 @@ while (<>) {
|
||||
}
|
||||
# forming identifier name
|
||||
$typename=new_name('enum_'.$table_name.'_'.$item[1],28);
|
||||
# $typename=lc('enum_'.$table_name.'_'.$item[1]);
|
||||
# creating input type function
|
||||
my $func_in="
|
||||
int2* $typename"."_in (char *str) {
|
||||
@ -482,7 +498,6 @@ CREATE OPERATOR <> (
|
||||
}
|
||||
$internalsize=$typesize;
|
||||
$typesize='int'.$typesize;
|
||||
# $typename=lc('set_'.$table_name.'_'.$item[1]);
|
||||
$typename=new_name('set_'.$table_name.'_'.$item[1],28);
|
||||
# creating input type function
|
||||
my $func_in="
|
||||
@ -669,34 +684,43 @@ CREATE OPERATOR <> (
|
||||
if(/^\s*KEY ([\w\d_]+)\s*\((.*)\).*/i) {
|
||||
my $tmpfld=$2; my $ky=$1;
|
||||
$tmpfld=~s/\s*,\s*/","/g if $dq;
|
||||
$tmpfld=~s/(\(\d+\))//g;
|
||||
$index{$table_name}[++$j]="CREATE INDEX ${ky}_$table_name\_index ON $dq$table_name$dq ($dq$tmpfld$dq);";
|
||||
}
|
||||
if(/^\s*UNIQUE.*?([\w\d_]+)\s*\((.*)\).*/i) {
|
||||
my $tmpfld=$2; my $ky=$1;
|
||||
$tmpfld=~s/,/","/g if $dq;
|
||||
$tmpfld=~s/(\(\d+\))//g;
|
||||
$index{$table_name}[++$j]="CREATE UNIQUE INDEX ${ky}_$table_name\_index ON $dq$table_name$dq ($dq$tmpfld$dq);";
|
||||
}
|
||||
s/^\s*UNIQUE (.+).*(\(.*\)).*\n//i;
|
||||
s/^\s*KEY (.+).*(\(.*\)).*\n//i;
|
||||
|
||||
if($dq && !/^\s*(PRIMARY KEY|UNIQUE |KEY |CREATE TABLE |\);)/i){
|
||||
if($dq && !/^\s*(PRIMARY KEY|UNIQUE |KEY |CREATE TABLE|INSERT INTO|\);)/i) {
|
||||
s/\s([A-Za-z_\d]+)\s/ $dq$+$dq /;
|
||||
}
|
||||
} ####if($tabledef)###############################
|
||||
} # end of if($tabledef)
|
||||
|
||||
if($dq && !s/INSERT INTO\s+?(.*?)\s+?/INSERT INTO $dq$1$dq /i) {
|
||||
# Quote lowercase identifiers in double quotes
|
||||
#while(!/^--/ && s/\s([A-Za-z_\d]+[a-z][A-Za-z_\d]*)\s/ $dq$+$dq /) {;}
|
||||
}
|
||||
s/INSERT INTO\s+?(.*?)\s+?/INSERT INTO $dq$1$dq /i;
|
||||
|
||||
# if not defined -s command-line option (safe data conversion),
|
||||
# attempting to convert timestamp data
|
||||
if(!$safe_data_conv) {
|
||||
# Fix timestamps
|
||||
s/'0000-00-00/'0001-01-01/g;
|
||||
# may work wrong !!!
|
||||
# may corrupt data !!!
|
||||
s/([,(])00000000000000(?=[,)])/$1'00010101 000000'/g;
|
||||
if(/[,(]\d{4}(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})[,)]/ &&
|
||||
$1>=0 && $1<=12 && $2>=0 && $2<=31 && $3>=0 && $3<=23 &&
|
||||
$4>=0 && $4<=59 && $5>=0 && $5<=59) {
|
||||
s/([,(])(\d{8})(\d{6})(?=[,)])/$1'$2 $3'/g;
|
||||
}
|
||||
if(/[,(]\d{4}(\d{2})(\d{2})[,)]/ &&
|
||||
$2>=0 && $2<=12 && $3>=0 && $3<=31) {
|
||||
s/([,(])(\d{4})(\d{2})(\d{2})(?=[,)])/$1'$2-$3-$4 00:00:00'/g;
|
||||
#<Hackzone> ---------------------------------------------------
|
||||
#</Hackzone> --------------------------------------------------
|
||||
}
|
||||
}
|
||||
|
||||
$dump.=$_;
|
||||
}
|
||||
|
||||
@ -738,7 +762,7 @@ close(LIBTYPES);
|
||||
|
||||
open(MAKE,">Makefile");
|
||||
print MAKE "#
|
||||
# My2Pg \$Revision: 1.11 $ \translated dump
|
||||
# My2Pg \$Revision: 1.12 $ \translated dump
|
||||
# Makefile
|
||||
#
|
||||
|
||||
@ -784,7 +808,7 @@ sub usage() {
|
||||
print <<EOF
|
||||
my2pg - MySQL to PostgreSQL database dump converter
|
||||
|
||||
Copyright (c) 2000 Max Rudensky <fonin\@ziet.zhitomir.ua>
|
||||
Copyright (c) 2000-2002 Max Rudensky <fonin\@ziet.zhitomir.ua>
|
||||
Copyright (c) 2000 Valentine Danilchuk <valdan\@ziet.zhitomir.ua>
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
@ -793,12 +817,13 @@ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
code source for license details.
|
||||
|
||||
SYNTAX:
|
||||
my2pg [-hnd]
|
||||
my2pg [-hnds]
|
||||
|
||||
OPTIONS:
|
||||
h - this help
|
||||
n - convert *CHAR NOT NULL DEFAULT '' types to *CHAR NULL
|
||||
d - double quotes around table and column names
|
||||
s - do not attempt to convert data (timestamps at the moment)
|
||||
EOF
|
||||
;
|
||||
}
|
||||
@ -810,7 +835,7 @@ my2pg - MySQL -> PostgreSQL dump conversion utility.
|
||||
|
||||
=head1 SYNTAX
|
||||
|
||||
mysqldump db | ./my2pg.pl [-n] > pgsqldump.sql
|
||||
mysqldump db | ./my2pg.pl [-nds] > pgsqldump.sql
|
||||
vi libtypes.c
|
||||
make
|
||||
psql database < pgsqldump.txt
|
||||
@ -818,11 +843,11 @@ where
|
||||
|
||||
=over 4
|
||||
|
||||
=item B<pgsqldump.sql>
|
||||
=item F<pgsqldump.sql>
|
||||
|
||||
- file suitable for loading into PostgreSQL.
|
||||
|
||||
=item B<libtypes.c>
|
||||
=item F<libtypes.c>
|
||||
|
||||
- C source for emulated MySQL types (ENUM, SET) generated by B<my2pg>
|
||||
|
||||
@ -835,7 +860,7 @@ B<my2pg> performs such conversions:
|
||||
|
||||
=over 4
|
||||
|
||||
=item Type conversion.
|
||||
=item * Type conversion.
|
||||
|
||||
It tries to find proper Postgres
|
||||
type for each column.
|
||||
@ -844,30 +869,30 @@ ENUM and SET types implemented via user types
|
||||
(C source for such types can be found in
|
||||
B<libtypes.c> file);
|
||||
|
||||
=item Identifiers double-quotation.
|
||||
=item * Encloses identifiers into double quotes.
|
||||
|
||||
All column and table
|
||||
names should be enclosed to double-quotes to prevent
|
||||
interferension with reserved words;
|
||||
conflict with reserved SQL keywords;
|
||||
|
||||
=item Converting
|
||||
=item * Converting
|
||||
|
||||
AUTO_INCREMENT fields to SERIAL. Actually, creating the sequence and
|
||||
setting default value to nextval('seq'), well, you know :)
|
||||
|
||||
=item Converting
|
||||
=item * Converting
|
||||
|
||||
KEY(field) to CREATE INDEX i_field on table (field);
|
||||
|
||||
=item The same
|
||||
=item * The same
|
||||
|
||||
for UNIQUE keys;
|
||||
|
||||
=item Indices
|
||||
=item * Indices
|
||||
|
||||
are creating AFTER rows insertion (to speed up the load);
|
||||
|
||||
=item Translates '#'
|
||||
=item * Translates '#'
|
||||
|
||||
MySQL comments to ANSI SQL '--'
|
||||
|
||||
@ -895,18 +920,23 @@ Add double quotes around table and column names
|
||||
|
||||
Show usage banner.
|
||||
|
||||
=item -s
|
||||
|
||||
Do not attempt to convert data. Currently my2pg only tries to convert
|
||||
date and time data.
|
||||
|
||||
=back
|
||||
|
||||
=head1 SIDE EFFECTS
|
||||
|
||||
=over 4
|
||||
|
||||
=item creates
|
||||
=item * creates
|
||||
|
||||
file B<libtypes.c> in current directory
|
||||
overwriting existed file without any checks;
|
||||
|
||||
=item the same
|
||||
=item * the same
|
||||
|
||||
for Makefile.
|
||||
|
||||
@ -914,26 +944,42 @@ for Makefile.
|
||||
|
||||
=head1 BUGS
|
||||
|
||||
This program is still beta. Testers wanted.
|
||||
Known bugs are:
|
||||
|
||||
=over 4
|
||||
|
||||
=item Poor doublequotation.
|
||||
=item * Possible problems with the timestamp data.
|
||||
|
||||
PostgreSQL does not accept incorrect date/time values like B<2002-00-15>,
|
||||
while MySQL does not care about that. Currently my2pg cannot handle this
|
||||
issue. You should care yourself to convert such a data.
|
||||
|
||||
=item * Use -s option if your numeric data are broken during conversion.
|
||||
|
||||
My2pg attempts to convert MySQL timestamps of the form B<yyyymmdd> to
|
||||
B<yyyy-mm-dd> and B<yyyymmddhhmmss> to B<yyyy-mm-dd hh:mm:ss>. It performs
|
||||
some heuristic checks to ensure that the month,day,hour,minutes and seconds have
|
||||
values from the correct range (0..12, 0..31, 0..23, 0..59, 0..59 respectively).
|
||||
It is still possible that your numeric values that satisfy these conditions
|
||||
will get broken.
|
||||
|
||||
=item * Possible problems with enclosing identifiers in double quotes.
|
||||
|
||||
All identifiers such as table and column names should be enclosed in double
|
||||
quotes. Program can't handle upper-case identifiers,
|
||||
like DBA. Lower-case identifiers are OK.
|
||||
|
||||
=item SET type emulation is not full. LIKE operation on
|
||||
=item * SET type emulation is not full. LIKE operation on
|
||||
|
||||
SETs, raw integer input values should be implemented
|
||||
|
||||
=item B<Makefile> generated during output is
|
||||
=item * B<Makefile>
|
||||
|
||||
generated during output is
|
||||
platform-dependent and surely works only on
|
||||
Linux/gcc (FreeBSD/gcc probably works as well - not tested)
|
||||
|
||||
=item Generated B<libtypes.c> contain line
|
||||
=item * Generated B<libtypes.c> contain line
|
||||
|
||||
#include <postgres.h>
|
||||
|
||||
@ -944,16 +990,25 @@ include path, you need to check it before compiling.
|
||||
|
||||
=head1 AUTHORS
|
||||
|
||||
B<(c) 2000 Maxim V. Rudensky (fonin@ziet.zhitomir.ua)>
|
||||
B<(c) 2000 Valentine V. Danilchuk (valdan@ziet.zhitomir.ua)>
|
||||
B<(c) 2000-2002 Maxim V. Rudensky (fonin@ziet.zhitomir.ua)> (developer, maintainer)
|
||||
|
||||
B<(c) 2000 Valentine V. Danilchuk (valdan@ziet.zhitomir.ua)> (original script)
|
||||
|
||||
=head1 CREDITS
|
||||
|
||||
Great thanks to all those people who provided feedback and make development
|
||||
of this tool easier.
|
||||
|
||||
Jeff Waugh <jaw@ic.net>
|
||||
|
||||
Joakim Lemström <jocke@bytewize.com> || <buddyh19@hotmail.com>
|
||||
|
||||
Yunliang Yu <yu@math.duke.edu>
|
||||
|
||||
Brad Hilton <bhilton@vpop.net>
|
||||
|
||||
If you are not listed here please write to me.
|
||||
|
||||
=head1 LICENSE
|
||||
|
||||
B<BSD>
|
||||
|
Loading…
x
Reference in New Issue
Block a user