From 86109c5536e6dad0e21369db224def742bde0fb4 Mon Sep 17 00:00:00 2001 From: root Date: Wed, 12 Aug 2020 12:41:27 +0200 Subject: [PATCH 1/4] escape all columns name in create table --- pg2mysql.inc.php | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/pg2mysql.inc.php b/pg2mysql.inc.php index 752391f..89586a3 100644 --- a/pg2mysql.inc.php +++ b/pg2mysql.inc.php @@ -246,16 +246,14 @@ function pg2mysql($input, $header=true) // Remove defaults pointing to functions $line=preg_replace("/ DEFAULT .*\(\)/", "", $line); + $field=getfieldname($line); + if (strstr($line, "auto_increment")) { - $field=getfieldname($line); $tbl_extra.=", " . $config['autoincrement_key_type'] . "(`$field`)\n"; } - $specialfields=array("repeat","status","type","call", "key", "regexp"); - - $field=getfieldname($line); - if (in_array($field, $specialfields)) { - $line=str_replace("$field ", "`$field` ", $line); + if ($field && $field!=')') { + $line=preg_replace("/(^\s*)`?(${field})`?/", '${1}`${2}`', $line); } //text/blob fields are not allowed to have a default, so if we find a text DEFAULT, change it to varchar(255) DEFAULT @@ -264,11 +262,10 @@ function pg2mysql($input, $header=true) } //just skip a CONSTRAINT line - if (strstr($line, " CONSTRAINT ")) { + if ($field == 'CONSTRAINT') { $line=""; //and if the previous output ended with a , remove the , $lastchr=substr($output, -2, 1); - // echo "lastchr=$lastchr"; if ($lastchr==",") { $output=substr($output, 0, -2)."\n"; } From 77f50f3e883d10e7473182107e1a8bb94b4c3c9d Mon Sep 17 00:00:00 2001 From: root Date: Wed, 12 Aug 2020 14:16:25 +0200 Subject: [PATCH 2/4] support create table inherits --- pg2mysql.inc.php | 53 +++++++++++++++++++++++++++++++++++------------- 1 file changed, 39 insertions(+), 14 deletions(-) diff --git a/pg2mysql.inc.php b/pg2mysql.inc.php index 89586a3..25c32a8 100644 --- a/pg2mysql.inc.php +++ b/pg2mysql.inc.php @@ -31,6 +31,9 @@ $config['engine']="InnoDB"; $config['autoincrement_key_type'] = getenv("PG2MYSQL_AUTOINCREMENT_KEY_TYPE") ? getenv("PG2MYSQL_AUTOINCREMENT_KEY_TYPE") : "PRIMARY KEY"; +$tables = array(); +$tables_extra = array(); + // Timezone to use date_default_timezone_set('UTC'); @@ -139,6 +142,7 @@ function pg2mysql_large($infilename, $outfilename) function pg2mysql($input, $header=true) { global $config; + global $tables_col, $tables_extra; if (is_array($input)) { $lines=$input; @@ -156,28 +160,49 @@ function pg2mysql($input, $header=true) } $in_create_table = $in_insert = false; - + $create_table_name=""; $linenumber=0; - $tbl_extra=""; + $table_col = $table_extra=""; while (isset($lines[$linenumber])) { $line=$lines[$linenumber]; if (substr($line, 0, 12)=="CREATE TABLE") { - $in_create_table=true; $line=str_replace("\"", "`", $line); - $output.=$line; + preg_match("/CREATE TABLE `?([^\ ]*)`?/", $line, $regs); + $create_table_name = $regs[1]; + $in_create_table=true; + $tables_col[$create_table_name] = ''; $linenumber++; continue; } + if ($in_create_table && trim($line)==")") { + preg_match('/INHERITS \("?([^\ ]*)"?\);/', $lines[$linenumber+1], $regs); + $inherits_from=$regs[1]; + if ($inherits_from) { + $parts = explode("\n", $output, 2); + $table_col = "-- inherited from $inherits_from {\n" . + $tables_col[$inherits_from] . ($table_col ? ',' : '') . + "-- }\n" . + $table_col; + $table_extra = "-- inherited from $inherits_from {\n" . + $tables_extra[$inherits_from] . ($table_extra ? ',' : '') . + "-- }\n" . + $table_extra; + $linenumber++; + $line=");\n"; + } + } if (substr($line, 0, 2)==");" && $in_create_table) { $in_create_table=false; $line=") ENGINE={$config['engine']};\n\n"; - $output.=$tbl_extra; - $output.=$line; + $output.= "CREATE TABLE `$create_table_name` (\n" . $table_col . $table_extra . $line; + + $tables_col[$create_table_name] = $table_col; + $tables_extra[$create_table_name] = $table_extra; $linenumber++; - $tbl_extra=""; + $table_col = $table_extra=""; continue; } @@ -249,7 +274,7 @@ function pg2mysql($input, $header=true) $field=getfieldname($line); if (strstr($line, "auto_increment")) { - $tbl_extra.=", " . $config['autoincrement_key_type'] . "(`$field`)\n"; + $table_extra.=", " . $config['autoincrement_key_type'] . "(`$field`)\n"; } if ($field && $field!=')') { @@ -264,14 +289,14 @@ function pg2mysql($input, $header=true) //just skip a CONSTRAINT line if ($field == 'CONSTRAINT') { $line=""; - //and if the previous output ended with a , remove the , - $lastchr=substr($output, -2, 1); + //and if the previous $table_col ended with a , remove the , + $lastchr=substr($table_col, -2, 1); if ($lastchr==",") { - $output=substr($output, 0, -2)."\n"; + $table_col=substr($table_col, 0, -2)."\n"; } } - $output.=$line; + $table_col.=$line; } if (substr($line, 0, 11)=="INSERT INTO") { @@ -352,9 +377,9 @@ function pg2mysql($input, $header=true) preg_match('/CREATE INDEX "?([a-zA-Z0-9_]*)"? ON "?([a-zA-Z0-9_]*)"? USING btree \((.*)\);/', $line, $matches); if (isset($matches[1]) && isset($matches[2]) && isset($matches[3])) { $indexname=$matches[1]; - $tablename=$matches[2]; + $create_table_name=$matches[2]; $columns=str_replace("\"", "`", $matches[3]); - $output.="ALTER TABLE `{$tablename}` ADD INDEX ( {$columns} ) ;\n"; + $output.="ALTER TABLE `{$create_table_name}` ADD INDEX ( {$columns} ) ;\n"; } } From 80a97c01952cd580610e967d3a7f6362d4911636 Mon Sep 17 00:00:00 2001 From: root Date: Wed, 12 Aug 2020 15:19:41 +0200 Subject: [PATCH 3/4] allow varchar & char to have up to 65000 chars --- pg2mysql.inc.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pg2mysql.inc.php b/pg2mysql.inc.php index 25c32a8..33e70c1 100644 --- a/pg2mysql.inc.php +++ b/pg2mysql.inc.php @@ -220,7 +220,7 @@ function pg2mysql($input, $header=true) $line=str_replace("` `text`", "` text", $line); // fix because pg_dump quotes text type for some reason if (preg_match("/ character varying\(([0-9]*)\)/", $line, $regs)) { $num=$regs[1]; - if ($num<=255) { + if ($num<=65000) { # Pattern delimniter "/" fails here. Use alternatively "|". $line=preg_replace("| character varying\([0-9]*\)|", " varchar($num)", $line); } else { @@ -248,7 +248,7 @@ function pg2mysql($input, $header=true) $line=preg_replace("/::.*$/", "\n", $line); if (preg_match("/character\(([0-9]*)\)/", $line, $regs)) { $num=$regs[1]; - if ($num<=255) { + if ($num<=65000) { $line=preg_replace("/ character\([0-9]*\)/", " varchar($num)", $line); } else { $line=preg_replace("/ character\([0-9]*\)/", " text", $line); From 35f645f69562d7ca441d8d9b6590c77a5041ecb8 Mon Sep 17 00:00:00 2001 From: root Date: Wed, 12 Aug 2020 15:32:09 +0200 Subject: [PATCH 4/4] add the ability to ignore INDEX --- README.md | 12 +++++++++++- pg2mysql.inc.php | 3 ++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 4ad3236..0506345 100644 --- a/README.md +++ b/README.md @@ -31,6 +31,16 @@ export PG2MYSQL_AUTOINCREMENT_KEY_TYPE=KEY php pg2mysql_cli.php ``` +### Ignoring INDEX + +Tables indexes are converted except if the environment variable PG2MYSQL_IGNORE_INDEX is set to a truthy value. + +Usage example: +``` +export PG2MYSQL_IGNORE_INDEX=1 +php pg2mysql_cli.php +``` + ## Web usage To use, simply unzip into your website somewhere, and point your browser at `pg2mysql.php` @@ -47,4 +57,4 @@ $mysql=pg2mysql($postgres); Credit goes to: Author: James Grant Lightbox Technolgoies -http://www.lightbox.org \ No newline at end of file +http://www.lightbox.org diff --git a/pg2mysql.inc.php b/pg2mysql.inc.php index 33e70c1..5e445fc 100644 --- a/pg2mysql.inc.php +++ b/pg2mysql.inc.php @@ -30,6 +30,7 @@ //this is the default, it can be overridden here, or specified as the third parameter on the command line $config['engine']="InnoDB"; $config['autoincrement_key_type'] = getenv("PG2MYSQL_AUTOINCREMENT_KEY_TYPE") ? getenv("PG2MYSQL_AUTOINCREMENT_KEY_TYPE") : "PRIMARY KEY"; +$config['ignore_index'] = getenv("PG2MYSQL_IGNORE_INDEX"); $tables = array(); $tables_extra = array(); @@ -373,7 +374,7 @@ function pg2mysql($input, $header=true) } //while we're here, we might as well catch CREATE INDEX as well - if (substr($line, 0, 12)=="CREATE INDEX") { + if (substr($line, 0, 12)=="CREATE INDEX" && !$config['ignore_index']) { preg_match('/CREATE INDEX "?([a-zA-Z0-9_]*)"? ON "?([a-zA-Z0-9_]*)"? USING btree \((.*)\);/', $line, $matches); if (isset($matches[1]) && isset($matches[2]) && isset($matches[3])) { $indexname=$matches[1];