Mercurial > hg > fapweb
diff managedb.php @ 540:ab08640b4302
Improve database upgrade code.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Sun, 15 Dec 2013 23:46:19 +0200 |
parents | f872843ae396 |
children | e5a088dd3a4b |
line wrap: on
line diff
--- a/managedb.php Fri Dec 13 18:50:57 2013 +0200 +++ b/managedb.php Sun Dec 15 23:46:19 2013 +0200 @@ -161,7 +161,7 @@ // -// +// Database table definitions // $sqlTables = array( // Database metadata @@ -388,13 +388,25 @@ case "pgsql": foreach ($col as $elem) { - if ($elem != "AUTOINCREMENT") + // For Postgres, use SERIAL for autoincrement + if ($elem == "AUTOINCREMENT") + $tmp[1] = "SERIAL"; + else $tmp[] = $elem; } break; - + + case "mysql": + foreach ($col as $elem) + { + if ($elem != "AUTOINCREMENT") + $tmp[] = "AUTO_INCREMENT"; + else + $tmp[] = $elem; + } + break; + case "sqlite": - case "mysql": $tmp = $col; break; @@ -411,7 +423,7 @@ function stCreateOneTable($dbh, $name, $schema) { - return (stDBExecSQL($dbh, "CREATE TABLE IF NOT EXISTS ".$name." (".$schema.")") !== FALSE) ? TRUE : FALSE; + return (stDBExecSQL($dbh, "CREATE TABLE ".$name." (".$schema.")") !== FALSE) ? TRUE : FALSE; } @@ -422,6 +434,7 @@ foreach ($sqlTables as $name => $schemaData) { echo " - '".$name."'\n"; + stDBExecSQL($dbh, "DROP TABLE IF EXISTS ".$name); if (!stCreateOneTable($dbh, $name, stGetTableSchema($dbh, $schemaData)) && !$upgrade) return FALSE; } @@ -434,24 +447,32 @@ ); -function stUpgradeMap($type, $name, $value = FALSE) +function stUpgradeMap($type, $name) { - global $upgradeMappings; + global $upgradeMappings, $currVersion, $dbVersion; - if (isset($upgradeMappings[$name])) + $converted = FALSE; + $tmpName = $name; + + for ($ver = $currVersion; $ver <= $dbVersion; $ver++) { - if (isset($upgradeMappings[$name][$type])) + if (isset($upgradeMappings[$ver]) && + isset($upgradeMappings[$ver][$tmpName]) && + isset($upgradeMappings[$ver][$tmpName][$type])) { - // XXX TODO .. + $tmpName = $upgradeMappings[$ver][$tmpName][$type]; + $converted = TRUE; } - else - die("Upgrade failed due to missing or invalid upgrade definition: ".$type." ".$name." from version X to Y.\n"); } + + if ($converted) + return $tmpName; else switch ($type) { + case "table": return $name; case "key": return $name; - case "value": return $value; + default: die("Invalid upgrade type '".$type."'.\n"); } } @@ -477,9 +498,10 @@ // Map the key, in case the name has changed $inKey = stUpgradeMap("key", $key); + $inTable = stUpgradeMap("table", $table); // Check if we are upgrading - if ($upgrade && ($res = stDBFetchSQL($inDB, stDBPrepareSQL($inDB, "SELECT * FROM ".$table." WHERE key=%s", $inKey))) !== FALSE) + if ($upgrade && ($res = stDBFetchSQL($inDB, stDBPrepareSQL($inDB, "SELECT * FROM ".$inTable." WHERE key=%s", $inKey))) !== FALSE) { // Yup, upgrade the data, if we can echo "."; @@ -489,7 +511,7 @@ $sql = stDBPrepareSQL($outDB, "INSERT INTO ".$table." (key,vtype,".$var.",sdesc) VALUES (%s,%d,".$type.",%s)", - $key, $data[0], stUpgradeMap("value", $key, $res[$var]), $data[2]); + $key, $data[0], $res[$var], $data[2]); } else { @@ -577,33 +599,37 @@ stDBExecSQL($outDB, "BEGIN TRANSACTION"); foreach ($sqlTables as $name => $schema) - if (!in_array($name, $excluded)) { - echo " - '".$name."' "; + $inTable = stUpgradeMap("table", $name); - foreach (stDBExecSQL($inDB, "SELECT * FROM ".$name) as $row) + if (!in_array($inTable, $excluded) && !in_array($name, $excluded)) { - $avals = array(); - $acols = array(); - - foreach ($schema as $col) + echo " - '".$name."' "; + + foreach (stDBExecSQL($inDB, "SELECT * FROM ".$inTable) as $row) { - if (isset($row[$col[0]])) + $avals = array(); + $acols = array(); + + foreach ($schema as $col) { - $avals[] = stGetSQLTypeParam($outDB, $col[1], $row[$col[0]]); - $acols[] = $col[0]; + if (isset($row[$col[0]])) + { + $avals[] = stGetSQLTypeParam($outDB, $col[1], $row[$col[0]]); + $acols[] = $col[0]; + } } - } - $sql = "INSERT INTO ".$name." (".implode(",", $acols).") VALUES (".implode(",", $avals).")"; - if (stDBExecSQL($outDB, $sql) === false) - { - $status = FALSE; - break; + $sql = "INSERT INTO ".$name." (".implode(",", $acols).") VALUES (".implode(",", $avals).")"; + if (stDBExecSQL($outDB, $sql) === false) + { + $status = FALSE; + break; + } + echo "."; } - echo "."; + echo "\n"; } - echo "\n"; } stDBExecSQL($outDB, "COMMIT");