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 752391f..5e445fc 100644 --- a/pg2mysql.inc.php +++ b/pg2mysql.inc.php @@ -30,6 +30,10 @@ //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(); // Timezone to use date_default_timezone_set('UTC'); @@ -139,6 +143,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 +161,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; } @@ -195,7 +221,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 { @@ -223,7 +249,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); @@ -246,16 +272,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"; + $table_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,17 +288,16 @@ 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"; + //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") { @@ -351,13 +374,13 @@ 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]; - $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"; } }