| 1 | 1406,1412c1406,1446 |
|---|
| 2 | < // Check to see which tables and fields exist |
|---|
| 3 | < if ($tables = $wpdb->get_col('SHOW TABLES;')) { |
|---|
| 4 | < // For every table in the database |
|---|
| 5 | < foreach ($tables as $table) { |
|---|
| 6 | < // Upgrade global tables only for the main site. Don't upgrade at all if DO_NOT_UPGRADE_GLOBAL_TABLES is defined. |
|---|
| 7 | < if ( in_array($table, $wpdb->tables('global')) && ( !is_main_site() || defined('DO_NOT_UPGRADE_GLOBAL_TABLES') ) ) |
|---|
| 8 | < continue; |
|---|
| 9 | --- |
|---|
| 10 | > foreach ($cqueries as $table => $qry) { |
|---|
| 11 | > // Upgrade global tables only for the main site. Don't upgrade at all if DO_NOT_UPGRADE_GLOBAL_TABLES is defined. |
|---|
| 12 | > if ( in_array($table, $wpdb->tables('global')) && ( !is_main_site() || defined('DO_NOT_UPGRADE_GLOBAL_TABLES') ) ) |
|---|
| 13 | > continue; |
|---|
| 14 | > |
|---|
| 15 | > // Fetch the table column structure from the database |
|---|
| 16 | > $tablefields = $wpdb->get_results("DESCRIBE {$table};"); |
|---|
| 17 | > if (!$tablefields) |
|---|
| 18 | > continue; |
|---|
| 19 | > |
|---|
| 20 | > // Clear the field and index arrays |
|---|
| 21 | > $cfields = $indices = array(); |
|---|
| 22 | > // Get all of the field names in the query from between the parens |
|---|
| 23 | > preg_match("|\((.*)\)|ms", $cqueries[strtolower($table)], $match2); |
|---|
| 24 | > $qryline = trim($match2[1]); |
|---|
| 25 | > |
|---|
| 26 | > // Separate field lines into an array |
|---|
| 27 | > $flds = explode("\n", $qryline); |
|---|
| 28 | > |
|---|
| 29 | > //echo "<hr/><pre>\n".print_r(strtolower($table), true).":\n".print_r($cqueries, true)."</pre><hr/>"; |
|---|
| 30 | > |
|---|
| 31 | > // For every field line specified in the query |
|---|
| 32 | > foreach ($flds as $fld) { |
|---|
| 33 | > // Extract the field name |
|---|
| 34 | > preg_match("|^([^ ]*)|", trim($fld), $fvals); |
|---|
| 35 | > $fieldname = trim( $fvals[1], '`' ); |
|---|
| 36 | > |
|---|
| 37 | > // Verify the found field name |
|---|
| 38 | > $validfield = true; |
|---|
| 39 | > switch (strtolower($fieldname)) { |
|---|
| 40 | > case '': |
|---|
| 41 | > case 'primary': |
|---|
| 42 | > case 'index': |
|---|
| 43 | > case 'fulltext': |
|---|
| 44 | > case 'unique': |
|---|
| 45 | > case 'key': |
|---|
| 46 | > $validfield = false; |
|---|
| 47 | > $indices[] = trim(trim($fld), ", \n"); |
|---|
| 48 | > break; |
|---|
| 49 | > } |
|---|
| 50 | > $fld = trim($fld); |
|---|
| 51 | 1414,1446c1448,1452 |
|---|
| 52 | < // If a table query exists for the database table... |
|---|
| 53 | < if ( array_key_exists(strtolower($table), $cqueries) ) { |
|---|
| 54 | < // Clear the field and index arrays |
|---|
| 55 | < $cfields = $indices = array(); |
|---|
| 56 | < // Get all of the field names in the query from between the parens |
|---|
| 57 | < preg_match("|\((.*)\)|ms", $cqueries[strtolower($table)], $match2); |
|---|
| 58 | < $qryline = trim($match2[1]); |
|---|
| 59 | < |
|---|
| 60 | < // Separate field lines into an array |
|---|
| 61 | < $flds = explode("\n", $qryline); |
|---|
| 62 | < |
|---|
| 63 | < //echo "<hr/><pre>\n".print_r(strtolower($table), true).":\n".print_r($cqueries, true)."</pre><hr/>"; |
|---|
| 64 | < |
|---|
| 65 | < // For every field line specified in the query |
|---|
| 66 | < foreach ($flds as $fld) { |
|---|
| 67 | < // Extract the field name |
|---|
| 68 | < preg_match("|^([^ ]*)|", trim($fld), $fvals); |
|---|
| 69 | < $fieldname = trim( $fvals[1], '`' ); |
|---|
| 70 | < |
|---|
| 71 | < // Verify the found field name |
|---|
| 72 | < $validfield = true; |
|---|
| 73 | < switch (strtolower($fieldname)) { |
|---|
| 74 | < case '': |
|---|
| 75 | < case 'primary': |
|---|
| 76 | < case 'index': |
|---|
| 77 | < case 'fulltext': |
|---|
| 78 | < case 'unique': |
|---|
| 79 | < case 'key': |
|---|
| 80 | < $validfield = false; |
|---|
| 81 | < $indices[] = trim(trim($fld), ", \n"); |
|---|
| 82 | < break; |
|---|
| 83 | < } |
|---|
| 84 | < $fld = trim($fld); |
|---|
| 85 | --- |
|---|
| 86 | > // If it's a valid field, add it to the field array |
|---|
| 87 | > if ($validfield) { |
|---|
| 88 | > $cfields[strtolower($fieldname)] = trim($fld, ", \n"); |
|---|
| 89 | > } |
|---|
| 90 | > } |
|---|
| 91 | 1448,1451c1454,1466 |
|---|
| 92 | < // If it's a valid field, add it to the field array |
|---|
| 93 | < if ($validfield) { |
|---|
| 94 | < $cfields[strtolower($fieldname)] = trim($fld, ", \n"); |
|---|
| 95 | < } |
|---|
| 96 | --- |
|---|
| 97 | > // For every field in the table |
|---|
| 98 | > foreach ($tablefields as $tablefield) { |
|---|
| 99 | > // If the table field exists in the field array... |
|---|
| 100 | > if (array_key_exists(strtolower($tablefield->Field), $cfields)) { |
|---|
| 101 | > // Get the field type from the query |
|---|
| 102 | > preg_match("|".$tablefield->Field." ([^ ]*( unsigned)?)|i", $cfields[strtolower($tablefield->Field)], $matches); |
|---|
| 103 | > $fieldtype = $matches[1]; |
|---|
| 104 | > |
|---|
| 105 | > // Is actual field type different from the field type in query? |
|---|
| 106 | > if ($tablefield->Type != $fieldtype) { |
|---|
| 107 | > // Add a query to change the column type |
|---|
| 108 | > $cqueries[] = "ALTER TABLE {$table} CHANGE COLUMN {$tablefield->Field} " . $cfields[strtolower($tablefield->Field)]; |
|---|
| 109 | > $for_update[$table.'.'.$tablefield->Field] = "Changed type of {$table}.{$tablefield->Field} from {$tablefield->Type} to {$fieldtype}"; |
|---|
| 110 | 1454,1486c1469,1476 |
|---|
| 111 | < // Fetch the table column structure from the database |
|---|
| 112 | < $tablefields = $wpdb->get_results("DESCRIBE {$table};"); |
|---|
| 113 | < |
|---|
| 114 | < // For every field in the table |
|---|
| 115 | < foreach ($tablefields as $tablefield) { |
|---|
| 116 | < // If the table field exists in the field array... |
|---|
| 117 | < if (array_key_exists(strtolower($tablefield->Field), $cfields)) { |
|---|
| 118 | < // Get the field type from the query |
|---|
| 119 | < preg_match("|".$tablefield->Field." ([^ ]*( unsigned)?)|i", $cfields[strtolower($tablefield->Field)], $matches); |
|---|
| 120 | < $fieldtype = $matches[1]; |
|---|
| 121 | < |
|---|
| 122 | < // Is actual field type different from the field type in query? |
|---|
| 123 | < if ($tablefield->Type != $fieldtype) { |
|---|
| 124 | < // Add a query to change the column type |
|---|
| 125 | < $cqueries[] = "ALTER TABLE {$table} CHANGE COLUMN {$tablefield->Field} " . $cfields[strtolower($tablefield->Field)]; |
|---|
| 126 | < $for_update[$table.'.'.$tablefield->Field] = "Changed type of {$table}.{$tablefield->Field} from {$tablefield->Type} to {$fieldtype}"; |
|---|
| 127 | < } |
|---|
| 128 | < |
|---|
| 129 | < // Get the default value from the array |
|---|
| 130 | < //echo "{$cfields[strtolower($tablefield->Field)]}<br>"; |
|---|
| 131 | < if (preg_match("| DEFAULT '(.*)'|i", $cfields[strtolower($tablefield->Field)], $matches)) { |
|---|
| 132 | < $default_value = $matches[1]; |
|---|
| 133 | < if ($tablefield->Default != $default_value) { |
|---|
| 134 | < // Add a query to change the column's default value |
|---|
| 135 | < $cqueries[] = "ALTER TABLE {$table} ALTER COLUMN {$tablefield->Field} SET DEFAULT '{$default_value}'"; |
|---|
| 136 | < $for_update[$table.'.'.$tablefield->Field] = "Changed default value of {$table}.{$tablefield->Field} from {$tablefield->Default} to {$default_value}"; |
|---|
| 137 | < } |
|---|
| 138 | < } |
|---|
| 139 | < |
|---|
| 140 | < // Remove the field from the array (so it's not added) |
|---|
| 141 | < unset($cfields[strtolower($tablefield->Field)]); |
|---|
| 142 | < } else { |
|---|
| 143 | < // This field exists in the table, but not in the creation queries? |
|---|
| 144 | --- |
|---|
| 145 | > // Get the default value from the array |
|---|
| 146 | > //echo "{$cfields[strtolower($tablefield->Field)]}<br>"; |
|---|
| 147 | > if (preg_match("| DEFAULT '(.*)'|i", $cfields[strtolower($tablefield->Field)], $matches)) { |
|---|
| 148 | > $default_value = $matches[1]; |
|---|
| 149 | > if ($tablefield->Default != $default_value) { |
|---|
| 150 | > // Add a query to change the column's default value |
|---|
| 151 | > $cqueries[] = "ALTER TABLE {$table} ALTER COLUMN {$tablefield->Field} SET DEFAULT '{$default_value}'"; |
|---|
| 152 | > $for_update[$table.'.'.$tablefield->Field] = "Changed default value of {$table}.{$tablefield->Field} from {$tablefield->Default} to {$default_value}"; |
|---|
| 153 | 1490,1495c1480,1485 |
|---|
| 154 | < // For every remaining field specified for the table |
|---|
| 155 | < foreach ($cfields as $fieldname => $fielddef) { |
|---|
| 156 | < // Push a query line into $cqueries that adds the field to that table |
|---|
| 157 | < $cqueries[] = "ALTER TABLE {$table} ADD COLUMN $fielddef"; |
|---|
| 158 | < $for_update[$table.'.'.$fieldname] = 'Added column '.$table.'.'.$fieldname; |
|---|
| 159 | < } |
|---|
| 160 | --- |
|---|
| 161 | > // Remove the field from the array (so it's not added) |
|---|
| 162 | > unset($cfields[strtolower($tablefield->Field)]); |
|---|
| 163 | > } else { |
|---|
| 164 | > // This field exists in the table, but not in the creation queries? |
|---|
| 165 | > } |
|---|
| 166 | > } |
|---|
| 167 | 1497,1511c1487,1508 |
|---|
| 168 | < // Index stuff goes here |
|---|
| 169 | < // Fetch the table index structure from the database |
|---|
| 170 | < $tableindices = $wpdb->get_results("SHOW INDEX FROM {$table};"); |
|---|
| 171 | < |
|---|
| 172 | < if ($tableindices) { |
|---|
| 173 | < // Clear the index array |
|---|
| 174 | < unset($index_ary); |
|---|
| 175 | < |
|---|
| 176 | < // For every index in the table |
|---|
| 177 | < foreach ($tableindices as $tableindex) { |
|---|
| 178 | < // Add the index to the index data array |
|---|
| 179 | < $keyname = $tableindex->Key_name; |
|---|
| 180 | < $index_ary[$keyname]['columns'][] = array('fieldname' => $tableindex->Column_name, 'subpart' => $tableindex->Sub_part); |
|---|
| 181 | < $index_ary[$keyname]['unique'] = ($tableindex->Non_unique == 0)?true:false; |
|---|
| 182 | < } |
|---|
| 183 | --- |
|---|
| 184 | > // For every remaining field specified for the table |
|---|
| 185 | > foreach ($cfields as $fieldname => $fielddef) { |
|---|
| 186 | > // Push a query line into $cqueries that adds the field to that table |
|---|
| 187 | > $cqueries[] = "ALTER TABLE {$table} ADD COLUMN $fielddef"; |
|---|
| 188 | > $for_update[$table.'.'.$fieldname] = 'Added column '.$table.'.'.$fieldname; |
|---|
| 189 | > } |
|---|
| 190 | > |
|---|
| 191 | > // Index stuff goes here |
|---|
| 192 | > // Fetch the table index structure from the database |
|---|
| 193 | > $tableindices = $wpdb->get_results("SHOW INDEX FROM {$table};"); |
|---|
| 194 | > |
|---|
| 195 | > if ($tableindices) { |
|---|
| 196 | > // Clear the index array |
|---|
| 197 | > unset($index_ary); |
|---|
| 198 | > |
|---|
| 199 | > // For every index in the table |
|---|
| 200 | > foreach ($tableindices as $tableindex) { |
|---|
| 201 | > // Add the index to the index data array |
|---|
| 202 | > $keyname = $tableindex->Key_name; |
|---|
| 203 | > $index_ary[$keyname]['columns'][] = array('fieldname' => $tableindex->Column_name, 'subpart' => $tableindex->Sub_part); |
|---|
| 204 | > $index_ary[$keyname]['unique'] = ($tableindex->Non_unique == 0)?true:false; |
|---|
| 205 | > } |
|---|
| 206 | 1513,1542c1510,1530 |
|---|
| 207 | < // For each actual index in the index array |
|---|
| 208 | < foreach ($index_ary as $index_name => $index_data) { |
|---|
| 209 | < // Build a create string to compare to the query |
|---|
| 210 | < $index_string = ''; |
|---|
| 211 | < if ($index_name == 'PRIMARY') { |
|---|
| 212 | < $index_string .= 'PRIMARY '; |
|---|
| 213 | < } else if($index_data['unique']) { |
|---|
| 214 | < $index_string .= 'UNIQUE '; |
|---|
| 215 | < } |
|---|
| 216 | < $index_string .= 'KEY '; |
|---|
| 217 | < if ($index_name != 'PRIMARY') { |
|---|
| 218 | < $index_string .= $index_name; |
|---|
| 219 | < } |
|---|
| 220 | < $index_columns = ''; |
|---|
| 221 | < // For each column in the index |
|---|
| 222 | < foreach ($index_data['columns'] as $column_data) { |
|---|
| 223 | < if ($index_columns != '') $index_columns .= ','; |
|---|
| 224 | < // Add the field to the column list string |
|---|
| 225 | < $index_columns .= $column_data['fieldname']; |
|---|
| 226 | < if ($column_data['subpart'] != '') { |
|---|
| 227 | < $index_columns .= '('.$column_data['subpart'].')'; |
|---|
| 228 | < } |
|---|
| 229 | < } |
|---|
| 230 | < // Add the column list to the index create string |
|---|
| 231 | < $index_string .= ' ('.$index_columns.')'; |
|---|
| 232 | < if (!(($aindex = array_search($index_string, $indices)) === false)) { |
|---|
| 233 | < unset($indices[$aindex]); |
|---|
| 234 | < //echo "<pre style=\"border:1px solid #ccc;margin-top:5px;\">{$table}:<br />Found index:".$index_string."</pre>\n"; |
|---|
| 235 | < } |
|---|
| 236 | < //else echo "<pre style=\"border:1px solid #ccc;margin-top:5px;\">{$table}:<br /><b>Did not find index:</b>".$index_string."<br />".print_r($indices, true)."</pre>\n"; |
|---|
| 237 | --- |
|---|
| 238 | > // For each actual index in the index array |
|---|
| 239 | > foreach ($index_ary as $index_name => $index_data) { |
|---|
| 240 | > // Build a create string to compare to the query |
|---|
| 241 | > $index_string = ''; |
|---|
| 242 | > if ($index_name == 'PRIMARY') { |
|---|
| 243 | > $index_string .= 'PRIMARY '; |
|---|
| 244 | > } else if($index_data['unique']) { |
|---|
| 245 | > $index_string .= 'UNIQUE '; |
|---|
| 246 | > } |
|---|
| 247 | > $index_string .= 'KEY '; |
|---|
| 248 | > if ($index_name != 'PRIMARY') { |
|---|
| 249 | > $index_string .= $index_name; |
|---|
| 250 | > } |
|---|
| 251 | > $index_columns = ''; |
|---|
| 252 | > // For each column in the index |
|---|
| 253 | > foreach ($index_data['columns'] as $column_data) { |
|---|
| 254 | > if ($index_columns != '') $index_columns .= ','; |
|---|
| 255 | > // Add the field to the column list string |
|---|
| 256 | > $index_columns .= $column_data['fieldname']; |
|---|
| 257 | > if ($column_data['subpart'] != '') { |
|---|
| 258 | > $index_columns .= '('.$column_data['subpart'].')'; |
|---|
| 259 | 1545,1550c1533,1537 |
|---|
| 260 | < |
|---|
| 261 | < // For every remaining index specified for the table |
|---|
| 262 | < foreach ( (array) $indices as $index ) { |
|---|
| 263 | < // Push a query line into $cqueries that adds the index to that table |
|---|
| 264 | < $cqueries[] = "ALTER TABLE {$table} ADD $index"; |
|---|
| 265 | < $for_update[$table.'.'.$fieldname] = 'Added index '.$table.' '.$index; |
|---|
| 266 | --- |
|---|
| 267 | > // Add the column list to the index create string |
|---|
| 268 | > $index_string .= ' ('.$index_columns.')'; |
|---|
| 269 | > if (!(($aindex = array_search($index_string, $indices)) === false)) { |
|---|
| 270 | > unset($indices[$aindex]); |
|---|
| 271 | > //echo "<pre style=\"border:1px solid #ccc;margin-top:5px;\">{$table}:<br />Found index:".$index_string."</pre>\n"; |
|---|
| 272 | 1552,1557c1539 |
|---|
| 273 | < |
|---|
| 274 | < // Remove the original table creation query from processing |
|---|
| 275 | < unset($cqueries[strtolower($table)]); |
|---|
| 276 | < unset($for_update[strtolower($table)]); |
|---|
| 277 | < } else { |
|---|
| 278 | < // This table exists in the database, but not in the creation queries? |
|---|
| 279 | --- |
|---|
| 280 | > //else echo "<pre style=\"border:1px solid #ccc;margin-top:5px;\">{$table}:<br /><b>Did not find index:</b>".$index_string."<br />".print_r($indices, true)."</pre>\n"; |
|---|
| 281 | 1559a1542,1552 |
|---|
| 282 | > |
|---|
| 283 | > // For every remaining index specified for the table |
|---|
| 284 | > foreach ( (array) $indices as $index ) { |
|---|
| 285 | > // Push a query line into $cqueries that adds the index to that table |
|---|
| 286 | > $cqueries[] = "ALTER TABLE {$table} ADD $index"; |
|---|
| 287 | > $for_update[$table.'.'.$fieldname] = 'Added index '.$table.' '.$index; |
|---|
| 288 | > } |
|---|
| 289 | > |
|---|
| 290 | > // Remove the original table creation query from processing |
|---|
| 291 | > unset($cqueries[strtolower($table)]); |
|---|
| 292 | > unset($for_update[strtolower($table)]); |
|---|