Skip to content

Commit

Permalink
fix tinytext convert type
Browse files Browse the repository at this point in the history
  • Loading branch information
mihailshumilovsms committed Feb 19, 2014
1 parent 0062cd3 commit c183e00
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,8 @@ mysql2postgresql
================

Converter mysql schema to postgresql

Usage

1. Create schema dump in xml format using command: `mysqldump --xml -d -u USER_NAME DB_NAME > DUMP_FILE_NAME`
2. Run converter using command: `php convertor.php -i DUMP_FILE_NAME -o PSQL_FILE_NAME`
24 changes: 19 additions & 5 deletions convertor.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@
$options = getopt("i:o:", array("input-file:", "output-file:"));

$iFilePath = $options["i"] ? $options["i"] : $options["input-file"];
$oFilePath = $options["o"] ? $options["o"] : $options["output-file"];

$oData = array();

$oXml = simplexml_load_file($iFilePath);

$aDBs = (array)$oXml->{database};
ob_start();
$aDBs = (array)$oXml->{'database'};
foreach ($aDBs['table_structure'] as $key => $val) {
$tableName = (string)$val->attributes()->name;
$oData[$tableName] = array("fields" => array(), "types" => array(), "key" => array(), "primary" => array());
Expand All @@ -39,7 +41,9 @@
$fieldStr .= "real ";
} elseif ((string)$fieldData->attributes()->Type == "datetime") {
$fieldStr .= "timestamp ";
} elseif ((string)$fieldData->attributes()->Type == "mediumtext") {
} elseif (((string)$fieldData->attributes()->Type == "mediumtext") || ((string)$fieldData->attributes(
)->Type == "tinytext")
) {
$fieldStr .= "text ";
} elseif (substr((string)$fieldData->attributes()->Type, 0, 4) == "enum") {
//Create custom type
Expand Down Expand Up @@ -80,7 +84,7 @@
echo "CREATE TYPE " . $customType . ";\n";
}

echo "CREATE TABLE $tableName (\n";
echo "\nCREATE TABLE $tableName (\n";
echo "\t";
echo join(",\n\t", $oData[$tableName]["fields"]);

Expand All @@ -90,6 +94,16 @@
echo ");\n";

foreach ($oData[$tableName]["key"] as $keyName => $keyData) {
echo "CREATE INDEX \"" . $keyName . "\" ON $tableName (" . join(",", $keyData['fields']) . ");\n";
echo "CREATE INDEX \"" . $tableName . "_" . $keyName . "\" ON $tableName (" . join(
",",
$keyData['fields']
) . ");\n";
}
}
}

$dumpData = ob_get_contents();
ob_end_clean();

$fh = fopen($oFilePath, "w+");
fwrite($fh, $dumpData);
fclose($fh);

0 comments on commit c183e00

Please sign in to comment.