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.
8
eval '(exit $?0)' && eval 'exec perl -S $0 ${1+"$@"}'
9
& eval 'exec perl -S $0 $argv:q'
15
my $progname = "mysql2pgsql";
18
GetOptions("debug!", "verbose!", "version", "path=s", "help", "data!");
20
my $debug = $opt_debug || 0;
21
my $verbose = $opt_verbose || 0;
22
my $pathfrom = $opt_path || "";
23
my $nodata = (! $opt_data);
25
$pathfrom = "$pathfrom/" if ($pathfrom =~ /.*[^\/]$/);
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);
33
print "$0 --verbose --version --help --path=dir --nodata infile ...\n";
45
$ostem = $1 if ($ostem =~ /.+\/([^\/]+)$/);
46
$ostem = $1 if ($ostem =~ /(.+)[.][^.]*$/);
48
$oname = "$ostem.sql92";
49
$pname = "$ostem.init";
51
@xargs = ($iname, $oname);
52
push @xargs, $pname unless ($nodata);
54
print "@xargs\n" if ($debug);
56
TransformDumpFile($iname, $oname, $pname);
61
sub TransformDumpFile {
62
local ($iname, $oname, $pname) = @_;
66
local @tables; # list of tables created
71
open(IN, "<$iname") || die "Unable to open file $iname";
78
print("Calling CreateSchema with $#dlines lines\n") if ($debug);
79
@slines = CreateSchema(@dlines);
81
open(OUT, ">$oname") || die "Unable to open output file $oname";
84
print "> $_" if ($debug);
89
return if (! defined($pname));
91
@plines = PopulateSchema(@tables);
93
open(OUT, ">$pname") || die "Unable to open output file $pname";
96
print "> $_" if ($debug);
110
$tpath = "$pathfrom$table";
112
print "Table is $table\n" if ($debug);
114
push @out, "copy $table from '$tpath.txt';\n";
115
if (defined($pkeys{$table})) {
116
foreach ($pkeys{$table}) {
118
$pseq = $pseqs{$table};
120
print "Key for $table is $pkey on $pseq\n" if ($debug);
122
# //push @out, "\$value = select max($pkey) from $table;\n";
123
push @out, "select setval ('$pseq', (select max($pkey) from $table));\n";
139
push @out, "-- Generated from mysql2pgsql\n";
140
push @out, "-- (c) 2000, Thomas Lockhart, PostgreSQL Inc.\n";
146
print "< $_\n" if ($debug);
147
# Replace hash comments with SQL9x standard syntax
148
$_ = "-- $1" if (/^[\#](.*)/);
150
# Found a CREATE TABLE statement?
151
if (/(create\s+table)\s+(\w+)\s+([(])\s*$/i) {
153
$table = "\"$1\"" if ($table =~ /^(user)$/);
154
push @tables, $table;
155
push @tabledef, "create table $table (";
160
print "< $_\n" if ($debug);
162
# Replace int(11) with SQL9x standard syntax
163
while (/int\(\d*\)/gi) {
167
# Replace float(10,2) with SQL9x standard syntax
168
while (/(float)\((\d+),\s*(\d+)\)/gi) {
172
# Replace smallinteger with SQL9x syntax
173
while (/smallinteger/gi) {
177
# Replace mediumtext with PostgreSQL syntax
178
while (/(longtext|mediumtext|blob|largeblob)/gi) {
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);
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";
193
push @out, "CREATE SEQUENCE $serid;\n\n";
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$'";
200
# Replace date with double-quoted name
201
# while (/^(\s*)(date|time)(\s+)/gi) {
202
# $_ = "$1\"$2\"$3$'";
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) {
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) {
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";
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";
237
# If this is the end of the statement, save it and exit loop
238
} elsif (/^\s*[)]\;/) {
240
# push @out, "< $_\n";
243
# Otherwise, just save the line
245
# push @out, "$last\n" if (defined($last));
252
foreach $t (@tabledef) {
257
foreach $k (keys %keydef) {
258
push @out, "create index $keydef{$k} on $table ($k);\n";
267
# push @out, "$last\n" if (defined($last));
269
foreach (keys %pkeys) {
270
my $val = $pkeys{$_};
271
print "key is $val\n" if ($debug);
278
local $line = shift @_;
280
$line = "$1" if ($line =~ /(.*)[,]\s*$/);