/[sql]/mysql2pgsql
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /mysql2pgsql

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.1.1.1 by dpavlin, Mon Mar 6 07:13:23 2000 UTC revision 1.5 by dpavlin, Tue Oct 24 11:40:45 2000 UTC
# Line 1  Line 1 
1  #!/usr/local/bin/perl  #!/usr/local/bin/perl -w
2    
3  # MySQL to PostgreSQL dump file converter  # MySQL to PostgreSQL dump file converter
4  #  #
# Line 11  Line 11 
11  # 1999-12-26 DbP don't make serial from auto_increment, create all manually  # 1999-12-26 DbP don't make serial from auto_increment, create all manually
12  #                (to set start value right)  #                (to set start value right)
13  # 2000-01-11 DbP now creates sequences with correct value  # 2000-01-11 DbP now creates sequences with correct value
14    # 2000-04-25 DbP import into CVS (at cvs.linux.hr)
15    
16  use DBI;  use DBI;
17    
# Line 61  while(<DUMP>) { Line 62  while(<DUMP>) {
62                          if (/^\s*(\w+)\s+/) {                          if (/^\s*(\w+)\s+/) {
63                                  $seq="${table}_${1}_seq";                                  $seq="${table}_${1}_seq";
64                                  push @sequences,"$table.$1";                                  push @sequences,"$table.$1";
65                                  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;
66                          } else {                          } else {
67                                  die "can't get name of field!";                                  die "can't get name of field!";
68                          }                          }
# Line 85  while(<DUMP>) { Line 86  while(<DUMP>) {
86    
87                  # blob -> text                  # blob -> text
88                  s/\w*blob/text/gi;                  s/\w*blob/text/gi;
89                    # tinytext -> text
90                    s/tinytext/text/gi;
91    
92                  # char -> varchar                  # char -> varchar
93                  # PostgreSQL would otherwise pad with spaces as opposed                  # PostgreSQL would otherwise pad with spaces as opposed
# Line 93  while(<DUMP>) { Line 96  while(<DUMP>) {
96    
97                  # nuke date representation (not supported in PostgreSQL)                  # nuke date representation (not supported in PostgreSQL)
98                  s/datetime default '[^']+'/datetime/i;                  s/datetime default '[^']+'/datetime/i;
99                    s/date default '[^']+'/datetime/i;
100                    s/time default '[^']+'/datetime/i;
101    
102                  # change not null datetime filend to null valid ones                  # change not null datetime field to null valid ones
103                  # (to support remapping of "zaro time" to null                  # (to support remapping of "zero time" to null
104                  s/datetime not null/datetime/i;                  s/datetime not null/datetime/i;
105    
106                    # nuke size of timestamp
107                    s/timestamp\([^)]*\)/timestamp/i;
108    
109                    # double -> float8
110                    s/double\([^)]*\)/float8/i;
111    
112                    # add unique to definition of type (MySQL separates this)
113                    if (/unique \w+ \((\w+)\)/i) {
114                            $sql=~s/($1)([^,]+)/$1$2 unique/i;
115                            next;
116                    }
117    
118                    # quote column names
119                    s/(^\s*)(\S+)(\s*)/$1"$2"$3/gi if (!/key/i);
120    
121            } else {        # not inside create table
122    
123                  #---- fix data in inserted data: (from MS world)                  #---- fix data in inserted data: (from MS world)
124                  # FIX: disabled for now                  # FIX: disabled for now
125                  if (00 && /insert into/i) {                  if (00 && /insert into/i) {
# Line 108  while(<DUMP>) { Line 130  while(<DUMP>) {
130                          s!\x92!`!g;                          s!\x92!`!g;
131                  }                  }
132    
         } else {        # not inside create table  
   
133                  # fix dates '0000-00-00 00:00:00' (should be null)                  # fix dates '0000-00-00 00:00:00' (should be null)
134                  s/'0000-00-00 00:00:00'/null/gi;                  s/'0000-00-00 00:00:00'/null/gi;
135                    s/'0000-00-00'/null/gi;
136                    s/'00:00:00'/null/gi;
137                    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'/;
138          }          }
139    
140          $sql.="$_";          $sql.="$_";
# Line 146  foreach $seq (@sequences) { Line 169  foreach $seq (@sequences) {
169          print STDERR "$sql\n";          print STDERR "$sql\n";
170          $sth = $dbh->prepare($sql) || die $dbh->errstr();          $sth = $dbh->prepare($sql) || die $dbh->errstr();
171          $sth->execute() || die $sth->errstr();          $sth->execute() || die $sth->errstr();
172          ($start) = $sth->fetchrow_array();          ($start) = $sth->fetchrow_array() || 1;
173    
174          $seq="${table}_${field}_seq";          $seq="${table}_${field}_seq";
175    

Legend:
Removed from v.1.1.1.1  
changed lines
  Added in v.1.5

  ViewVC Help
Powered by ViewVC 1.1.26