forked from science-ation/science-ation
Modifications to fix db update problems
This commit is contained in:
parent
ee2e03df4b
commit
a2239d84da
@ -1,14 +1,12 @@
|
||||
<?php
|
||||
function db_update_173_pre(){
|
||||
}
|
||||
|
||||
/****
|
||||
This script takes all Latin1 character encoding in the database and converts it to UTF-8
|
||||
****/
|
||||
define("_DO_UPDATE", true);
|
||||
function db_update_173_post(){
|
||||
mysql_query("SET NAMES latin1");
|
||||
|
||||
// patch the committees_link table
|
||||
mysql_query("ALTER TABLE `committees_link` ADD `id` INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY");
|
||||
|
||||
// loop through every table in the database
|
||||
$data = mysql_query("SHOW TABLES");
|
||||
@ -18,6 +16,7 @@ function db_update_173_post(){
|
||||
while($tableInfo = mysql_fetch_array($data)){
|
||||
$tableName = $tableInfo[0];
|
||||
$allTables[] = $tableName;
|
||||
$fieldTypes[$tableName] = array();
|
||||
|
||||
// loop through every field in the table
|
||||
$query = mysql_query("DESCRIBE " . $tableName);
|
||||
@ -25,12 +24,13 @@ function db_update_173_post(){
|
||||
while($rowInfo = mysql_fetch_array($query)){
|
||||
|
||||
// find out if this field is a varchar, char, text, or tinytext field
|
||||
if(preg_match('/.*char.*|.*text.*/', $rowInfo['Type'])){
|
||||
if(preg_match('/.*char.*|.*text.*|.*enum.*/i', $rowInfo['Type'])){
|
||||
// it does, so this field needs to be converted
|
||||
if(!array_key_exists($tableName, $fields)){
|
||||
$fields[$tableName] = array();
|
||||
}
|
||||
$fields[$tableName][] = $rowInfo['Field'];
|
||||
$fieldTypes[$tableName][$rowInfo['Field']] = $rowInfo['Type'];
|
||||
}
|
||||
if($rowInfo['Key'] == 'PRI'){
|
||||
$keys[$tableName][] = $rowInfo['Field'];
|
||||
@ -38,6 +38,7 @@ function db_update_173_post(){
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* The array "fields" now contains the names of all fields in the database that
|
||||
need to be converted (and only those that need to be converted), stored in this format:
|
||||
|
||||
@ -59,9 +60,18 @@ function db_update_173_post(){
|
||||
|
||||
Now we need to run through those tables one at a time and convert them
|
||||
*/
|
||||
$errorTally = 0;
|
||||
echo "Updating records:\n";
|
||||
$totalFailCount = 0;
|
||||
foreach($fields as $tableName => $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(){
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user