WordPress.org

Make WordPress Core

Ticket #2699: 2699.6.diff

File 2699.6.diff, 3.4 KB (added by ryan, 9 years ago)

Make option_id primary. Add uniques for option_name and autoload

  • wp-includes/version.php

     
    1515 *
    1616 * @global int $wp_db_version
    1717 */
    18 $wp_db_version = 12136;
     18$wp_db_version = 12204;
    1919
    2020/**
    2121 * Holds the TinyMCE version
  • wp-admin/includes/upgrade.php

     
    16851685function pre_schema_upgrade() {
    16861686        global $wp_current_db_version, $wp_db_version, $wpdb;
    16871687
    1688         // Only run if less than 2.9
    1689         if ( $wp_current_db_version >= 11557 )
     1688        // Upgrade 2.9 development versions
     1689        if ( ( $wp_current_db_version > 11557 ) && ( $wp_current_db_version < 12204 ) ) {
     1690                // Drop the option_id index. dbDelta() doesn't do the drop.
     1691                $wpdb->query("ALTER TABLE $wpdb->options DROP INDEX option_id");
     1692
     1693                // Drop the old primary key and add the new.
     1694                $wpdb->query("ALTER TABLE $wpdb->options DROP PRIMARY KEY, ADD PRIMARY KEY(option_id)");
     1695
    16901696                return;
    1691 
    1692         // Delete duplicate options.  Keep the option with the highest option_id.
    1693         $delete_options = $wpdb->get_col("SELECT o1.option_id FROM $wpdb->options AS o1 JOIN $wpdb->options AS o2 ON o2.option_name = o1.option_name AND o2.option_id > o1.option_id");
    1694         if ( !empty($delete_options) ) {
    1695                 $delete_options = implode(',', $delete_options);
    1696                 $wpdb->query("DELETE FROM $wpdb->options WHERE option_id IN ($delete_options)");
    16971697        }
    16981698
    1699         // Add an index on option_id to satisfy the auto_increment requirement
    1700         $wpdb->query("ALTER TABLE $wpdb->options ADD INDEX option_id (option_id)");
     1699        // Upgrade versions prior to 2.9
     1700        if ( $wp_current_db_version < 11557 ) {
     1701                // Delete duplicate options.  Keep the option with the highest option_id.
     1702                $delete_options = $wpdb->get_col("SELECT o1.option_id FROM $wpdb->options AS o1 JOIN $wpdb->options AS o2 ON o2.option_name = o1.option_name AND o2.option_id > o1.option_id");
     1703                if ( !empty($delete_options) ) {
     1704                        $delete_options = implode(',', $delete_options);
     1705                        $wpdb->query("DELETE FROM $wpdb->options WHERE option_id IN ($delete_options)");
     1706                }
    17011707
    1702         // Drop the old primary key. The new primary will be created by dbDelta()
    1703         $wpdb->query("ALTER TABLE $wpdb->options DROP PRIMARY KEY");
     1708                // Drop the old primary key and add the new.
     1709                $wpdb->query("ALTER TABLE $wpdb->options DROP PRIMARY KEY, ADD PRIMARY KEY(option_id)");
    17041710
    1705         // Drop the old option_name index. dbDelta() doesn't do the drop.
    1706         $wpdb->query("ALTER TABLE $wpdb->options DROP INDEX option_name");
     1711                // Drop the old option_name index. dbDelta() doesn't do the drop.
     1712                $wpdb->query("ALTER TABLE $wpdb->options DROP INDEX option_name");
     1713        }
     1714
    17071715}
    17081716
    17091717?>
  • wp-admin/includes/schema.php

     
    108108  option_name varchar(64) NOT NULL default '',
    109109  option_value longtext NOT NULL,
    110110  autoload varchar(20) NOT NULL default 'yes',
    111   PRIMARY KEY  (option_name),
    112   KEY option_id (option_id)
     111  PRIMARY KEY  (option_id),
     112  UNIQUE option_name (option_name),
     113  UNIQUE autoloaded_options (autoload, option_name)
    113114) $charset_collate;
    114115CREATE TABLE $wpdb->postmeta (
    115116  meta_id bigint(20) unsigned NOT NULL auto_increment,