From a2239d84da86304953742109b913958063d20a2d Mon Sep 17 00:00:00 2001 From: jacob Date: Tue, 8 Jun 2010 20:31:52 +0000 Subject: [PATCH] Modifications to fix db update problems --- db/db.update.173.php | 126 ++++++++++++++++++++++++++++++++++++------- 1 file changed, 107 insertions(+), 19 deletions(-) diff --git a/db/db.update.173.php b/db/db.update.173.php index eecb96d..b69da33 100644 --- a/db/db.update.173.php +++ b/db/db.update.173.php @@ -1,14 +1,12 @@ $fieldSet){ + + // if this table does not have a primary key to reference by, we'll add one + $tempKey = false; + if(count($keys[$tableName]) == 0){ + echo "table `$tableName` has no primary key. We'll create one for this update and drop it afterwards.\n"; + mysql_query("ALTER TABLE `$tableName` ADD `__TEMP__id__` INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY"); + $keys[$tableName][] = '__TEMP__id__'; + $tempKey = true; + } + // build the query that gives us the field values we need to update in this table, as well as the primary keys $query = "SELECT `" . implode('`, `', $fieldSet) . '`'; for($n = 0; $n < count($keys[$tableName]); $n++){ @@ -75,8 +85,28 @@ function db_update_173_post(){ while($record = mysql_fetch_array($data)){ $updates[] = $record; } +// mysql_query("SET NAMES utf8"); + echo "Changing default character set on table \"$tableName\": "; + // now we have the data - let's convert the table + + if(_DO_UPDATE){ + $query = "ALTER TABLE `$tableName` DEFAULT CHARACTER SET 'utf8'"; + $success = mysql_query($query); + if($success){ + echo " success\n"; + }else{ + echo " Failed using query: $query\n"; + $totalFailCount ++; + } + }else{ + //echo "ALTER TABLE `$tableName` DEFAULT CHARACTER SET 'utf8';\n"; + echo " success\n"; + } + + echo "\n" . 'Modifying fields: "' . implode('", "', $fieldSet) . '" in table: "' . $tableName . '"'; // now re-insert those values into the table + $failCount = 0; foreach($updates as $update){ $query = "UPDATE $tableName SET"; $useComma = false; @@ -88,25 +118,83 @@ function db_update_173_post(){ $useComma = true; } $newValue = mb_convert_encoding($fieldValue, "UTF-8", "iso-8859-1"); - $query .= sprintf(" `%s` = '%s'", $fieldName, mysql_real_escape_string($newValue)); + $query .= " `" . $fieldName . "` = '" . mysql_real_escape_string($newValue) . "'"; + //$query .= sprintf(" `%s` = '%s'", $fieldName, mysql_real_escape_string($newValue)); } $query .= " WHERE "; for($n = 0; $n < count($keys[$tableName]); $n++){ if($n > 0) $query .= " AND "; $query .= "`" . $keys[$tableName][$n] . "` = '" . mysql_real_escape_string($update["__KEYFIELD_" . ($n + 1) . "__"]) . "'"; } - $success = mysql_query($query); - if($success) echo '.'; - else{ - echo "\nFailed to execute query: $query\n"; - $errorTally ++; + if(_DO_UPDATE){ + $success = mysql_query($query); + if($success){ + echo '.'; + }else{ + echo "\nFailed to execute query: $query\n"; + $failCount++; + } + }else{ + echo "."; + } + + } + echo "\n"; + if($failCount > 0){ + echo "Updating records in table $tableName failed on $failcount records.\n"; + } + $totalFailCount += $failCount; + unset($updates); + + // if we've created a temporary key for this table we'll remove it now + if($tempKey == true){ + echo "Removing the temporary key that we used for this table\n"; + $query = "ALTER TABLE `$tableName` DROP `__TEMP__id__`"; + mysql_query($query); + } + + // we also need to change the character encoding for the individual fields + echo "Changing character encoding for individual fields in table \"$tableName\""; + foreach($fieldTypes[$tableName] as $fieldName => $fieldType){ + $query = "ALTER TABLE `$tableName` CHANGE `$fieldName` `$fieldName` "; + $query .= $fieldType; + $query .= " CHARACTER SET utf8 COLLATE utf8_general_ci"; + if(_DO_UPDATE){ + $success = mysql_query($query); + if($success){ + echo "."; + }else{ + echo "\nFailed using query: $query\n"; + $totalFailCount ++; + } + }else{ + echo "."; } } - unset($updates); - } - echo "\nComplete with $errorTally failed queries.\n"; - // now drop the id column that we added to the committees_link table - $query = "ALTER TABLE `committees_link` DROP `id`"; - mysql_query($query); + } + + // with all of the affected tables updated, let's go ahead and update the unaffected ones + foreach($allTables as $tableName){ + if(!array_key_exists($tableName, $fields)){ + echo "Changing default character set on table \"$tableName\": "; + if(_DO_UPDATE){ + $query = "ALTER TABLE `$tableName` DEFAULT CHARACTER SET 'utf8'"; + $success = mysql_query($query); + if($success){ + echo " success\n"; + }else{ + echo " Failed using query: $query\n"; + $totalFailCount++; + } + }else{ + echo " success\n"; + } + } + } + + echo "\nFinished updating values with $totalFailCount failed queries.\n"; +} + +function db_update_173_pre(){ }