Changeset 19041
- Timestamp:
- 10/21/2011 10:40:30 PM (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/wp-admin/includes/upgrade.php
r19040 r19041 1474 1474 continue; 1475 1475 1476 // Clear the field and index arrays 1477 $cfields = $indices = array(); 1478 // Get all of the field names in the query from between the parens 1479 preg_match("|\((.*)\)|ms", $qry, $match2); 1480 $qryline = trim($match2[1]); 1481 1482 // Separate field lines into an array 1483 $flds = explode("\n", $qryline); 1484 1485 //echo "<hr/><pre>\n".print_r(strtolower($table), true).":\n".print_r($cqueries, true)."</pre><hr/>"; 1486 1487 // For every field line specified in the query 1488 foreach ($flds as $fld) { 1489 // Extract the field name 1490 preg_match("|^([^ ]*)|", trim($fld), $fvals); 1491 $fieldname = trim( $fvals[1], '`' ); 1492 1493 // Verify the found field name 1494 $validfield = true; 1495 switch (strtolower($fieldname)) { 1496 case '': 1497 case 'primary': 1498 case 'index': 1499 case 'fulltext': 1500 case 'unique': 1501 case 'key': 1502 $validfield = false; 1503 $indices[] = trim(trim($fld), ", \n"); 1504 break; 1505 } 1506 $fld = trim($fld); 1507 1508 // If it's a valid field, add it to the field array 1509 if ($validfield) { 1510 $cfields[strtolower($fieldname)] = trim($fld, ", \n"); 1476 // Clear the field and index arrays 1477 $cfields = $indices = array(); 1478 // Get all of the field names in the query from between the parens 1479 preg_match("|\((.*)\)|ms", $qry, $match2); 1480 $qryline = trim($match2[1]); 1481 1482 // Separate field lines into an array 1483 $flds = explode("\n", $qryline); 1484 1485 //echo "<hr/><pre>\n".print_r(strtolower($table), true).":\n".print_r($cqueries, true)."</pre><hr/>"; 1486 1487 // For every field line specified in the query 1488 foreach ($flds as $fld) { 1489 // Extract the field name 1490 preg_match("|^([^ ]*)|", trim($fld), $fvals); 1491 $fieldname = trim( $fvals[1], '`' ); 1492 1493 // Verify the found field name 1494 $validfield = true; 1495 switch (strtolower($fieldname)) { 1496 case '': 1497 case 'primary': 1498 case 'index': 1499 case 'fulltext': 1500 case 'unique': 1501 case 'key': 1502 $validfield = false; 1503 $indices[] = trim(trim($fld), ", \n"); 1504 break; 1505 } 1506 $fld = trim($fld); 1507 1508 // If it's a valid field, add it to the field array 1509 if ($validfield) { 1510 $cfields[strtolower($fieldname)] = trim($fld, ", \n"); 1511 } 1512 } 1513 1514 // For every field in the table 1515 foreach ($tablefields as $tablefield) { 1516 // If the table field exists in the field array... 1517 if (array_key_exists(strtolower($tablefield->Field), $cfields)) { 1518 // Get the field type from the query 1519 preg_match("|".$tablefield->Field." ([^ ]*( unsigned)?)|i", $cfields[strtolower($tablefield->Field)], $matches); 1520 $fieldtype = $matches[1]; 1521 1522 // Is actual field type different from the field type in query? 1523 if ($tablefield->Type != $fieldtype) { 1524 // Add a query to change the column type 1525 $cqueries[] = "ALTER TABLE {$table} CHANGE COLUMN {$tablefield->Field} " . $cfields[strtolower($tablefield->Field)]; 1526 $for_update[$table.'.'.$tablefield->Field] = "Changed type of {$table}.{$tablefield->Field} from {$tablefield->Type} to {$fieldtype}"; 1527 } 1528 1529 // Get the default value from the array 1530 //echo "{$cfields[strtolower($tablefield->Field)]}<br>"; 1531 if (preg_match("| DEFAULT '(.*)'|i", $cfields[strtolower($tablefield->Field)], $matches)) { 1532 $default_value = $matches[1]; 1533 if ($tablefield->Default != $default_value) { 1534 // Add a query to change the column's default value 1535 $cqueries[] = "ALTER TABLE {$table} ALTER COLUMN {$tablefield->Field} SET DEFAULT '{$default_value}'"; 1536 $for_update[$table.'.'.$tablefield->Field] = "Changed default value of {$table}.{$tablefield->Field} from {$tablefield->Default} to {$default_value}"; 1511 1537 } 1512 1538 } 1513 1539 1514 // For every field in the table 1515 foreach ($tablefields as $tablefield) { 1516 // If the table field exists in the field array... 1517 if (array_key_exists(strtolower($tablefield->Field), $cfields)) { 1518 // Get the field type from the query 1519 preg_match("|".$tablefield->Field." ([^ ]*( unsigned)?)|i", $cfields[strtolower($tablefield->Field)], $matches); 1520 $fieldtype = $matches[1]; 1521 1522 // Is actual field type different from the field type in query? 1523 if ($tablefield->Type != $fieldtype) { 1524 // Add a query to change the column type 1525 $cqueries[] = "ALTER TABLE {$table} CHANGE COLUMN {$tablefield->Field} " . $cfields[strtolower($tablefield->Field)]; 1526 $for_update[$table.'.'.$tablefield->Field] = "Changed type of {$table}.{$tablefield->Field} from {$tablefield->Type} to {$fieldtype}"; 1527 } 1528 1529 // Get the default value from the array 1530 //echo "{$cfields[strtolower($tablefield->Field)]}<br>"; 1531 if (preg_match("| DEFAULT '(.*)'|i", $cfields[strtolower($tablefield->Field)], $matches)) { 1532 $default_value = $matches[1]; 1533 if ($tablefield->Default != $default_value) { 1534 // Add a query to change the column's default value 1535 $cqueries[] = "ALTER TABLE {$table} ALTER COLUMN {$tablefield->Field} SET DEFAULT '{$default_value}'"; 1536 $for_update[$table.'.'.$tablefield->Field] = "Changed default value of {$table}.{$tablefield->Field} from {$tablefield->Default} to {$default_value}"; 1537 } 1538 } 1539 1540 // Remove the field from the array (so it's not added) 1541 unset($cfields[strtolower($tablefield->Field)]); 1542 } else { 1543 // This field exists in the table, but not in the creation queries? 1540 // Remove the field from the array (so it's not added) 1541 unset($cfields[strtolower($tablefield->Field)]); 1542 } else { 1543 // This field exists in the table, but not in the creation queries? 1544 } 1545 } 1546 1547 // For every remaining field specified for the table 1548 foreach ($cfields as $fieldname => $fielddef) { 1549 // Push a query line into $cqueries that adds the field to that table 1550 $cqueries[] = "ALTER TABLE {$table} ADD COLUMN $fielddef"; 1551 $for_update[$table.'.'.$fieldname] = 'Added column '.$table.'.'.$fieldname; 1552 } 1553 1554 // Index stuff goes here 1555 // Fetch the table index structure from the database 1556 $tableindices = $wpdb->get_results("SHOW INDEX FROM {$table};"); 1557 1558 if ($tableindices) { 1559 // Clear the index array 1560 unset($index_ary); 1561 1562 // For every index in the table 1563 foreach ($tableindices as $tableindex) { 1564 // Add the index to the index data array 1565 $keyname = $tableindex->Key_name; 1566 $index_ary[$keyname]['columns'][] = array('fieldname' => $tableindex->Column_name, 'subpart' => $tableindex->Sub_part); 1567 $index_ary[$keyname]['unique'] = ($tableindex->Non_unique == 0)?true:false; 1568 } 1569 1570 // For each actual index in the index array 1571 foreach ($index_ary as $index_name => $index_data) { 1572 // Build a create string to compare to the query 1573 $index_string = ''; 1574 if ($index_name == 'PRIMARY') { 1575 $index_string .= 'PRIMARY '; 1576 } else if($index_data['unique']) { 1577 $index_string .= 'UNIQUE '; 1578 } 1579 $index_string .= 'KEY '; 1580 if ($index_name != 'PRIMARY') { 1581 $index_string .= $index_name; 1582 } 1583 $index_columns = ''; 1584 // For each column in the index 1585 foreach ($index_data['columns'] as $column_data) { 1586 if ($index_columns != '') $index_columns .= ','; 1587 // Add the field to the column list string 1588 $index_columns .= $column_data['fieldname']; 1589 if ($column_data['subpart'] != '') { 1590 $index_columns .= '('.$column_data['subpart'].')'; 1544 1591 } 1545 1592 } 1546 1547 // For every remaining field specified for the table 1548 foreach ($cfields as $fieldname => $fielddef) { 1549 // Push a query line into $cqueries that adds the field to that table 1550 $cqueries[] = "ALTER TABLE {$table} ADD COLUMN $fielddef"; 1551 $for_update[$table.'.'.$fieldname] = 'Added column '.$table.'.'.$fieldname; 1593 // Add the column list to the index create string 1594 $index_string .= ' ('.$index_columns.')'; 1595 if (!(($aindex = array_search($index_string, $indices)) === false)) { 1596 unset($indices[$aindex]); 1597 //echo "<pre style=\"border:1px solid #ccc;margin-top:5px;\">{$table}:<br />Found index:".$index_string."</pre>\n"; 1552 1598 } 1553 1554 // Index stuff goes here 1555 // Fetch the table index structure from the database 1556 $tableindices = $wpdb->get_results("SHOW INDEX FROM {$table};"); 1557 1558 if ($tableindices) { 1559 // Clear the index array 1560 unset($index_ary); 1561 1562 // For every index in the table 1563 foreach ($tableindices as $tableindex) { 1564 // Add the index to the index data array 1565 $keyname = $tableindex->Key_name; 1566 $index_ary[$keyname]['columns'][] = array('fieldname' => $tableindex->Column_name, 'subpart' => $tableindex->Sub_part); 1567 $index_ary[$keyname]['unique'] = ($tableindex->Non_unique == 0)?true:false; 1568 } 1569 1570 // For each actual index in the index array 1571 foreach ($index_ary as $index_name => $index_data) { 1572 // Build a create string to compare to the query 1573 $index_string = ''; 1574 if ($index_name == 'PRIMARY') { 1575 $index_string .= 'PRIMARY '; 1576 } else if($index_data['unique']) { 1577 $index_string .= 'UNIQUE '; 1578 } 1579 $index_string .= 'KEY '; 1580 if ($index_name != 'PRIMARY') { 1581 $index_string .= $index_name; 1582 } 1583 $index_columns = ''; 1584 // For each column in the index 1585 foreach ($index_data['columns'] as $column_data) { 1586 if ($index_columns != '') $index_columns .= ','; 1587 // Add the field to the column list string 1588 $index_columns .= $column_data['fieldname']; 1589 if ($column_data['subpart'] != '') { 1590 $index_columns .= '('.$column_data['subpart'].')'; 1591 } 1592 } 1593 // Add the column list to the index create string 1594 $index_string .= ' ('.$index_columns.')'; 1595 if (!(($aindex = array_search($index_string, $indices)) === false)) { 1596 unset($indices[$aindex]); 1597 //echo "<pre style=\"border:1px solid #ccc;margin-top:5px;\">{$table}:<br />Found index:".$index_string."</pre>\n"; 1598 } 1599 //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"; 1600 } 1601 } 1602 1603 // For every remaining index specified for the table 1604 foreach ( (array) $indices as $index ) { 1605 // Push a query line into $cqueries that adds the index to that table 1606 $cqueries[] = "ALTER TABLE {$table} ADD $index"; 1607 $for_update[$table.'.'.$fieldname] = 'Added index '.$table.' '.$index; 1608 } 1599 //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"; 1600 } 1601 } 1602 1603 // For every remaining index specified for the table 1604 foreach ( (array) $indices as $index ) { 1605 // Push a query line into $cqueries that adds the index to that table 1606 $cqueries[] = "ALTER TABLE {$table} ADD $index"; 1607 $for_update[$table.'.'.$fieldname] = 'Added index '.$table.' '.$index; 1608 } 1609 1609 1610 1610 // Remove the original table creation query from processing
Note: See TracChangeset
for help on using the changeset viewer.