Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/wp-admin/includes/schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,8 @@ function wp_get_db_schema( $scope = 'all', $blog_id = null ) {
meta_value longtext,
PRIMARY KEY (meta_id),
KEY post_id (post_id),
KEY meta_key (meta_key($max_index_length))
KEY meta_key (meta_key($max_index_length)),
KEY meta_key_id (meta_key($max_index_length),post_id)
) $charset_collate;
CREATE TABLE $wpdb->posts (
ID bigint(20) unsigned NOT NULL auto_increment,
Expand Down
20 changes: 20 additions & 0 deletions src/wp-admin/includes/upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -2508,6 +2508,26 @@ function upgrade_700() {
)
);
}

// Add composite index for meta_key and post_id on postmeta table.
if ( $wp_current_db_version < 61697 ) {
$max_index_length = 191;
// Check if the index already exists.
$index_exists = $wpdb->get_var(
$wpdb->prepare(
"SELECT COUNT(1) FROM INFORMATION_SCHEMA.STATISTICS
WHERE table_schema = %s
AND table_name = %s
AND index_name = 'meta_key_id'",
DB_NAME,
$wpdb->postmeta
)
);

if ( ! $index_exists ) {
$wpdb->query( "ALTER TABLE $wpdb->postmeta ADD INDEX meta_key_id (meta_key($max_index_length),post_id)" );
}
}
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/wp-includes/version.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
*
* @global int $wp_db_version
*/
$wp_db_version = 61696;
$wp_db_version = 61697;

/**
* Holds the TinyMCE version.
Expand Down
64 changes: 64 additions & 0 deletions tests/phpunit/tests/admin/includesSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -331,4 +331,68 @@ public function data_populate_network_meta() {
),
);
}

/**
* Test that upgrade_700() adds the composite index to postmeta table.
*/
public function test_upgrade_700_adds_postmeta_composite_index() {
global $wpdb, $wp_current_db_version;

require_once ABSPATH . 'wp-admin/includes/upgrade.php';

// Check if index exists first.
$index_exists = $wpdb->get_var(
$wpdb->prepare(
"SELECT COUNT(1) FROM INFORMATION_SCHEMA.STATISTICS
WHERE table_schema = %s
AND table_name = %s
AND index_name = 'meta_key_id'",
DB_NAME,
$wpdb->postmeta
)
);

// Drop the index if it exists to simulate an upgrade scenario.
if ( $index_exists > 0 ) {
$wpdb->query( "ALTER TABLE $wpdb->postmeta DROP INDEX meta_key_id" );
}

// Verify the index doesn't exist.
$index_exists_before = $wpdb->get_var(
$wpdb->prepare(
"SELECT COUNT(1) FROM INFORMATION_SCHEMA.STATISTICS
WHERE table_schema = %s
AND table_name = %s
AND index_name = 'meta_key_id'",
DB_NAME,
$wpdb->postmeta
)
);

$this->assertSame( 0, (int) $index_exists_before, 'The meta_key_id index should not exist before upgrade.' );

// Simulate an old database version.
$original_db_version = $wp_current_db_version;
$wp_current_db_version = 61696;

// Run the upgrade function.
upgrade_700();

// Restore original version.
$wp_current_db_version = $original_db_version;

// Verify the index now exists.
$index_exists_after = $wpdb->get_var(
$wpdb->prepare(
"SELECT COUNT(1) FROM INFORMATION_SCHEMA.STATISTICS
WHERE table_schema = %s
AND table_name = %s
AND index_name = 'meta_key_id'",
DB_NAME,
$wpdb->postmeta
)
);

$this->assertGreaterThan( 0, $index_exists_after, 'The meta_key_id composite index should exist after upgrade_700().' );
}
}
26 changes: 26 additions & 0 deletions tests/phpunit/tests/db/dbDelta.php
Original file line number Diff line number Diff line change
Expand Up @@ -758,6 +758,32 @@ public function test_wp_get_db_schema_does_not_alter_queries_on_existing_install
$this->assertEmpty( $updates );
}

/**
* Test that the postmeta table includes the composite index meta_key_id.
*/
public function test_postmeta_composite_index_in_schema() {
global $wpdb;

$schema = wp_get_db_schema();

// Verify that the schema contains the composite index definition.
$this->assertStringContainsString( 'KEY meta_key_id', $schema, 'Schema should contain the meta_key_id composite index.' );

// Verify that the index exists in the actual database.
$index_exists = $wpdb->get_var(
$wpdb->prepare(
"SELECT COUNT(1) FROM INFORMATION_SCHEMA.STATISTICS
WHERE table_schema = %s
AND table_name = %s
AND index_name = 'meta_key_id'",
DB_NAME,
$wpdb->postmeta
)
);

$this->assertGreaterThan( 0, $index_exists, 'The meta_key_id composite index should exist in the postmeta table.' );
}

/**
* @ticket 20263
*/
Expand Down
Loading