r1569: Added improvements to convert_database_charset.php
authorRobert Sesek <rsesek@bluestatic.org>
Sun, 8 Jul 2007 00:01:23 +0000 (00:01 +0000)
committerRobert Sesek <rsesek@bluestatic.org>
Sun, 8 Jul 2007 00:01:23 +0000 (00:01 +0000)
install/convert_database_charset.php

index 8b5e4484fbf0a8a32b39a22c4d60cbe5fd2819ef..272c57c1e0e9084a14ad43c2184c5726b6245d9b 100644 (file)
@@ -31,6 +31,15 @@ define('TARGET', 'utf8_general_ci');
 
 $bugsys->input_clean('step', TYPE_UINT);
 
+// columns to convert per table
+$columnConversions = array();
+
+// primary keys that are text
+$primaryKeys = array();
+
+// fulltext
+$fulltext = array();
+
 // ###################################################################
 
 function PrintContinue($step)
@@ -63,19 +72,49 @@ if ($bugsys->in['step'] == 0)
 
 else if ($bugsys->in['step'] == 1)
 {
+       echo '<h1>Preserve Existing Data</h1>';
+       echo '<p>This converts all the table columns that are stored with a character set to a BLOB type to ensure there are no problems with the target character set.</p>';
+       
+       $admin->table_start();
+       $admin->table_head('Column Preservation');
+       $admin->table_column_head(array('Table', 'Column'));
+       
+       // get rid of indices that are strings
+       $db->showerrors = false;
+       $db->query("ALTER TABLE " . TABLE_PREFIX . "bug DROP INDEX summary");
+       $db->query("ALTER TABLE " . TABLE_PREFIX . "comment DROP INDEX comment");
+       $db->query("ALTER TABLE " . TABLE_PREFIX . "language DROP INDEX languagecode");
+       $db->query("ALTER TABLE " . TABLE_PREFIX . "language DROP INDEX langcode");
+       $db->showerrors = true;
+       
+       $tables = $db->query("SHOW TABLES");
+       while ($table = $db->fetch_array($tables, false))
+       {
+               $columns = $db->query("SHOW FULL COLUMNS FROM $table[0]");
+               while ($col = $db->fetch_array($columns))
+               {
+                       if (!is_null($col['Collation']) AND (strpos($col['Type'], 'char') !== false OR strpos($col['Type'], 'text') !== false))
+                       {
+                               $columnConversions[$table[0]][$col['Field']] = $col['Type'];
+                               if ($col['Key'] == 'PRI')
+                               {
+                                       $db->query("ALTER TABLE $table[0] DROP PRIMARY KEY");
+                                       $primaryKeys[$table[0]] = $col['Field'];
+                               }
+                               $db->query("ALTER TABLE $table[0] MODIFY $col[Field] BLOB");
+                               $admin->row_text($table[0], $col['Field']);
+                       }
+               }
+       }
+       
+       $admin->table_end();
+
        echo '<h1>Convert Database Character Set</h1>';
        echo '<p>This step changes the database\'s character set.</p>';
        
        require './includes/config.php';
        $db->query("ALTER DATABASE $database COLLATE " . TARGET);
-       
-       PrintContinue(2);
-}
-
-// ###################################################################
 
-else if ($bugsys->in['step'] == 2)
-{
        echo '<h1>Convert Table Character Set</h1>';
        echo '<p>This converts each table\'s character set and collation.</p>';
        
@@ -91,14 +130,7 @@ else if ($bugsys->in['step'] == 2)
        }
        
        $admin->table_end();
-       
-       PrintContinue(3);
-}
-
-// ###################################################################
 
-else if ($bugsys->in['step'] == 3)
-{
        echo '<h1>Convert Table Columns</h1>';
        echo '<p>This converts all the table columns that are stored with a character set.</p>';
        
@@ -106,28 +138,32 @@ else if ($bugsys->in['step'] == 3)
        $admin->table_head('Column Conversion');
        $admin->table_column_head(array('Table', 'Column'));
        
-       $tables = $db->query("SHOW TABLES");
-       while ($table = $db->fetch_array($tables, false))
+       foreach ($columnConversions AS $table => $columns)
        {
-               $columns = $db->query("SHOW FULL COLUMNS FROM $table[0]");
-               while ($col = $db->fetch_array($columns))
+               foreach ($columns AS $column => $type)
                {
-                       if (!is_null($col['Collation']) AND (strpos($col['Type'], 'char') !== false OR strpos($col['Type'], 'text') !== false))
-                       {
-                               $db->query("ALTER TABLE $table[0] MODIFY $col[Field] $col[Type] COLLATE " . TARGET);
-                               $admin->row_text($table[0], $col['Field']);
-                       }
+                       $db->query("ALTER TABLE $table MODIFY $column $type COLLATE " . TARGET);
+                       $admin->row_text($table, $column);
                }
        }
        
+       foreach ($primaryKeys AS $table => $field)
+       {
+               $db->query("ALTER TABLE $table ADD PRIMARY KEY ($field)");
+       }
+       
+       $db->query("ALTER TABLE " . TABLE_PREFIX . "bug ADD FULLTEXT(summary)");
+       $db->query("ALTER TABLE " . TABLE_PREFIX . "comment ADD FULLTEXT(comment)");
+       $db->query("ALTER TABLE " . TABLE_PREFIX . "language ADD UNIQUE(langcode)");
+       
        $admin->table_end();
        
-       PrintContinue(4);
+       PrintContinue(2);
 }
 
 // ###################################################################
 
-else if ($bugsys->in['step'] == 4)
+else if ($bugsys->in['step'] == 2)
 {
        echo '<h1>Conversion Complete</h1>';
        echo '<p>Your database has now been converted to the <strong>' . TARGET . '</strong> collation. Congratulations. Please check that everything is operating correctly.</p>';