WordPress.org

Make WordPress Core

Changeset 11883


Ignore:
Timestamp:
08/26/2009 10:46:33 PM (11 years ago)
Author:
ryan
Message:

Make option_name the primary key for the options table. Props Denis-de-Bernardy. fixes #2699

Location:
trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-admin/includes/schema.php

    r11450 r11883  
    100100  option_value longtext NOT NULL,
    101101  autoload varchar(20) NOT NULL default 'yes',
    102   PRIMARY KEY  (option_id,blog_id,option_name),
    103   KEY option_name (option_name)
     102  PRIMARY KEY  (option_name),
     103  KEY option_id (option_id)
    104104) $charset_collate;
    105105CREATE TABLE $wpdb->postmeta (
  • trunk/wp-admin/includes/upgrade.php

    r11326 r11883  
    270270    wp_check_mysql_version();
    271271    wp_cache_flush();
     272    pre_schema_upgrade();
    272273    make_db_current_silent();
    273274    upgrade_all();
     
    555556            $limit = $option->dupes - 1;
    556557            $dupe_ids = $wpdb->get_col( $wpdb->prepare("SELECT option_id FROM $wpdb->options WHERE option_name = %s LIMIT %d", $option->option_name, $limit) );
    557             $dupe_ids = join($dupe_ids, ',');
    558             $wpdb->query("DELETE FROM $wpdb->options WHERE option_id IN ($dupe_ids)");
     558            if ( $dupe_ids ) {
     559                $dupe_ids = join($dupe_ids, ',');
     560                $wpdb->query("DELETE FROM $wpdb->options WHERE option_id IN ($dupe_ids)");
     561            }
    559562        }
    560563    }
     
    16571660}
    16581661
     1662/**
     1663 * Runs before the schema is upgraded.
     1664 */
     1665function pre_schema_upgrade() {
     1666    global $wp_current_db_version, $wp_db_version, $wpdb;
     1667
     1668    // Only run if less than 2.9
     1669    if ( $wp_current_db_version >= 11557 )
     1670        return;
     1671
     1672    // Delete duplicate options.  Keep the option with the highest option_id.
     1673    $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");
     1674    if ( !empty($delete_options) ) {
     1675        $delete_options = implode("', '", $delete_options);
     1676        $wpdb->query("DELETE FROM $wpdb->options WHERE option_id IN ($delete_options)");
     1677    }
     1678
     1679    // Add an index on option_id to satisfy the auto_increment requirement
     1680    $wpdb->query("ALTER TABLE $wpdb->options ADD INDEX option_id (option_id)");
     1681
     1682    // Drop the old primary key. The new primary will be created by dbDelta()
     1683    $wpdb->query("ALTER TABLE $wpdb->options DROP PRIMARY KEY");
     1684
     1685    // Drop the old option_name index. dbDelta() doesn't do the drop.
     1686    $wpdb->query("ALTER TABLE $wpdb->options DROP INDEX option_name");
     1687}
     1688
    16591689?>
  • trunk/wp-includes/functions.php

    r11878 r11883  
    622622    // Get the ID, if no ID then return
    623623    // expected_slashed ($name)
    624     $option = $wpdb->get_row( "SELECT option_id, autoload FROM $wpdb->options WHERE option_name = '$name'" );
    625     if ( is_null($option) || !$option->option_id )
     624    $option = $wpdb->get_row( "SELECT autoload FROM $wpdb->options WHERE option_name = '$name'" );
     625    if ( is_null($option) )
    626626        return false;
    627627    // expected_slashed ($name)
  • trunk/wp-includes/version.php

    r11589 r11883  
    1616 * @global int $wp_db_version
    1717 */
    18 $wp_db_version = 11548;
     18$wp_db_version = 11557;
    1919
    2020/**
Note: See TracChangeset for help on using the changeset viewer.