~vcs-imports/mammoth-replicator/trunk

« back to all changes in this revision

Viewing changes to contrib/mysql/mysql2pgsql

  • Committer: alvherre
  • Date: 2005-12-16 21:24:52 UTC
  • Revision ID: svn-v4:db760fc0-0f08-0410-9d63-cc6633f64896:trunk:1
Initial import of the REL8_0_3 sources from the Pgsql CVS repository.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/perl
 
2
# mysql2pgsql
 
3
# Take a MySQL schema dump and turn it into SQL92 and PostgreSQL form.
 
4
# Thomas Lockhart, (c) 2000, PostgreSQL Inc.
 
5
# Thanks to Tim Perdue at SourceForge.Net for testing and feedback.
 
6
#
 
7
 
 
8
eval '(exit $?0)' && eval 'exec perl -S $0 ${1+"$@"}'
 
9
    & eval 'exec perl -S $0 $argv:q'
 
10
    if 0;
 
11
 
 
12
use IO::File;
 
13
use Getopt::Long;
 
14
 
 
15
my $progname = "mysql2pgsql";
 
16
my $version = "0.3";
 
17
 
 
18
GetOptions("debug!", "verbose!", "version", "path=s", "help", "data!");
 
19
 
 
20
my $debug = $opt_debug || 0;
 
21
my $verbose = $opt_verbose || 0;
 
22
my $pathfrom = $opt_path || "";
 
23
my $nodata = (! $opt_data);
 
24
 
 
25
$pathfrom = "$pathfrom/" if ($pathfrom =~ /.*[^\/]$/);
 
26
 
 
27
print "$0: $progname version $version\n"
 
28
    if ($opt_version || $opt_help);
 
29
print "\t(c) 2000 Thomas Lockhart PostgreSQL Inc.\n"
 
30
    if ($opt_version && $opt_verbose || $opt_help);
 
31
 
 
32
if ($opt_help) {
 
33
    print "$0 --verbose --version --help --path=dir --nodata infile ...\n";
 
34
    exit;
 
35
}
 
36
 
 
37
while (@ARGV) {
 
38
    my $ostem;
 
39
        my $oname;
 
40
        my $pname;
 
41
        my @xargs;
 
42
 
 
43
    $iname = shift @ARGV;
 
44
    $ostem = $iname;
 
45
    $ostem = $1 if ($ostem =~ /.+\/([^\/]+)$/);
 
46
    $ostem = $1 if ($ostem =~ /(.+)[.][^.]*$/);
 
47
 
 
48
    $oname = "$ostem.sql92";
 
49
    $pname = "$ostem.init";
 
50
 
 
51
        @xargs = ($iname, $oname);
 
52
        push @xargs, $pname unless ($nodata);
 
53
 
 
54
    print "@xargs\n" if ($debug);
 
55
 
 
56
    TransformDumpFile($iname, $oname, $pname);
 
57
}
 
58
 
 
59
exit;
 
60
 
 
61
sub TransformDumpFile {
 
62
    local ($iname, $oname, $pname) = @_;
 
63
    local @dlines;
 
64
    local @slines;
 
65
    local @plines;
 
66
    local @tables; # list of tables created
 
67
    local %pkeys;
 
68
    local %pseqs;
 
69
    local %sequences;
 
70
 
 
71
    open(IN, "<$iname") || die "Unable to open file $iname";
 
72
 
 
73
    while (<IN>) {
 
74
                chomp;
 
75
                push @dlines, $_;
 
76
    }
 
77
 
 
78
    print("Calling CreateSchema with $#dlines lines\n") if ($debug);
 
79
    @slines = CreateSchema(@dlines);
 
80
 
 
81
    open(OUT, ">$oname") || die "Unable to open output file $oname";
 
82
 
 
83
    foreach (@slines) {
 
84
                print "> $_" if ($debug);
 
85
                print OUT "$_";
 
86
    }
 
87
    close(OUT);
 
88
 
 
89
    return if (! defined($pname));
 
90
 
 
91
    @plines = PopulateSchema(@tables);
 
92
 
 
93
    open(OUT, ">$pname") || die "Unable to open output file $pname";
 
94
 
 
95
    foreach (@plines) {
 
96
                print "> $_" if ($debug);
 
97
                print OUT "$_";
 
98
    }
 
99
    close(OUT);
 
100
}
 
101
 
 
102
sub PopulateSchema {
 
103
    local @tables = @_;
 
104
    local @out;
 
105
    local $pkey;
 
106
    local $pseq;
 
107
 
 
108
    foreach (@tables) {
 
109
                $table = $_;
 
110
                $tpath = "$pathfrom$table";
 
111
 
 
112
                print "Table is $table\n" if ($debug);
 
113
                push @out, "\n";
 
114
                push @out, "copy $table from '$tpath.txt';\n";
 
115
                if (defined($pkeys{$table})) {
 
116
                        foreach ($pkeys{$table}) {
 
117
                                $pkey = $_;
 
118
                                $pseq = $pseqs{$table};
 
119
 
 
120
                                print "Key for $table is $pkey on $pseq\n" if ($debug);
 
121
 
 
122
#               //push @out, "\$value = select max($pkey) from $table;\n";
 
123
                                push @out, "select setval ('$pseq', (select max($pkey) from $table));\n";
 
124
                        }
 
125
                }
 
126
    }
 
127
 
 
128
    return @out;
 
129
}
 
130
 
 
131
sub CreateSchema {
 
132
    local @lines = @_;
 
133
    local @out;
 
134
 
 
135
#    undef $last;
 
136
    local %knames;
 
137
 
 
138
    push @out, "--\n";
 
139
    push @out, "-- Generated from mysql2pgsql\n";
 
140
    push @out, "-- (c) 2000, Thomas Lockhart, PostgreSQL Inc.\n";
 
141
    push @out, "--\n";
 
142
    push @out, "\n";
 
143
 
 
144
    while (@lines) {
 
145
                $_ = shift @lines;
 
146
                print "< $_\n" if ($debug);
 
147
                # Replace hash comments with SQL9x standard syntax
 
148
                $_ = "-- $1" if (/^[\#](.*)/);
 
149
 
 
150
                # Found a CREATE TABLE statement?
 
151
                if (/(create\s+table)\s+(\w+)\s+([(])\s*$/i) {
 
152
                        $table = $2;
 
153
                        $table = "\"$1\"" if ($table =~ /^(user)$/);
 
154
                        push @tables, $table;
 
155
                        push @tabledef, "create table $table (";
 
156
#       push @out, "$_\n";
 
157
 
 
158
                        while (@lines) {
 
159
                                $_ = shift @lines;
 
160
                                print "< $_\n" if ($debug);
 
161
 
 
162
                                # Replace int(11) with SQL9x standard syntax
 
163
                                while (/int\(\d*\)/gi) {
 
164
                                        $_ = "$`integer$'";
 
165
                                }
 
166
 
 
167
                                # Replace float(10,2) with SQL9x standard syntax
 
168
                                while (/(float)\((\d+),\s*(\d+)\)/gi) {
 
169
                                        $_ = "$`$1($2)$'";
 
170
                                }
 
171
 
 
172
                                # Replace smallinteger with SQL9x syntax
 
173
                                while (/smallinteger/gi) {
 
174
                                        $_ = "$`integer$'";
 
175
                                }
 
176
 
 
177
                                # Replace mediumtext with PostgreSQL syntax
 
178
                                while (/(longtext|mediumtext|blob|largeblob)/gi) {
 
179
                                        $_ = "$`text$'";
 
180
                                }
 
181
 
 
182
                                # Replace integer ... auto_increment with PostgreSQL syntax
 
183
                                while (/(\s*)(\w+)\s+integer\s+(.*)\s+auto_increment/gi) {
 
184
                                        $serid = $table . "_pk_seq";
 
185
                                        push @out, "-- serial identifier $serid will likely be truncated\n"
 
186
                                                if (length($serid) >= 32);
 
187
 
 
188
                                        if (length($serid) >= 32) {
 
189
                                                $excess=(length($serid)-31);
 
190
                                                $serid = substr($table,0,-($excess)) . "_pk_seq";
 
191
                                                push @out, "-- serial identifier $serid was truncated\n";
 
192
                                        }
 
193
                    push @out, "CREATE SEQUENCE $serid;\n\n";
 
194
                                        $pkeys{$table} = $2;
 
195
                                        $pseqs{$table} = $serid;
 
196
                                        push @out, "-- key is $pkeys{$table}, sequence is $pseqs{$table}\n" if ($debug);
 
197
                                        $_ = "$`$1$2 integer default nextval('$serid') $3$'";
 
198
                                }
 
199
 
 
200
                                # Replace date with double-quoted name
 
201
#           while (/^(\s*)(date|time)(\s+)/gi) {
 
202
#               $_ = "$1\"$2\"$3$'";
 
203
#           }
 
204
 
 
205
                                # Found "KEY"? Then remove it from the CREATE TABLE statement
 
206
                                # and instead write a CREATE INDEX statement.
 
207
                                if (/^\s*key\s+(\w+)\s*[(](\w[()\w\d,\s]*)[)][,]?/i) {
 
208
                                        $iname = $1;
 
209
                                        $column = $2;
 
210
                                        $iname = $1 if ($iname =~ /^idx_(\w[\_\w\d]+)/);
 
211
                                        # Sheesh, there can be upper bounds on index string sizes?
 
212
                                        # Get rid of the length specifier (e.g. filename(45) -> filename)
 
213
                                        while ($column =~ /(\w[\w\d])[(]\d+[)]/g) {
 
214
                                                $column = "$`$1$'";
 
215
                                        }
 
216
#               $column = $1 if ($column =~ /(\w+)[(]\d+[)]/);
 
217
#               push @out, "Index on $table($column) is $iname\n";
 
218
                                        if (defined($knames{$iname})) {
 
219
                                                push @out, "-- $iname already exists";
 
220
#                   sprintf($iname, "idx_%_%s", $table, $iname);
 
221
#                   $iname = "idx_" . $table . "_" . $column;
 
222
                                                # Do not bother with more to the name; it will be too big anyway
 
223
                                                $iname = $table . "_" . $column;
 
224
                                                push @out, "; use $iname instead\n";
 
225
                                        }
 
226
                                        $knames{$iname} = $iname;
 
227
                                        $keydef{$column} = $iname;
 
228
#               push @out, "! $_\n";
 
229
#               $last = $tabledef[$#tabledef];
 
230
#               push @out, "? $#tabledef $last\n";
 
231
#               push @out, "match $1\n" if ($last =~ /(.*),\s*$/);
 
232
                                        # Remove the trailing comma from the previous line, if necessary
 
233
                                        $tabledef[$#tabledef] = $1
 
234
                                                if (($#tabledef > 0) && ($tabledef[$#tabledef] =~ /(.*),\s*$/));
 
235
#               push @out, "? $tabledef[$#tabledef]\n";
 
236
 
 
237
                                        # If this is the end of the statement, save it and exit loop
 
238
                                } elsif (/^\s*[)]\;/) {
 
239
                                push @tabledef, $_;
 
240
#               push @out, "< $_\n";
 
241
                                last;
 
242
 
 
243
                                # Otherwise, just save the line
 
244
                        } else {
 
245
#               push @out, "$last\n" if (defined($last));
 
246
#               $last = $_;
 
247
                                push @tabledef, $_;
 
248
#               push @out, "$_\n";
 
249
                        }
 
250
                }
 
251
 
 
252
                        foreach $t (@tabledef) {
 
253
                                push @out, "$t\n";
 
254
                        }
 
255
                        undef @tabledef;
 
256
 
 
257
                        foreach $k (keys %keydef) {
 
258
                                push @out, "create index $keydef{$k} on $table ($k);\n";
 
259
                        }
 
260
                        undef %keydef;
 
261
 
 
262
                } else {
 
263
                        push @out, "$_\n";
 
264
                }
 
265
    }
 
266
 
 
267
#    push @out, "$last\n" if (defined($last));
 
268
 
 
269
    foreach (keys %pkeys) {
 
270
                my $val = $pkeys{$_};
 
271
                print "key is $val\n" if ($debug);
 
272
    }
 
273
 
 
274
    return @out;
 
275
}
 
276
 
 
277
sub StripComma {
 
278
    local $line = shift @_;
 
279
 
 
280
    $line = "$1" if ($line =~ /(.*)[,]\s*$/);
 
281
 
 
282
    return $line;
 
283
}